From patchwork Tue Feb 8 18:43:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739214 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 D409CC433EF for ; Tue, 8 Feb 2022 18:45:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385370AbiBHSpC (ORCPT ); Tue, 8 Feb 2022 13:45:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385189AbiBHSo5 (ORCPT ); Tue, 8 Feb 2022 13:44:57 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3AA8C0612C2; Tue, 8 Feb 2022 10:44:43 -0800 (PST) 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 218Gqsc5026751; Tue, 8 Feb 2022 18:44:08 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=Q1NIwb+2r1U2jtlr3RdXJ8qglw0iSOw8bH8mLxYS2B0=; b=AxXm6gdA8pFxoipOduu/tx55aqgJaWyHp+J6unIl45u1N4ezcmdiueEgavy94mVKz/5Q tKCFDRsWWMeuAaROFy9hverg2iTfd6qkRo3XWYC2nSm5otxtzMbgIHhRLC7Br0uWkX3V P9zkZmhaw9dtl9/KlRVOpjtEWow6gxur90Xe1QR1AGfcvL5iEJxlWJsC3NBVuXuQLWAP SsLXgHCAGZsL8hWhd4Xlkcayf6UMxlk51EhRX/4KztIWWXCFgEN90Kl4ZwIoxPc6iuVL +HqvoLBkeZqbExEH1FZ4nNEhTvYJssjgEcHSM2P46gpcGbX75yFZ/iV3ZYD/CxQ2Tp/4 gg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3e3h28j8q9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:08 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 218Ia2Q6166506; Tue, 8 Feb 2022 18:44:06 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2044.outbound.protection.outlook.com [104.47.56.44]) by userp3020.oracle.com with ESMTP id 3e1jprbdcg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z3ofAIR0QWZZfwVrN3VN6rNxo/3jHauAL4wQ/N4hYR54YkGAjpXq+SW+hBJi0abEb53w9iVTJaEOXtWJvc0sV3HYOJg6Vmh5fH0mD6Z0CJnH68ZpvWp9tWG0dF+tnsLE1a3eK/xaUIhI8yH+ViWCr1wrIqMdaG9xHowgffxU2/1higndr0XcOW27AaKUCg+tY5izUYo39tORJq8GYYbfHXcNMPKgIEeb9k/N3rNI76hrJsp2fv1qLYzxFMu/yziq/CivyC5pTWMWILS4yxtjI+9LRhsTjH5yoUk7F9LpMAhI8W2/gWEI1tWP9VgDv13AUlaTlGtb9stzko8S7rL9SQ== 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=Q1NIwb+2r1U2jtlr3RdXJ8qglw0iSOw8bH8mLxYS2B0=; b=GYUawMwmN9I0xFHa4XGJTyeYUj/2AMz+77kozgGvRo/sF20HafchhB24a+FBNWiY7FA/Urix+SsmNNVSL9dNZra9KYhlfEBxG0qM1WklTj3e3KEJCbdf3rzeIRxX4QapShq5gKilGVCFR+CrZeP8QoEUilCdbBfa7Ph//6ISlvVtsYnCTnTF3JlObtemVff1k2uDSjv0G/R9Qr4dkZwjY6U2JQ/oe/1uZOGKfj8qwZD9IMQOGrzX/QlpcBX2VqM5brS7SrkZWa1Ki3KxMPHA/FKzsOnUIAhaUn4rI4kqiit5ckX3IwQ1CD8Y93WY2u4P6+5AXeYo25eiBSROwQof9A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=Q1NIwb+2r1U2jtlr3RdXJ8qglw0iSOw8bH8mLxYS2B0=; b=xZ3V5MKdU7sp7j50z6rD970adOimzRHcfob54DSj2b0VK0UEVwBdQNAoGsXvELBw4K1FOZldgrVmEb75xCy9EFtdaEFgC/3GMw0rdS8ryHWe7RtrVt47K9eNqSSLC3kZAmRA/WkbyxnEHw9FF+beaG8n2SCjG731ooe3/wZZCi8= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5781.namprd10.prod.outlook.com (2603:10b6:806:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Tue, 8 Feb 2022 18:44:04 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:04 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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, Victor Erminpour Subject: [PATCH v8 1/6] kbuild: bring back tristate.conf Date: Tue, 8 Feb 2022 18:43:04 +0000 Message-Id: <20220208184309.148192-2-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: 7d518264-eaa3-45e7-cf53-08d9eb32fb41 X-MS-TrafficTypeDiagnostic: SA1PR10MB5781: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: 6vUOVLJIllmX3jSf0zJobJ9JPLPxkZxrRlNeYbDbfRGVIIhI/X/IZkg0JwhMfjtpIUav0iQ3+m04F/CvG/WUP4Qr+mDvtKnHI6+oCvWSyEErg3XnQBS2SwLECC4bhN0NQcydxXRUeo6FXW60ukr2PRjNqehdlppfSWvGhVznWyMoAHQhLnFQzP9/yW7bhBHaFpyFZaOH8+89nhH1049RyZVJrlhwvqHkbp8R6HF2jQK3J545FGsUhdKfku/7uHaeYvfEzxDh2FEiP0USLKbV8oupWobK7j4tk7T7mpQvSgDO/ebxbne7DFJS+M6sIsmzBTEJHdyNbE7WjAnpfB8NB+DeZZsKu1lvMCQc7xMryq/J8rTmilaQyeT95fPX5pOlrTm5BrF85fZnNR0h/L5qJIE4h/OlQQeE+bYqH/zILptyP6Hp0gAOHDAU4zRS6o4hUDfVLvR7mPyOdOeZAR9bbx7Yr84o6bCGJ4nAvidsMy9n9TJX72/kgeRnrYnsg8quhRU6DqDUUIBshpyuaqUXx6cu73F5r0EvMRd239fvtTulMiaqV58g95iAo34ZNo8Ucrb1VnXIaZQOcEH71dY5NKiq2lx5RyWhPqmxYSgbSKu4ZgCANa0vkgmkBVK0j+rnbGjJDXWJeLllSH7WIH1R1w== 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:(13230001)(366004)(83380400001)(107886003)(86362001)(6486002)(36756003)(52116002)(5660300002)(6512007)(1076003)(508600001)(6506007)(7416002)(186003)(8676002)(44832011)(66556008)(316002)(2906002)(8936002)(66476007)(4326008)(2616005)(38100700002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mZOOQUodeBT7jVln2xp7Mrl740P0u4sLGRNR/F1maqZpb9fSsaY1AujiZvlEIIUrfNFN4Bw7KmIhiuKtvG8coPKYRq+e1p4yibw+070mjyZnFQ/K5srU2h1y2ha62Xai3Gg30Nvncj17Pvfkqfr8WjDan6XVhqpQOQsymtMIpGna8Yp3Uf9QwMqgLFh0fsnR1BLP6nFtOHX0G65XbfdyzgOhksRtM/zOCyz6U9XRb92GED8wKpwMN91+NbDjlkImc9zIQL++LqsKEK6QijMjTQBxiv1SbW8WsGC6jIhp+iWzuRNQpYUS2gJcTHKT9SZkJXWvgbElD3TPnlC9TdgNCjHqi6B2/4Gj8JfAYVTLM0QdSxCBE9amS1NlgBhgQnwzM2s7u5zzGCH9yG/aBPnuuful4+HhxxHgnutLyLr9liId7GdZCkoMd8ifk81IYmNp0R9Nfc7kknsC3DIcr4K7EX7Q5BUbm0UJWun3Mutj6XIKrGn7uULj1YQSH/owwEMjjuN7I9NoEAZFfbIOXLCyWF5vj++dX0KYlMQZ0Gy/0PPczOq+Ws4BssjN4t3309sTwgAJQVpV41WHsuZzQKQgR+QGYVy4VfEpV63NvKVJgVtYAInyg+YH2ahzsUFaoLsBNC9Fip6ub7ggG6HHX/prYFN33id3NWcL5YRfzKiiQdUoBg0rrRheGr4SRtW5BtuXkWkBMFViRqFNbzteWXEn/sigkZ2UhL0CzVhmw0cOSwC3aiM9Wx0gZQHJKE6l3pwWFkQ4YePiD4tFHwD17cEwaFfNo8VrinfYTEg84WvJXnbM+F5wvSNKtA1uYP+YPPlrE2Y3llCsYArdcilvUf91CUxd/sJhA8E0IwG1w5h98G0FFzIPQ4WHVTa0aJnJqfdRTnHsC0WTWH+ydsPwZ1PTXaZ1dc5uuac8tWaTnbWIAdz+BNvDFp5JKMdZURM5zs7qMuHsbSDJw19hrbC2d6r2JLK+NAkZVT3Zwj3uJpg3UPXh4MqUCoYESgxuUnZ6umdtITzhIv5xsNpInJ5LtSd1y8AMGdcuOvvukzGXonF4C491kkpZn2ZWFbWerou+w8JsXLC7XjjQOvt87KA95ECPXrBbIVAb9FFXhdpUp5q9OXYqJBCG5Od9cObhsAu3aH6ycsXr34TNcSPRf6/NJzehLVYPW56NywCPVkVq86H1+LO5ppNHjCCaGo3DEDy0GwKlIgrfoPei5KKzY+kJvTR2x9QLW5jpnM1jT8yOayWWJdLdQK2wC2FZECwOtu0M6FwZuHngDsIk064DDEawjGfY3uw0VabH99gCchKoQMqwQ+GVgzKpqWpAMhBYkduTt2L/BTpsuKbevPXk30/Jv8OnWW8bp+vaAFjgv50o7VWU8QgGaE1SWmVU5t2ikTM6Yl+wk57+xcalC0PZL9FtEO9n9MnzZQzdHaxomMKS72Nml1/5Kiqkrl/V5qM7W8YLc81tiCw/NR9Vb18EHcBEw5qtxBs2gG7jPJ2hAMTnuMAqEYV0+P7M9iynOti9DlA7MoC5c2r/ae4IEf/OqJXGAEF5sCastZT5WQFU+vvzcgtAq+tgqQVVwtQZq5ORdjU/g9alXraxeWczO9wYq7X4RDrdFjs1F8w63RORiU56tJgUlBS/RAnnGuA+wrDAMyhj/yTK X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7d518264-eaa3-45e7-cf53-08d9eb32fb41 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:04.7484 (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: SMeACkdKEb4ZqsMN8jYmd52cvOSTLtdvQpx2+8HXUr6lyyGmTLj/G4QDujinTKHq07ExXoH387BGWHMUprB0HQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5781 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 bulkscore=0 malwarescore=0 suspectscore=0 phishscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-ORIG-GUID: 17MMnhxkdo9khO4fXsLG44v3ZgXpAOIT X-Proofpoint-GUID: 17MMnhxkdo9khO4fXsLG44v3ZgXpAOIT Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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 Reviewed-by: Victor Erminpour Reviewed-by: Kris Van Hees --- Notes: v7: rewrite in terms of the new confdata refactoring v8: adjust for changes in 5.17 merge window Documentation/kbuild/kconfig.rst | 5 ++++ Makefile | 2 +- scripts/kconfig/confdata.c | 42 +++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 7 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 ceb987e5c87b..199b6f388484 100644 --- a/Makefile +++ b/Makefile @@ -716,7 +716,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 59717be31210..21dd9112311c 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; @@ -667,8 +674,12 @@ static char *escape_string_value(const char *in) */ enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE }; +#define PRINT_ESCAPE 0x01 +#define PRINT_UPCASE 0x02 +#define PRINT_TRISTATE_ONLY 0x04 + static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, - bool escape_string) + int flags) { const char *val; char *escaped = NULL; @@ -676,6 +687,9 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, if (sym->type == S_UNKNOWN) return; + if (flags & PRINT_TRISTATE_ONLY && sym->type != S_TRISTATE) + return; + val = sym_get_string_value(sym); if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) && @@ -685,29 +699,38 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, return; } - if (sym->type == S_STRING && escape_string) { + if (sym->type == S_STRING && flags & PRINT_ESCAPE) { escaped = escape_string_value(val); val = escaped; } - fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); + if (flags & PRINT_UPCASE) + 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, PRINT_ESCAPE); } static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym) { - __print_symbol(fp, sym, OUTPUT_N_NONE, false); + __print_symbol(fp, sym, OUTPUT_N_NONE, 0); +} + +static void print_symbol_for_tristate(FILE *fp, struct symbol *sym) +{ + __print_symbol(fp, sym, OUTPUT_N_NONE, PRINT_ESCAPE | PRINT_UPCASE | + PRINT_TRISTATE_ONLY); } void print_symbol_for_listconfig(struct symbol *sym) { - __print_symbol(stdout, sym, OUTPUT_N, true); + __print_symbol(stdout, sym, OUTPUT_N, PRINT_ESCAPE); } static void print_symbol_for_c(FILE *fp, struct symbol *sym) @@ -1131,6 +1154,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 Tue Feb 8 18:43:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739213 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 94279C433F5 for ; Tue, 8 Feb 2022 18:44:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385058AbiBHSon (ORCPT ); Tue, 8 Feb 2022 13:44:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385015AbiBHSom (ORCPT ); Tue, 8 Feb 2022 13:44:42 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A8DAC0613CB; Tue, 8 Feb 2022 10:44:41 -0800 (PST) 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 218GjxHu026745; Tue, 8 Feb 2022 18:44:11 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=nSIELne8b69yJidBmyRYmkARSMNrUgCTXjrhp0ZAVlI=; b=mNkbwqab09eAAeNQz2wrKclSLG1qFtDC0OW6Qshl5GGgcV7/13wBGDfMj2+LU8wrwgbX Bf/RqqKIUoJr/O4RM2XaOM93lEijtlUOI16IDgGde4HgKQvVjxK+x7B8OZDJXoFnPEfp WYdph8NbCcN4JeZKdgcL77kj7AwbnjMag6GdfUaFlRDUjpv8KCBp/ok76fiKAVhWIxVr vK1l8G46mw3Uz1EQ0A9yfRHvwhl5+ko8aDw/PC3vlgQVxw46fpw8MdH35lJX5PRoTWri 9C3Y61qjX9VOdsXTw83KaT2qco65lPDQrb/LIhl8wgltPzb06T+g/qwwwyiBJOkqcPoa Ow== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3e3h28j8qp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:11 +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 218IZXLK159273; Tue, 8 Feb 2022 18:44:10 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2041.outbound.protection.outlook.com [104.47.56.41]) by aserp3020.oracle.com with ESMTP id 3e1h26thr2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hkO8lZ9IjUSyTLgaUPyCsMJf2mFRXvOD99pmVajwki0HclD6bBepDbkiTHju3xikD2XSJ5IzZ/AoLfpMvHv41jhen86E1eFTNqtw4oLWBtDpUif7BVXUJmXiWx61TY1wv9hswnDqIo/Gv7Tk3ItL1OAkIFW9MJvRI1AeyVFIB9+Pj5+hfRUBDJESjM5wi0CTOpXCe/MqCKj/4gGQq0zxZ9bSQkaSEnxnBvbo9eFHKRRV2S688JrfOXy/Kef3+T1RQotEQM7r80x1adaXpbeXEDZ9Ev1LbUJKuoz0J8GoFgnR+ZI3O7e2z1IoYuNc/0XigLCwwTFNgekowFFKkJ7jEw== 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=nSIELne8b69yJidBmyRYmkARSMNrUgCTXjrhp0ZAVlI=; b=QnnC8PIfgFzQdyLdeM4ooRAOv2uWGoHZu+YzaROv2j8s/52lG+z9Dj8nLuqJfccIrv77Oj7oSOkj/S8hvkLP6+hW7a4/ex1He89RQaGxQp1IJYGkjF9KDRcoM3csiIztgOCoxRRixlSUgmNZsxEOJFCuXjaHMDCjZOkGLhklnuQtw1iurIZ/1nzb5+cMHSy0dZTNbEJSHQoegBYZp2KYGZOJ9czj6OdFP2AQEo1sGkcYTemroCdBqb9MJjn0SPPzGxb6kYyN6SSFDzZ1F77w/wEO3BwBMqy5vdgq5L/2G2602JtFEjfj9DiU34zXcbjdCevpSuraiVz60Azw9q4TGA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=nSIELne8b69yJidBmyRYmkARSMNrUgCTXjrhp0ZAVlI=; b=rCDenqcFMVz/Ol2mQ5hG+BeLLq+anFE6w6xQSyEg4W2WaCjSzpYKBLSg17xEUS+jNg7JcozO9OrChPxKawOvyspfkgVDIo8I4lbYtu6M1kgeoCnuYzBScI9Yv1nGrq1+QuXTROJYMktcsFcwb3zQFCRm6OyN7WfO5FA4pMN/+Bg= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5781.namprd10.prod.outlook.com (2603:10b6:806:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Tue, 8 Feb 2022 18:44:08 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:08 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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 v8 2/6] kbuild: add modules_thick.builtin Date: Tue, 8 Feb 2022 18:43:05 +0000 Message-Id: <20220208184309.148192-3-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: 94458f20-412f-4f6c-1425-08d9eb32fd64 X-MS-TrafficTypeDiagnostic: SA1PR10MB5781:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2803; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 97XPM24X/9AelFQ+3fhuROOeliS56NT5jOluSorMbnMH3nWxTJ//V4M3UeYM/Kbr+cdG7jT+Kj5BjjMgYuJYrtJl14Fvss3jN1kAupWKZk5/vTSFyZ5fXQqc1oakHSYlM66jW+JzKp2ujHbsgagw+9bjvfdHWbQOYUh3fDhwGKeQnlxh2Xb0TpQpWsdcrWDTJdqrnWpp8mNI09E0DM3i+dW/RTP989kcj7g7PtvdLhgD4QPYuFg9DXnXAU5I793Jf7fwIIeLasfP+2ZAQraEMCjDV/7ElBrkTQDWlj6L4nK5tR3esRCCEiKB1aLrFewdh50w/b9abeChaljGQ9mAqI9e3zzInmjgUlEJq5zjF0NWqduSDL4w30pBbC6r48J9lQm2qnrF4fgleM3xNdh3S6RN7QvzTtkWbc9OrL5P7xFvAEuVCt3XmuCC3BPUZbjo+RzyQhlx5XmKpVj3HmABmsW49hPZSNLQYkAa+xBeFDpEDvpPfsURAgMzUIFo2ladmFZM5SOfyhaavoLk3DfZaOxJtrTwgxMxnUkPP2/p8pTMX8qKuiesyV7Q46mI/iXkXqwFoJslcDMkcAodE7ZFWLoaZQCfcWAlTuhLaofBxT4zr9gfMfkk0kq5J0iwvd5wadoFNfvDQ3dx58F2lXPXyUkYk0pN+1N4DJrylvSMmin0yscrGvKkElPCezDzJIdfeGwro4lhZVzCqjsF+YFj/Q== 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:(13230001)(366004)(83380400001)(107886003)(86362001)(6486002)(36756003)(52116002)(5660300002)(6512007)(30864003)(1076003)(508600001)(6506007)(7416002)(186003)(6666004)(8676002)(44832011)(66556008)(316002)(2906002)(8936002)(66476007)(4326008)(2616005)(38100700002)(66946007)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: FWVe8jzSQZCCLspfxeOtddu52PSAextifZPUvUdyTib17ATF15STfhWO2UN38IIz45nzZgVyoDsNZbQ8KWCqx2r7PeTXxrILKpwu4NKBZltt+DcDfQIdszxvxbJuLzRcjjRqSr6FovxiyFx/zUY7gB/3fEcpSfsNmTB9GyshC5U+ont0ye1uvnU9s1nZHZqIqx2B5socGN1ZvuchoiPOol4FragNAjpDxMrJDOibAfcCVVWB92oDyMBQuBmJcbv4j4ogCxUx9ilsqMzHixUfVuQ6f6ZzbFwcnSY5pzSgYJu7bTz7tgeSkg9fpyhUsRHIrXSCjl81sdh38d9mr9kNLYI4egF55O0NoCoMNOSiy2PIPYXyavAXUHKQx8bBISNcPlJ0LvSiExjS386QLk9+pUNCZq4lrzO1MgdI2B7E8aSb6MegyGriw4VR0Wjw3LYXuLJ9Yj9inL0du3PYld4EnsBoilX83t0WjWNdeI69j4pkVyKJgPqq3D5oeMKYmV/O0ZbQi9XofpFkBB0HqNpmt8kEx9RiCV7gfXS7tfaWA2SXyhDxNxM7Qsvf2uvBJ0oGjKIXZvWGHRmrx2glp2BalzNWr/nBBvGse5PKShKrssMJS9Ne4d8xEl5SD/TUnpzY2noRuJ2+RH3/QyelOh4j5OLc1FXo6/k8oyG1HDx+oAxFMPDwc9V4HRrN3M5vRAQFE1xnUYcKytfdtvQB+Ywu6TnShR7uPTKqCWnh2/cOKvbQeY6SAgCa6zE4DzdNfPCAwXMl6KoQ8KkmTM3skdY7LGQ/SRDkwrTqcjjpgnmGQfEu3sySRzSA/chDUTr4mqTQ8OzlUU3xu9jf7GNyBwfTk38vFiQ7mGmUz6GvzvJHmuLrNYvK5rygRb7k5Hun/De5V35Ldk13KNsUNIn/Zw3pHlw5aHzej3alpyYd0yn54FbU9KOmbpTif2OBRiT4A52DSmCqiLJ8wAK1VbpsHgYcMjcrSySNnMfyAcHdsvX4wKDeIpb1PZb/DMEZFl4Tzqit6c2WimtVZO4fgc/KrgfToXRvKbtb3y19dQL7Ia1YPP8ykgIlyH9GTWnKmn9NHtVDo0DsFjzpeyQtXKQi+J0rQpBpFCkp+589e03YuHeTfL0dKlFZMZ9+qeX2cX4UyJfihIc6H5xenwaDtsi+l6V1rmSDCYk+t38WBSs9kc/UNqAr6dKRspMMTuXKfL4r3P0Pf2yiHcjcn6Ui2I05zkjhO/MwRHXhogiOl5h5Ogk4m0NAfGUsjbSV9b/ezN2i7NtB82W8dMv21cO1WcCkZZtrAcJFm5xRXUW8o8/sR8lYYsYwis9IPMkNUw1MBPA1pEWATfj8QBaRZCgDBNQsK7DwLGkkimH+L88/qEIhv18BO7+ZbeOQ1vgc5O+0Ui331NMnNbFmM+D2ycu+vpGsECg1JHaN6HDs/ZTfY48QfPcKv3OHkE+VBjbTo/DgYhxLRUlVJ2E7krCONlHhFT3q3JlfCgEThGD+bB4PelN6LalketcmcBLFVKWoA3OnaUAeJg8cOq/YWsChRzeBSqwvn8cCbWH2SgXrMmuVqwdvAJKhFAGmWE5fjrUN1lejHaxs0WKHPe2VwEErL1wGOvaGZZ1hkhMCEX3M/9rRFQIH8MkHPZJ8AIksIW20Sgws+hyz+vfd X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 94458f20-412f-4f6c-1425-08d9eb32fd64 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:08.3128 (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: bND8KScB2XZG41PgEQYBE07G6FdEXn87fT/4U2zo2NnkIIoEgZT501EWAZSvwbyerRkdKib1fYCuU1ZsppHrlg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5781 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 bulkscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-ORIG-GUID: 7Qy0e2YLBvTxzveCkOkPoQBdc2DhB0eN X-Proofpoint-GUID: 7Qy0e2YLBvTxzveCkOkPoQBdc2DhB0eN Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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 Reviewed-by: Kris Van Hees --- .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 199b6f388484..5e823fe8390f 100644 --- a/Makefile +++ b/Makefile @@ -1470,6 +1470,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 @@ -1849,7 +1866,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 3514c2149e9d..167bbbd5fdf5 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 Tue Feb 8 18:43:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739218 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 EA87DC433F5 for ; Tue, 8 Feb 2022 18:45:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385361AbiBHSpO (ORCPT ); Tue, 8 Feb 2022 13:45:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385278AbiBHSo7 (ORCPT ); Tue, 8 Feb 2022 13:44:59 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C54CC0612B9; Tue, 8 Feb 2022 10:44:46 -0800 (PST) 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 218ITQ3E011786; Tue, 8 Feb 2022 18:44:15 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=OKvrIewsRC0n7wQ1ADm9yNHvKDdLBMnjMk3Hmg292Y8=; b=vDWJDSDWJouAYAgbTNugpcKcsEm4ZRKe4muzn/jzntOfmYCSuoS18y4RieIeTJqMHrDh RX2pSfgTh6n/NMStxTGuAJhXJVd2Pwiiz/0tuGjgmIHGgKzFUGe0zugVYl//K2D/v+27 thvbX+r51WFx9B2Kp/6WcrWvUJO6xdn1R3EBHX3qFohtH9EEWgnYKRwlkoOXVUYIsamr sexRZpn17387J3H6KL28dSdMW7gjMfaX2PC/l1sULcSakvUxny3PFJPQqncrg0RUVyh4 4BYNMN+B042SXH+h859F1f3aZDBfOJY7n20IkQ8VPv9ycc73uc9hGFfe+OENZsjSYZi5 Og== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3e345sma1x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:14 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 218Ia2CI166549; Tue, 8 Feb 2022 18:44:13 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2049.outbound.protection.outlook.com [104.47.56.49]) by userp3020.oracle.com with ESMTP id 3e1jprbdg3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W04DJFxhFg2CI+6KMCwe0xiuX8SoRTP8IYGizdwPnULivNDvIZNc8nnisgGRNNA2b0dE/Z5jJxO0Yy6zfq+f5r/91gFa3tpKZn2GL5a7jI3yhoZpCxwiwcH/ySsu/YVhhtAmq9D9xDWHq1Rt3IBjTu3ZqsfedVfojqEruhLr81SsDFe7xO/bJKRCnhIg/JsdtfNtuCCkujVr/stVnnNga+HeRLqy7xe4UGKdry+nz3m2FyCS5ImBwVpBOmOZ9Zrh3cBNE4xnU38o2ZG6iYmBHPivy5sB1NjNAeixujWi9aPyfoKOnqeK32bpzqiYcRDC1nsrIWQO/7kYw1PbpXfL/A== 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=OKvrIewsRC0n7wQ1ADm9yNHvKDdLBMnjMk3Hmg292Y8=; b=MDIR+ViTcxbq6/iDBzktbXqfLqLlCrx12sZgnwHwLzdSTdoIn/D955stJS30O2cOO4wv36FpK17N/eWNDigjhOPluRhktct+PyM6sMrs/Fxv1Al9//DVj7CpvuwXCJTeBPvfMC7D8A/OzM+1SOGVFbvNecp7Go2Z3W25RGe+Et4Hyyqh83jJU7KCb5rBxzbX7+KoY+U1lY+l1e5zFfuuo0IoAlA2+BwYuMN0BWw2jtTISvt0D51HVSf4c47/4Em95/gczuk5FBCwOErT0WElcsO++PixVGX+4Edg25uS/qeOHKwD9/BAIwyd0L9rzPg8jj+D6B8/by5o8LrrwQdq7A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=OKvrIewsRC0n7wQ1ADm9yNHvKDdLBMnjMk3Hmg292Y8=; b=humf0VKDHY8wXVG7+BkdfAUGOEf5zzarpnVDXHmY+io+aqLSk3Irgt/Yp33tVXHHxOFH1hUzS1PuzlkZtGmiZLkik+cAYY1C3Ton80QVIPwFFVQLCNUlTlra53Gotz7NNwa7HhTOOioowXSqHAEOB8oCuMR3BpfXAFf+Ywbu2xs= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5781.namprd10.prod.outlook.com (2603:10b6:806:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Tue, 8 Feb 2022 18:44:12 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:12 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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 v8 3/6] kbuild: generate an address ranges map at vmlinux link time Date: Tue, 8 Feb 2022 18:43:06 +0000 Message-Id: <20220208184309.148192-4-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: 7299e23a-43e6-46ff-a828-08d9eb32ff8b X-MS-TrafficTypeDiagnostic: SA1PR10MB5781:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hi+ZgWJuHnvJjNK5TRzX9SH7BWjs+tNHZ7yIUOreLn0m+TpJGdxE4dBncWAq4PxklcHmYppJSx1KIWq6EimIAEx7PUHWnBKoMNsPIjQlOGnVTOT4jF8N91oj5OM9I6w/m2VZS8zuzS2GuGilfJxbut483O2dDCvTF/rmjbW9RtMNSOBI8lj8V8C8RXlJdspYi0xDzUCvDfZmX2ldmjmZFczfbJWQ1jylE48kYplYXyLZDJRSu7aEz6J7K1Sle9ly4X/lWAZzGgFA4UfcXzS1iCKiZbpxUMofiH19hihyKSha3p/NVlqs9Q93zEsV+GpfQG7+7nHqDkvlR9An+SZKycm+1qx2fFo9L/QrtR93+/3YIvvPwvTUk5HrBrF2ljiJFduhDjBoT1GYY4zbW3KX3TI9zbJiObkq35jfTXzp8cYPvKO1dcXay1/TObWAJ2kShmOhefq9ZeP2i43ryuuvL51CGjbx6dvDfFfLxsJEfiv/TeE7EHocjZIu1W3HLdMhjp/6ld0XQ/uoN7JAwUCdFItyd9WGJRhtf38AXjfvWuTWaRdhbaKJW6qfClXFn+EX8UbvapNG9JaaF9VXut1CgV2T941QFaO3McW1AwPuTP72tHqajjUtGAh0w2dkprLaPVmvxUBy76MUIOR35pkdqg== 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:(13230001)(366004)(83380400001)(107886003)(86362001)(6486002)(36756003)(52116002)(5660300002)(6512007)(1076003)(508600001)(6506007)(7416002)(186003)(6666004)(8676002)(44832011)(66556008)(316002)(2906002)(8936002)(66476007)(4326008)(2616005)(38100700002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0WrXCYwdBBG3yJITlGaqjgM+3ta16W7RnbwkoQKeTFXiiLO7CAVJ6ewC3eEdN2EmnqZgahYdayiYLCYPDEnrG716rUA2NoCEqXGs0yyD740gxMrAB1hoFV1o0oK3nHOsyziEb3sLlrsjRdEh2BAOAUOZAKQYG8/DiIVT8HX/w8OGn4k7JFweB1C/ZbgR+H7/1ijO1bP3OZXYLQRbAx8wETxVmQmu8enSt2M98HGLPFQET+J5l1+S+w33EFDqWGkpFLox8dnPbJUfuaBUXVdueL0+gKdkZpZwttSU1ZWpQkTFJeNEkifxgDjsSDIkx0VhxZxXJCgKs0+LB53AfwTvjBaXuWviguD0tjh9+lvFqWQ7Pabtq6dY0EtYWYNmj2NiBrUim5gICAPyiLEaJh9q0GwOEdq1eRHpp6ZEfIQ/XDat9qO7/37apyStESQx1jRo1olvPJzKod/T6kNnG2HzAWA5e1IE5x43u4th4OvmpB5l0TRTSsNID+fOwhdMUMfR65trEW78CA0ze5Xx3xFSSBqkJ8/hO6zUpaGSzbhwfWpJ7RBCR1mMt4yVTxpNtNUeEIRSlTScyLYFF0uwKYHvZJcYGkXwCz7gBltU8+UifqG9hSb5g2WL6y0jczVxhwlJRAXBww78WVxc1Ggt2xRItjIJX27jyXyRh4rlIy9V7k4aFk+MU0HhuFwBlJBC5LA2iKlQOstIcGHVu9x3b9qRQCWsQW9VipSwbogz/Tmsmic1vYEEOGmzpqpROH4UeIhMynd6dPlpI2oFn55SuMZi269LlST8IXo1/BVoCHYv+8oD2srGz1yqVv8D9/J8+yiEBOKyyg3wFEVWiEBGKA3XR5aovVfanoV5MD4I0nWao8KcI71FfbBvj1N46hJqv5RDNBEaUfkU7xufrMDLEgf/PwoVCGAi2pSe2qnUIYRN6NoBPVnSetKomdk1f1xEt8k4qd3WxYMO2/2tF9z+9KSS3ToqOc4e1iHWW3IsK/mgQ1kR7CJQESiyPTC2frVnSl8DsxGZ/tMjHKhw/j0RE7mMaVle8LGJ/zQLseJ1AQH+8mPUDlxGX9B9S1REvLbQpM/2RlZDLHd8uUg0qg2yB739Lf0ek19wCWDpjVLfG78qzSyq04h1zb0QZVmnv96IE/h5R3372Tn5kYR3p9E9+dvv4bgbT5iaaLKXaqw6NaQ8HmAHgoq7K3vFMeY0Lp4tKhFtBgnJkz6Lzm3ywgMdQNMFAzNVc+ldbpu+spoMR8zGVaDbDj7QzJXq0rim0slHMJCqQd88Or5cR1OOWUtILBia4wqioDEpfXIe5A2FKXkBUCvABjvgZ8IZcNroE+QAFG401i7UASRebWxFx+EGNIGW3gTQYRAsVX+o3sUpQbpIW+OnslLkhaBi3Z4jXDx5L3mWSXoiU8sej8fq8KDLjRcm6STCDQVJMqZooTckXzZjznzKT11ewl4PfsDNF8Tp7hOUrdD/ceqlil5C23eD6Kv8BPHwP1b5LDy6M8XtQOKkDOKisXi2vPuizfDMCf7UdH88KfhEJuBv/2S0fCl6x1AlA0tl4j6NTOz2wuFvLvDW5cztZwd29ux/T4iRMPWC1j9fVDNlThOwAPj3MO1XQROqf7OWJM/I5KNq7gnht5en4e0E7yFEUOEHcEBFmB/VSdQM X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7299e23a-43e6-46ff-a828-08d9eb32ff8b X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:11.9432 (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: qK09pNms3OvQicK92w+BTupqufgjo6qho0AWgg7k8JfA/pQKOpsb7GRsBJpckRPiyGJE9OrN2wqaxllG7diKwA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5781 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 bulkscore=0 malwarescore=0 suspectscore=0 phishscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-GUID: DKmjGeWeqnWJ_J-6kbtWOobFbYo8HlKK X-Proofpoint-ORIG-GUID: DKmjGeWeqnWJ_J-6kbtWOobFbYo8HlKK Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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 Reviewed-by: Kris Van Hees --- 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 666f7bbc13eb..981cd441ca21 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -203,7 +203,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 @@ -246,6 +246,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 is_enabled CONFIG_KALLSYMS_ALL; then kallsymopt="${kallsymopt} --all-symbols" fi From patchwork Tue Feb 8 18:43:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739216 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 787FEC433FE for ; Tue, 8 Feb 2022 18:45:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385322AbiBHSpI (ORCPT ); Tue, 8 Feb 2022 13:45:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385359AbiBHSpB (ORCPT ); Tue, 8 Feb 2022 13:45:01 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73E61C03FEEF; Tue, 8 Feb 2022 10:44:53 -0800 (PST) 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 218Gc5su007554; Tue, 8 Feb 2022 18:44:23 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=CirLBKQAk+kqw/rFQuK3wh8l0X95JKJGfm1ou7/quDY=; b=qCPTtc/GjLah0fGNzPV1zbDX6X71em6bnpb8vjETJpWtFXIRLbVb6npcR7iReLXkgN1H o4KSolmrjyz+nRx0npYhmW1f7cikzKGPn/nI/qk0VfMXjqVB9q/1K/TMDJw1qC+oq2Zd o1tj9pPI5chO+I+nwGFrZxQT77clLVP2Vv9slkTpPT7qZwKRzyINDEJxy2/UE0e4+ng/ H9PARHapOvBGtzRjLyEJN8L/SlIxHXVrDFwUOP3/fNNBdUOeTxluI1nT/msREZ11UG7I e91gECNG3R2ueQyENs3jyTBDP/NdUGsGJkeNbLvIt7yGs/02bRyhyRKqOi/XLW7DEaAe 6w== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3e368tunk2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:22 +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 218IZYnX159440; Tue, 8 Feb 2022 18:44:18 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2043.outbound.protection.outlook.com [104.47.56.43]) by aserp3020.oracle.com with ESMTP id 3e1h26thwd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L04uH9tKSIW9X0p6og1gegugxYfQmjXgtrT5CZdQ+I08odM4tH3CucjNHp2XXeNVt1uMib/jwRQgfF11YzsIEF8vp4GGZ4P7CGSq/NWUOm9sUJqQznqQZpGJKLDd87dfJQsqrekNMlFVT2SkbvaF5XypOvTaYtpZhWg2maQ3/LCuX8oHtox6Grbrzg3MFs/4Pm/yp/usWTH2Wh4qsvslZzM5vwRt8Bov/bVz4DKvysphs+oGadR5YH+4F60B55X8pNjhljROTAyOlXDn2pFtHPIB+ghqhfnJhr+lvFli9Cla9sntZ55dc7Wtr7ITXdWkNUdsgxMwRSGns49GMkHORw== 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=CirLBKQAk+kqw/rFQuK3wh8l0X95JKJGfm1ou7/quDY=; b=mxghW6EWJ0MkGuHP87Fb8dY3iuYYQugsc5D0BD7CEVWAny0LoM9qlkikNpZLEL9Bz8H0KoyNAdtLGThVMdgKMYIkkaiWN0ka50QQcyQyIkGQjGBnZHcwOSsxu5aQYipn9rpsmokWMT9gAlpLE9HYZGyO4fFrgZaMXP472I30GI1N2ZXUVH+9Cg4FzTQdDY8PMYnt344wXKaIPvDzyUVo59iyyVzZHHeN2lIVcaF36E3jWuuvXCCaFOHk/dIXkbJFl2teDcYHlkKB/xEB+vbvsUsFGcLGdKXoHIftyZDKnZCW8SqzOE1268NmwToOlBErZylOljAQGr+04WcW3db2hg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=CirLBKQAk+kqw/rFQuK3wh8l0X95JKJGfm1ou7/quDY=; b=pQ9xIFTkVdvnOh+FdUXJ3y+0xu7EUxFTxZHHJot7kqgYCoBB/C5hCxmT2ZZ6wojhvm+cadSFIHy6YAtktiZmIATacRpuHkXLHv1rksRfvWl/o0QeXhDesoIuCr9Mc+8RfQnLEBE3qIfrt3wIMMf8yfMDVxfnrl7EqewjvDTXb6c= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5781.namprd10.prod.outlook.com (2603:10b6:806:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Tue, 8 Feb 2022 18:44:16 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:15 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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 v8 4/6] kallsyms: introduce sections needed to map symbols to built-in modules Date: Tue, 8 Feb 2022 18:43:07 +0000 Message-Id: <20220208184309.148192-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: ea166568-82bb-46ac-9044-08d9eb3301e5 X-MS-TrafficTypeDiagnostic: SA1PR10MB5781:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1923; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EDX7zK//pOpXjKar0clQG4ALKk93B3GkiXUYGz2VWqtABv7JJ2fZK2llai724cJmgXoxEvS8maMkKZNzNB+5QzUS4FqxPbOpyxBt8TiipLUhQlWP3Fg0uLKR7VSBoyEve8xoF9YmElBK+Xk5g6Rab7E82MpNrEh38OQ+EZD5tGq3eApyDNupUB0n7DFk0Ztvku1EVsyxkbjfjpZY/b88RA+KkLhawi7+Ca8Spae2njKM4rICQx5u9wYylmJfpllTMatvj79+ZYd/Fik+a86utwFUS6hwqhxOq0kydKTJDstasQIyyb8Y0LlQiEkP5WErc9bWkgcyJ93agU+WmF9tD125K0TCW1FSYvBSlHIQL06NPj+EsLK8JGPpNceVDifepgxBwWIq3TWjQGq1hSSacFlvedIWpu6K4FE+bdmNE3E/FkeWUcsfMKrKpYmyvkcMRqwMxofmIxPn9lqUi44LHOZptZayenaqnsmiTpAOdvMBWiqdVyxJ1sh1sMBn5QZSj1rJPdmeQoRgaGC0lnBjun384Iltb1aey85Lq6058W5/fl9wJInRrqMFc9ceCksKGkxI1nf5UWyVSX4uOo2lua5A7nvA94CS40NaDUrq/3YrfSxE7CzdxXNgoqwvr2BM7dKubbCqcb5D21dMkkV3Yg== 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:(13230001)(366004)(83380400001)(107886003)(86362001)(6486002)(36756003)(52116002)(5660300002)(6512007)(30864003)(1076003)(508600001)(6506007)(7416002)(186003)(6666004)(8676002)(44832011)(66556008)(316002)(2906002)(8936002)(66476007)(4326008)(2616005)(38100700002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ootrgJI0Ou9l8EbbkalnpvwBMiL7OFTHBUERh2wQc1jWVMjKqRxBWtI+B9KEzXGVw/BKE+youJ3DDHETVspGmRd70YKjlX9JJ3IFIczuo++HjJ/3z1wTCIZrkwDcwLlVdQpjZSltKH9a/WZxJHlWWEhgUIdTX1j1SOzEazgRw9hQsQUQcNBilk7HCueocrjKymhj7BONYMDzDmYndfcPixVKTBgrLwwkQj10qsszsTjCYNv3u1b0EcyTfZeeaXEw7eb8mPHF+VLuXwK3jcJ1M/ZIs4JFAaHbZIG5bMkgPp9VryVOQYak+NngEX95nKr2VuYE5xgJuevh8rzo4jhLXBTNlbsyPhdN/E5kmesulLXwAtLcVNUmBO3nG7vHZq7ogOhOUtFb+eUO5w2+W4CEAw+mrP5BTpSrRRtMUlziP4f72M1oN/AvRzVc7hpyXtHNxCOCrhNgHi+FA/34ZvgD37c6hkzXIk0QKoAA6PVEsOCqCBSHuE5KiBaM8AOe1LKg8gqJ2k0CZW0gU95oiq9jNuQoR5Lmm4mNM3+Mt65OZ43xoWUUYtPba54pxa/du1qiCGb83o1KVdXPn4hSEJfOwElowmaXHHf5I10uMZLMh0330WA5HdxmZ77ASEilnM1wrcJT7KKeGzFSyz1EnmSKKlNneCXfSKKYfcILsZVRb0DQJGZE8VVNaMw4lZgqw07/GTlItAhnlmKF/twkVmfCKvJECLytBqr7Pityqa8q9P/pnntjpQMVdSIfzNgHUJboHyNO7ah+UScpWCRLFwHf3Tml0OyrubEYCtpcdlbeZKZaBO7rJcn8ppwBMNZkwoCtX5ib4rUjyANNpj+Ltl1WJjnr1F/2tOxW0RyoiHoIba33XsLkgWC1Po3RxI7AGnOy6Kk936pxeXzKh5lkAV14eNvulhw94wvtWu/K1/lJEvjXqEzBMNs4J5wKto8l3HVgqytBtwr623ORdDrnd2qv34aHAqG9DzMibFJ92OhnZPZBfYw8VYsRyMITc6M+MK0O2lA7u2soFSyPDWBrSlFQgHkSMmpIC/570NYzUeLTF8ehEyUR06zT02FlOkNLz6S163mxuZAtzlJ/bvZGtKvXPqn1ivBTgGqSCGsOxnKOoAn9zLL6Zs2U0owhzgFyCZYZ8E1DL7RmfoZjWYNJQjdrFe6kAuemm6aBzfw9lBL1LXEnGRd+tHcmlRuHBERo9Eh6l4PaWI/3oAdIlFHn5+aSSVCq5Or0n/fkzsuqbZWw6QTvjfVd7h6UID6DrcCfDdnezP/OMWbA0gpMaw9K/w2VNaGCnITAhYRyIHrZ2PPUprKKnTuWfoLPGyggZLOM54brrBZFHRPQ2WIZjJbVzouQ7ibyEKJIvde7YL4pKuBzLbxDxouJvWbOV9wO3Dr/zw/meooDBqA6YBJxahD9XIyxVqWDuvn1ABMueBIKkVsJCmtMnGBkVW5qkPw38Z3/Hrla7jcr4EYt1mmwY8rr/MZMSpW4IurnF15rKzlaPR363eqvCXJ/YDL2GOw6/iYiYlkOSCvU+hnkUI9sDsfLvLvnB3S5stXU2MLO3M2n8nu5yrFnSeGHmE556MRdvYznQuBBe+qsp+xtmyqJw9YQfidYbURmgSw2Mg1pH9OgAPC4kYt5MzNOODhl8n/l9RE+m0FN X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ea166568-82bb-46ac-9044-08d9eb3301e5 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:15.8955 (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: udsjD8THGRaiXlSVo7i05gjNdmnhqjSQecgU3dE2Gk/kdMegzEqeftiJ0Fjc0AIRx1YISR2yZKyM19FHwYlkXA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5781 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 bulkscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-ORIG-GUID: 5tGzvZ0u-74UbeNCZC4idTwX_QkD7yLj X-Proofpoint-GUID: 5tGzvZ0u-74UbeNCZC4idTwX_QkD7yLj Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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 Reviewed-by: Kris Van Hees --- 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 5e823fe8390f..b719244cb571 100644 --- a/Makefile +++ b/Makefile @@ -1151,7 +1151,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 e9119bf54b1f..e1ca3d70cb1c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1530,6 +1530,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 ce5aa9030b74..c5cc4ac3d660 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -29,6 +29,12 @@ ifdef CONFIG_BUILDTIME_MCOUNT_SORT HOSTCFLAGS_sorttable.o += -DMCOUNT_SORT_ENABLED 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 Tue Feb 8 18:43:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739215 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 3DA91C433EF for ; Tue, 8 Feb 2022 18:45:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385360AbiBHSpH (ORCPT ); Tue, 8 Feb 2022 13:45:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385349AbiBHSpB (ORCPT ); Tue, 8 Feb 2022 13:45:01 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BF91C03FEE0; Tue, 8 Feb 2022 10:44:51 -0800 (PST) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 218ILqQQ020213; Tue, 8 Feb 2022 18:44:24 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=EJD1NXkyPpVL2e6EUCJnMp0hne6/ZXEXfHgkebDyDt8=; b=vewfROWgWbldW7bfRzfJ6sczC8YwiiHI4dZtc++rDSKwaTw2lvFSCb2dEz547ROg+TPn c+efZ8hbxGzZsA1fhEqLfvqhomXeA89MXRav5DUq0oJdhI5sadHHYPBVZSwlBMu4GNTb bCYcz4rEGWQL6y7IodJtTrXaLoezaCALRitg494LSNcvx14bQfBEBT+2S46O8Spub4rs ekxx+/wVwMggyHdGDZYa4HwldKkvxSd0+ENVwZnU4y9zdgXZpCr188kfgZ9ehjQpVEr9 pTf2s/Mv/OSzDEDEoZitP+3yO1quLLh+T9u/O27ODBXB3FA/aSr8i46vCKK+2uh8N+Cq yQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3e366wus0y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:23 +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 218Ia5NH082722; Tue, 8 Feb 2022 18:44:22 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2048.outbound.protection.outlook.com [104.47.73.48]) by aserp3030.oracle.com with ESMTP id 3e1f9fuvna-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lx7YDPoCu0+I/YNIcLxjiyXRR8LXl2/Dn/Xqo601XsV8ouytUNlS3D1uiJqtFzFM4t62flLJDbkXO5adC1Jo7nQZZHSreJV9WrEqyNVQ71FFSRYdaBcIzZ3zVBSj6HvKuD++W7m83r8lKrSVHZn03dqKQsIOIfUhIo5/sUikOWmZc02q0vGUmfotSsQRckUta42rtIwIm/on0wfHvVv+vEtuAVL93PwC4BjeohbHHhfcmlz5QneK2TQLhTk33735qfMkJQ6kJlV4w02v4pJDrr3QD54FBB1XfxZQNzMSVMuFMRwV1HP8f2jx02B4zZaV5ZeCros7cG351rBdkO8UUw== 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=EJD1NXkyPpVL2e6EUCJnMp0hne6/ZXEXfHgkebDyDt8=; b=CtkoBsoVv9b/mAwyD1EONLMAshVky9Pf1jvV4NLawE6a5OTNeEC/TslUqtCYqV6uOyfHdY7iDA6cPHrEEC6RRGm7Cbzlj6yJFJK3T9CDdGsZbsyCXBFASTgGiNQYovnBRSFuHSBe2jwgwdeEStcMX3KQBZFViK+W5QSjqJfr0mW6tlbnuANPpaqxa48esGd4oaR7y/0ktLlT12EG8d27gGwvemLVkuF6DLAlZGOE8ZFw70Ny3GQspfC+HFgUSdkLbXx4q3U7pG43f0dgUbQWYd0DiTThHQFvT2uzOyAPF5vrjXcqj/0YkZQH5HWxCqd7eXk8+UCU/X0RFrgUbyrlNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=EJD1NXkyPpVL2e6EUCJnMp0hne6/ZXEXfHgkebDyDt8=; b=xa2Lol9j9PzQ+rqC3MhDmetZym+G7RkD8FCc3L/iWox4FU96Udj7QebBBTJZ8PFGqa12L3UWrTZEOXsBqsaKLgulCl57BE6u7RakT/9hpocuH/nMrappiLBRpQhpLxfZJk2/oW1qRBJ1DzHbEEgC50Se5sewdhkszdBuXODjACY= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by DM6PR10MB3114.namprd10.prod.outlook.com (2603:10b6:5:1a1::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.12; Tue, 8 Feb 2022 18:44:19 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:19 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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 v8 5/6] kallsyms: optimize .kallsyms_modules* Date: Tue, 8 Feb 2022 18:43:08 +0000 Message-Id: <20220208184309.148192-6-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: 10ef29cc-889d-477e-6eb6-08d9eb3303dc X-MS-TrafficTypeDiagnostic: DM6PR10MB3114:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3173; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /dO46U8shIzcEB+mbxdIwmNIYb6Z9M0inBqJ/uQf/LSa7J4sg9X3yvPl0yDQ32CbeYLuA7zuesfHgSclsUUlnkynElrk3AnQjeKLV7o6etbLhwB1Jktg2bvcHydZwGrAfJtMhihHXD2ZgAhYOnhyt5z2u9Z1JAYZv4LrEmpeNsabNnjBTFEWwEE2edoeb5FdYz9Hy06hQ4ZfMZ+m0QbR4l1IpXOaqZ4TewYEnKXc+xprFfHWmZGVLVoNAQdXywCVxhXQ+GOKIqFwYWBQ9SpA3JKrJ4Wa/kPG0DAwXx9b2FNx2MosfsjcaaR3YRVyP1jecFFUPEfJjerUlirI8jX8ze/UB9RJ4i+tUCioML/pcmHF2cLsDPN+tYAQPtJZo6Bnhn6XRmh030aH7CEVfy11RGw2mOPbpKXQBfU2Fi5tVCaz4wtmhVajH7Fs+zeprOg4jiXWOG/lwVSM3Q6/tlT9xjltIflJDbDM+aD6nkdwip1Id0k8UnIcI4TOYKjiPYCGUr6pbeNLY2FpbWTgEqZCJg9NmtJFOr1bje6AzzF+VqhraKxiZuOjFuSFqGLtpIh4e83cwKTnGre280SfnZVSKVbPXW3IA5PHJJ560hBbPqtmC9QncPQBRcMF5jmJWnTs7uFfmqS7HrVw7JTlgwb8JHfCpLgMIbs//rZtoINiAhKDTh8G9tg4I8o8thwzFohHX2370SE/O/s/UR14hUCNsg== 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:(13230001)(366004)(6486002)(508600001)(66556008)(86362001)(316002)(2616005)(6506007)(38100700002)(5660300002)(6512007)(44832011)(52116002)(1076003)(4326008)(8676002)(6666004)(83380400001)(30864003)(107886003)(2906002)(186003)(7416002)(66946007)(8936002)(66476007)(36756003)(142923001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ibdEeo0bnNT/BsXUVvhP+VTRvFa8uiBCYqgOffvzpdlfR/aDApUy2t3bOKekkCGAzn6IQE5JQBqQEfv/M+A+1wPrSKFcDNth43dxY9oz0wx3aHZcELRUIT8TsecgDwO4aTk3gALcINwTHcpihWj6uhPy5sPsxmVmVH8YmvJDHfgqP8+TnaofRXW5JspKW5tmVG06cB8GAoBOrxTjYEmZ25HSAP8j5xnEhBG5kBOYaSLd+MtD2xAcVR2JTqD3NCCVUac6/S8g+od5oNIr64IC+8v++1uAxYt4pU1UhPtvxgzX+ZGT/nSCcwFXppjlJa/1r34aGwYhHjWd13Sv7IvXaf3WbYHfrYM1bxMl9suYPjvdF0sNDJBJTO0kYdurT03GsjRso82SlGzXfG5biosY/QcD/AaQdITAwBaOlqxFbX6E5hhBOdKPM1OJeE96fIhSVM++wLZ2BSQBoUucqBobvoCmUlXF7DtvhwmrAqgq2VWFOzoKx9wshNs25mGQoSxDtbdxTxUowiENZ4oiVi7FIXXxTdYhSi7Q8BALhLU05jY7pc7ccUdv302svPqdMksMWTqEIyOsbyk1JuxOLvKIhtVVRu2Cm5o7Eex4dSxZPeAArF4odKT4whmw6VC8x5/Jciu5AIOn22jQy5qZrlzrXjeG0drhrTKPrKTQwrNjTEGg+EqpVKeuX50b3ZVdYIbxIjy1vMTpE36wWpA5McA1/hK3uqETheZWk3oE8vqSeV5uP9UYvVRQrEU4no5uMJ6kyv3cfbDTgQ2hwa8uqXFPfQuSUZjU7WNXrTWi9CUlkUbKW8DKWf1uBzQ2LS+ZKzB3pxzdnIJUzOQnu0m4jXEEIwZ3+MNJou3mOd3Nt/YjJaC4kuImd6Q8LbrETpQgdzqUT+VQ29/qKZvZA0AKGkjc0KkwEdcoYRmzGwbpYu1i1yV+2feIoGXex4kayNulfP1qyfz7xYMHTIHEKcIqufeQIGIblTOr6AsN2tQ5BRWCimA8yaKS5cAVdfgiZaFcjVD5w7N7vSS74NahFPbSGwu/ZI6L6mRG6wqIdhlfEbQaJqQIip2yfLIRJ/lmrEs7s3Wd6bwIrshAiYzheHw55rFLlLUNdJ/g9AOaWV87fG2tKnJLJmge/ojxXlXJzWQQK5uNc/mDnGUjuGK0c3NFwrxIfJu8KSwiY7NCwacAmpVwxhz/HP5lwi88/4sXZS63IlHVpRllpuS/xofsBtz+dMEstozeW/JbdAGZKHwNXwFGMfUtDT+oiVbLnu47AF8glY5hthg8uqemlLT3BxhHNt9cWCINS0piRgYenE9eBjwQ70o5I6mFkW8FvhwZBgAIEqlu47vQ57Pwo7mG+YfIj6Uq2gRTHluNg1+LgW2XGbd6qND8Ld4ZZR8H6W/2fHFog6cZgFQgFQLWe7U4vPXIFW6SIlvAScti8Y2jmxvRaeBBR+doqCOyOzzEMVpZOdJedeqZzpjBpa1YGMR1hbRXruiSHIWg+HoOAXQd89t/mNYsx3AX5Xrpevizt2LiYcnzPW8MOQEQT0DBcEzyCmpiv/lzNde18ju5plKepkV7mIi/n0qCQtqUFL4vgmFMkw7rrrU8m4O0CvVygoU5ESYTkstyCp53wzCaeDX0sntPamLmF201y0zQ6vt2l2dtD6BIzRwu X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 10ef29cc-889d-477e-6eb6-08d9eb3303dc X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:19.1693 (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: 3+gC7i0Kl3Ft2gCqbCJUmfsOmmTb9439tRfaQXnd4okgXVczx841syKlgMS2frelPiwm5VRgLsNKGyTLpm0A6A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3114 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-GUID: apyUiTFpsP5UN8DOcHHDYKDWc4vlbFe1 X-Proofpoint-ORIG-GUID: apyUiTFpsP5UN8DOcHHDYKDWc4vlbFe1 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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, vmlinux grew by 0.17% (10824 bytes), and the compressed vmlinux only grew by 0.08% (7552 bytes): though this is very configuration-dependent, it seems likely to scale roughly with the kernel as a whole. Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- 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 Tue Feb 8 18:43:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12739217 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 914B7C43219 for ; Tue, 8 Feb 2022 18:45:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385312AbiBHSpJ (ORCPT ); Tue, 8 Feb 2022 13:45:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385316AbiBHSpH (ORCPT ); Tue, 8 Feb 2022 13:45:07 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A01BDC061579; Tue, 8 Feb 2022 10:45:05 -0800 (PST) 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 218Gc5sx007554; Tue, 8 Feb 2022 18:44:37 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=zl9h5yFVb16A6YpCHkr2/0ZLlLOzGaX9YEC8I1bEPGU=; b=sl7BzRHO3E1FZ7EJYQ7rPXLFYUO61WCjdLoAUbwkoNXV4exNkoRwV/J0se7Lp0ZVpuOs dM4mtBP0fZ0kvg5Aa+1qhd96wTavp0zFpruI2cObvApUnukIt7EQLLINaoCEgUTM4MjJ iImY7n3sz7Cj56Tvvt5LiJVbzF1Obfwx5i4LZoaEn/aZ3dEhugBq21xDDRhqvMEz6Mz6 pg/qQI3Sub4RxuShpv7cTRmQ2PgUvLHBEbY/lSmkoTzzv3yG37vfykxeNRSdAmYKlObi q/Ep8SQZkxXUmDjDJzHctJgoXWN43IN7mjjSxMRPyHGwX+40P55+sVggQaQvD7AMUsIo uA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3e368tunkd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:36 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 218IZnee037383; Tue, 8 Feb 2022 18:44:25 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2046.outbound.protection.outlook.com [104.47.56.46]) by userp3030.oracle.com with ESMTP id 3e1ec0w7q1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 08 Feb 2022 18:44:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FDh3X4wLz9QDZAM6lCaRg5kPVQjPgI5hE+X3SssM48RpSsnQL3l6hagWbeWX/WYwcWNP59toVWPIe6Hizlv8YXNJg7YtC42bjDYMxyjs2gBTLMxDQ6xNC/IBSDWQGcDF1sXBFd+fFZiyIO7h/HtYvvHYuO1MOJTr5KXHXwAUYb2wjQ5FpzfzbVLcsRHntFXPOYJrOtMja7UMzxafty0iDcYRymqSRjS8bj+BC6paGaFBRvG41tub7NAcGUOIpncK4eSYtAYlsC0GZI+ELZJQb9p0YH9ubdQwPSn4EwEWbQQt62MIJiaDYs2DuTXBwIR8X+bgWEm7vL813N3/zHwPzQ== 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=zl9h5yFVb16A6YpCHkr2/0ZLlLOzGaX9YEC8I1bEPGU=; b=oR/n/+NG9LqQysH/zHe+85qErmYdtOP3d32QVs0LEYSHeYHr8z8uc7s7o8BkW4JXar7Otu7iJaw1eZRGO9MKdJ2y01616FE2DE97rMrSi8roFgys1fuYgrJWizmkhw8y14xq8TB14y7anvwP0k18Txn/ftcGIXjUDgXYZV56IHe+6FL9pgbe6brfpWg0kN5qEy/Af7Lb85hpHb6zguIceeMJ21C6nl2CdlO/hyVhqj4IfdiIoXrhUffKml3vWo+0eUhyUzjdILBJKpUYDB1MyBTuTjBk064qapzcP/yfRa+v3zV3/XlE8YOjnOr5rn61U4atxR1CC821oW1gNyOG0g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; 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=zl9h5yFVb16A6YpCHkr2/0ZLlLOzGaX9YEC8I1bEPGU=; b=sWb+biYMNkdksP2Q0Ctnhfd+CVJxW62zuYJkn6oumH/pPASy/g3FmPnVSpg8B/Vjtg51bt97mrCB+C+LmpBGDnEHxhDt+ukT2loZRmT5dEVkc31Tr3RmNTllADszkPHyxxt+MYtazjkxZpHnzvO2mP7RkwUjZEmln1NBOaltdEU= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5781.namprd10.prod.outlook.com (2603:10b6:806:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Tue, 8 Feb 2022 18:44:22 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::644e:5828:a72c:c1de%3]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 18:44:22 +0000 From: Nick Alcock To: mcgrof@kernel.org, masahiroy@kernel.org Cc: jolsa@kernel.org, rostedt@goodmis.org, bas@baslab.org, tglozar@gmail.com, Ast-x64@protonmail.com, viktor.malik@gmail.com, dxu@dxuuu.xyz, acme@kernel.org, adrian.hunter@intel.com, ak@linux.intel.com, irogers@google.com, linux-kbuild@vger.kernel.org, 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 v8 6/6] kallsyms: add /proc/kallmodsyms Date: Tue, 8 Feb 2022 18:43:09 +0000 Message-Id: <20220208184309.148192-7-nick.alcock@oracle.com> X-Mailer: git-send-email 2.35.0.260.gb82b153193.dirty In-Reply-To: <20220208184309.148192-1-nick.alcock@oracle.com> References: <20220208184309.148192-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P123CA0088.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::21) 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: 08ee111a-7561-42e4-ff61-08d9eb3305c0 X-MS-TrafficTypeDiagnostic: SA1PR10MB5781: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: KTU94OIXTuDk0Sqv+MOJoVv4HpArkaVyXfNnG+Bq0SQakJFVe1ARMxqiv1uMK7aUpM2EdI45Ek0tm3mA0g+/sBWpOb1TxuvTA8lXjhtCh/B4uviqHX8yvGcWU3zL6ZVCbKSfH8z5Kuxbmlg7KI8jLvR3BRjq2Z206k89CO0sk7ZHTvrUBry6CMNfDRzRhatPcH7wm6eB4nLe8VwcmHA7GGlbETHiTigmTm4XflmvrtOV+Q9KOFu3vo6DcyZ6dgE8rgjXmdeId8HUA/8Q929xFrYcakOG+h2cjIuhTadPw7VKwy09Q58zEBYXU2bBSgQPPWD5RtSrC/UNpVKUktETInr9exfq+E0OXbn0N4OgyU81qm0s1HVJBhAN+vnvElJraOk0LjanzDtuB/n2BW3patb9webLmZlGGXd1KS0IetyWhpzzrtIcWAHbTnclj0VHWjmkVVxPuVFzuK9bve6nNOnKcqBpQsLhrISLwHUofjov2Jtbu4jd8wEtClQsUuplUdANJk9/tFQCAlgdjKNHTnl90fk6vwROhQklXpHS8Yc8O1TGVy+vlGWQy/SxdT21yZVtFwonFPOW1jBIKxn5JVLQIgivdX98Y88sKEMO4jii1wVamny2x5Fl6OMsBwsDj0RqSjyOLkVLLcji0WVNDg== 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:(13230001)(366004)(83380400001)(107886003)(86362001)(6486002)(36756003)(52116002)(5660300002)(6512007)(30864003)(1076003)(508600001)(6506007)(7416002)(186003)(6666004)(8676002)(44832011)(66556008)(316002)(2906002)(8936002)(66476007)(4326008)(2616005)(38100700002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bNjbeyDPdaG+XIqhaaulZxE9JFWuOkpoTreftO9bZlWLffxXo+oeg0pCKMOAj/DZ9doe2o+5EyD5qcYRQVwSn8V7mfJ+G7yfQpsrVPVLy+mh6mRGN2HEW6XP48xl70EFp7CurGyMOHov32aJwYTVAhUFUrUgPGFiwl7aU7W+bdrFGho0QsgfvcOEZ2awPtC/gErKIWw/qrTvTDnnEjlf3JUvrq+uN36w4LP6rFIBbTkHp27iHh8wME0NMYhfuwTK1gOXZ97/h9DohdKr6yt+o0IDbJmsQ+PJCchbfv9xg53zmIdVB43r2fvky9fcGTb1EtxtJrpF3QRXEqqZGckV3h3K2k/SFSKphKJ46dT0WmVIcyYoNr6nJh1JdYCl1PPbKv0dVex/XNmj+nD5wfsg5IWCye5MN6ACVaRG3+5+jJl2F7KSwlucv/D56z44kqLS5d5A7PWpO3U+REPgb/BdchohV1rSps/Q/9jNmr/uHY+DaAZxbc6LSpl1MtKMeG6IpWk8k5R/BAiypw898eAS2bSNSuF+/Cv0zPEbIPlWl4PpbDvvB7+xAX9HQaGWHalKv1klPtDqTrJYjnlbTz53LmGRDj6Ll16sH05MmBW4/wPF38wZoTwN6Zs+3sGq2qe0BsIWPqq0FYErEQVToXGTLtWGVPVvEzYZvvhXkaYTuavf1h/sSLWAOgluFj7kXzX6YUWpb8/FqWylPIA5RelqwqUYVXK46kbVlCaVsfGusHjAOyu31iYhR/TNMF27NdHySvi3A6mmuIgHPBV5aPYcLFdcF8TGERCwSFwjfRxjF+LZOziVzzfrpo9l4xIdaLBAWE6GhED9+072xEHENgPXlsFWjEXaDf+ZySt/CUVC5fFl+CVH+fL6fqFZ083Ng6JpXiRtp0rpZTzKE7DKGeyTdaaPESoxlfInUUJJ5TdxFVaZ+hzUsH8MPHz6DHoT2R8H/nsFJHPLxhF6ehl6xSQNyRjdwMlvVsTkDF9JwYms+riZ+bEjMwAN1kV4wzs6Lrq0ZC9brJYglfsFEDIt8RLY2IAHIvfrhmHXme8Q65EohPK35XHHKusheVzxvGj+XkewmkZA8m66xeELk8OfWF64te2upatnuzaZxR3yP8R3mOIiuPxXAKFX0jRdZMGI9CEZNvlI/voJcySs6+yHk07oYXegOIdJr1oUAK765+bPiBCbBtiy5yqcBfBcO53tddSDgIlGQPwrZlmhW4ozRRhtbn97YddCo5/Y4o2cN6gqJxeY9vUysFTkdOhQAc63dzyUcFHaEAdD6kjd1UR6SpQH2/K0W555hY5tcim3m7PKR3VMacmN3MxdUHgRgWcXusX/Yq/hi70R+VIrsu/KyX7stMMJ69Ktok6MYifFCDe/+VQ451PS49qv5NPT38YCNhZ+W/hsfiWzg0QbhpIrLnK07DRzfQ+2BMMKJtcqEX4FQ2WbmOaZ/h6Kk331/2EbBai6k8OQFdnCsZac+MPI8hW/jXjCN0OzZKmT8SGYQWaP9zQDw8Kf1Eug2wCFz87DaQ0sEaZwS3C6dzq7AGZWhbZ08piuUTfe7XKgKzB0MvOrGSLpvZ6JORhdwprIWSF4e4hMDPqTvDJSoHOzxyGjsmBdAHWikBIhPrPp1YH3jkytiYKOvZh30WWBM3Sfm3ybYx/4 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 08ee111a-7561-42e4-ff61-08d9eb3305c0 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 18:44:22.3279 (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: bL7hXx7i8aoidHI/MvoM8sjeh8YrMWwOU7Owjv4I0JhrpLvytJXpg5zHUPZfO7m3JSexMNtSqPvNtj+B1Us+jw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5781 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10252 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202080109 X-Proofpoint-ORIG-GUID: rdLo2xVeQ2yXh-q3l9LYzyahEODQNAuy X-Proofpoint-GUID: rdLo2xVeQ2yXh-q3l9LYzyahEODQNAuy Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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.) We explicitly filter out displaying modules for non-text symbols and section start/end symbols, because the former appear in distinct ranges which are not covered by the linker-generated ranges and the latter appear as text symbols and can share an address with built-in module symbols, but are not part of those modules. 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 Reviewed-by: Kris Van Hees --- kernel/kallsyms.c | 245 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 230 insertions(+), 15 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 951c93216fc4..31469565967e 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) { @@ -309,6 +338,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. */ @@ -580,6 +657,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; }; @@ -610,6 +689,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; @@ -629,6 +710,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; @@ -643,6 +726,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); @@ -663,23 +747,56 @@ 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, and iff this is a text (or weak) + * symbol. 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 && + (iter->type == 't' || iter->type == 'T' || + iter->type == 'w' || iter->type == 'W') && + 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; } @@ -725,7 +842,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) @@ -735,7 +852,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; @@ -745,14 +862,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; } @@ -761,7 +878,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; @@ -772,23 +889,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, @@ -796,6 +957,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 @@ -831,7 +1021,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 @@ -839,7 +1030,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); @@ -852,6 +1043,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) { @@ -862,7 +1065,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. */ @@ -879,9 +1082,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);