From patchwork Tue Aug 3 17:16:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416921 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C82FC4320A for ; Tue, 3 Aug 2021 17:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02C3960F41 for ; Tue, 3 Aug 2021 17:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237850AbhHCRQq (ORCPT ); Tue, 3 Aug 2021 13:16:46 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:35158 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237745AbhHCRQl (ORCPT ); Tue, 3 Aug 2021 13:16:41 -0400 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173HFuR5028928; Tue, 3 Aug 2021 17:16:27 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=BuxiClIYhvq8cOtVJlzo2XffYl3YRqTBRUKWLrFmFI0=; b=wbTwLV+a7c5salETSZh78xW3vCaEdfOrndVE/Wg+Hn9zxTzCgF7KUSUUR4HL1ipleaf1 uGunA2WTS/CijXLREqIMbB6dHp5beKCBbl7w0VPwb2/sCjpKkoqBrKyVGJDCWMawpRPi fFxRjM+FSQo/yrMqo8AuPxLtZWdXV2VmWY7rOu1RhWSxXX3JZqiJLqZwj4hGinP5wtqB G2uMzPt6mk78HxVS8TqzXEhvgZzWzUCR5BfpF7dEbHDDjYhu1pPdSIpqQDmXOBk4HlFf Xi9d2bgUrD1UA/FYU8rH+rN67qv98LliHAE34ePcurKQI+97ON7Bv8DahJP9ESGHMIxe OQ== 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-2020-01-29; bh=BuxiClIYhvq8cOtVJlzo2XffYl3YRqTBRUKWLrFmFI0=; b=mmzzxXFGOgeJikgIAH62+h9+B0KS2qxAYD30IstLt10yQ0bAyDlx9loXf17M3aY3Rmm9 gyOeaD/DoE7E8r1hkW5UfBd60ac5huV5H1Yo0e7FnRlRwwvNsynFqB7lffgSvM/Tko+I FyMxJoLnrMZdfd2HNgeMbay0OPFZ8t4phOqCjMqc7xR1x5zGv0uxYujxJmbOI8aTweGn 67WTxgKMC1wY34TnmKuVOQGXV7+mdVpmOqt2+6O84GBV4yExriCduyoGfwdEap+4ue+f ObgFENEYeQtg8d5I4XsVND/1n8JV+g+UBZ12jV8aXdHosrbQZpE6Ns8MolhQD3eMP0iB cA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3a70pjspgx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:27 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HFsPZ183317; Tue, 3 Aug 2021 17:16:26 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2172.outbound.protection.outlook.com [104.47.56.172]) by aserp3020.oracle.com with ESMTP id 3a4xb73fj9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bxBfHK8+PxXRHt540n2uDNTAmVYEIElFpy93P+SujW6px8GGTW1nZtxkOzhLkaXGUNMn5XZ9YpMddZQZTH1ljdgYyECpJbWBhjQDmxm2Kss77ptmCLWtQtxNEmPUpPZRDNdMsCX0YLWaO84MTVF39i1sBiVY+h4Ja84Ng22uCpbqXTKWhIGfjnTUsic/7U2ukyV5ME9ZhhWO5fsveJg9kHYDSp42Bq3lXvnKJBq8yXW9f8yRaRdBuKAVFFTKxcQ4y9KELlNxWrxyBffphobL0pOOjrfIcIa8XtvdryCOMi8ggMBU9HgD+PMqcqI5EowyUMiTmI9r4sG/zIdVS/15LA== 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-SenderADCheck; bh=BuxiClIYhvq8cOtVJlzo2XffYl3YRqTBRUKWLrFmFI0=; b=U41DDScs1nTX4DMeZ8iWC3iNVctXgOUlrcn/LpkVZ73O2yLdDXC1+tOEPsTPgqulmgDU0g2v9sfuNCcM8ZVa1sn6dN64ezsOj104h2pwCRTZ/bmVDuq7TRuNV4MuP13fZhcQMlc4bofrBphIw92D9TvCGmrm2EFAIq777n0+rApFn5naL4ukm8QQNkEFNxp96faqJHnGf08HaaC1XDSG9yccjycP1L1FvYeElQsZZs5lifh6fnoZ693tkbAoo9FAAnxgbFMFOUusLR9XgengZUhCtjosqfnl7D1G46VDQqruZGEesWnE+qTi0wxupnFlw/Rb7QgSrQ0M5Ihf5j6pcw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BuxiClIYhvq8cOtVJlzo2XffYl3YRqTBRUKWLrFmFI0=; b=GcE0DVH+xeFYSKWmXKY27stxr5EHipIkuub/R5BnVEFVEJ68QaNTMeqtq68WnKVBuDWucdf+37DxswILhWPKT4I0lUw2UXfkoq+JDeuq7zQgiMMxuyE1M+1CvRvhb99nxSPjuGTO1IZy34DYVeWWYzzhsga5ikbzLRj/Ff1WYU0= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:24 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:24 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 1/7] kbuild: bring back tristate.conf Date: Tue, 3 Aug 2021 18:16:02 +0100 Message-Id: <20210803171608.509163-2-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:22 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 89106133-24d7-4d4b-6c37-08d956a26bba X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:102; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XbUEyoQJpv7BxkHwsp0jj84tsJay5Tuh+ijUt2whSUYw0ehu5vS2K4tAVNxfYb0BqGvqi+mK3LrqL6CUEhwpmd4jltGhjyebYmj2/xou1CHVYCwCzd0Mlo7nHFLMVhXvu1tdOMrytu74Jnhho8DEZ82lK+i0Y1MI9tjyps0edVwAdVlhImlEM5JUKzezAmpogWpEUVg1nA0ADG7UfIvxBP6u0IqL+BbWjxvv+X2H1pda1441IW9ehj/H34ThRaijSr45NgKfgp5+J5xu04Cpw2BOSjLgdMk3V18k+CvKTaJTxqjKMdrt8/9vz+Xq1Uc6aIIpAfbX3WAVEJYbY79Nm4zkcbwS/YZBsGClTrx/o25ExUi/3yE/t3mwVdhoe/e+I5BEYGNus/N0KhILTLjj0JA600FtWdL/MTQ+UdEd6Cv8xPVJsKGALa3oLWnlD+3/vlvaNTVJLvpCRLSQcjqBT7zqqjK5TT6gqN3/eJnzzTeJ6FcqWVstD748Jm45x/7h7YzCHGawZpN1dkPRRuPzoIER0ajw5/tpm6aoALdechVqkrIXa+rw8oI3ci4ctJk/AnA3epfbc9LaFWupe3thHEsmnd5rOojPbGHZZq6pphItyDkEycVg86XO3oPgkmqrMus5EWbfRwWYq/eq/8SJLg== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yKWj8skXBngYye+7Pqjts86rJf5Tq6EtnQxBY+WmZMv6CzCiKjVAGk35FepOskI2CX+k89oDTjFoW91CYyzXxvXntHdBDH48z6bQYP4varCz/RVcY1+VV7Uonf/rwenFnr9UaZ1Rf5plTmPDXNB5vWgrPOnbSN7NrLf/Y/28+CE50RQcE1gyC+YiS91pX/uBAOPxCNsOLj6sigwScbs4t2iiPGfnxx+Y4uOP+JG1s9RB+XOPZ2P3kBq3XInBVLDaF3frgn5Oi0lbaUP5JGIwGtAfEQDXbPzdcBEHsQR34A2qsoCrllvmc2wIDQ89ZWELpGUxe3v878q43nejE4uaHQ/b6ciRuSywha8OPK8x8MXkyWIMC4xEMPV3Ww9XM1CXZ9mK0VI5Jq+OLrOJJQEboTroIK4dFrVmzpDB0mJv2foN2ZZc9iEPsfcFkGeREQ9eiaLtmMbhSFZVytXgU587chfhd2U3Wg0FqFx6CIvP2a+OkO7iV6q89xesRrqJxUYDkdjgCPh3NjP4mxJPGTIF+H1grOYjGn3LdguVv89oNvDZ7j7fTNfGV6X8g4KgGL8XUWRFMZx2WBFcM91AOp0HYViLllIVqok3GfW2foMZc1KiF6H32slhg5kAt+YuBIVuAvlPvH/y15Y6ZD6S5P/Qf47yEsxbrCrDP2VNKR81eBUtHSDPMdQY88OJzudOlExypziuuJdlyMc3hhCn0V7BRuNoWs7rpW6BEv50l0YR/JiHXXGQoQQR25DaicNDct9DyiedgWIlKTQdWVvkZSn5Ulo+uwoOVTb8Vcl2/63PkdBvsskkQGtz1adlYVmDId+MHKe+i99eU0F1LG4LPxm2uH9d801HXJim/q5z1y90SXTbpz3X+Y1mcG8qkshZhxY+GxTY7lH0xjdpGVQyBiGIMcE/62Sbx3tqbQvhj8bUjND1RPTPIw90BODqU70OGRAkL+mneSsNzcm6mjNiCvUk5RBjg0HA/r35u0NoWTS4oHuYnkMX//upwgLZ7qneM7OmHvqF3N2pinINZoSMFKmrwxYrRyMw81QX2Z/wuykB515x04C9ujJAzKqHGfkX7Mbi4VSJ7HPkh7mlWW4LQUDuR5oZ2K5BazsO/pr0h6zk4drmEACa3/fvQ5IKLynRpI/NsMmaQ6ioZ/tHNliV4xO8OaSS6Mg+qy61cEz+8mMAmAXOUuXOkCeSJyvHrYD+HRHHXTeqXcUnowteOOiw8EnW0DjFptiSHiHASabISc+Fk2WH/v6I6HgKAVUIfk+Vqyp0/52ihmR/Q4CBst6Q8dxHjmRlLexRvsDuM4M+EXDu/X3GfYduIOc95PlK+rvfJraIu31C6t0Es1/ljuhCoRislg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89106133-24d7-4d4b-6c37-08d956a26bba X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:24.2711 (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: Wzj6VrCtAE1UAm1FQZgL8C8cK9GL9iAWqHj+12yF9L2hOBRkal/sI26MqVybmDHP8OjjvrkLiQWeKn5rpgJj3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 phishscore=0 malwarescore=0 bulkscore=0 spamscore=0 adultscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-GUID: GWjeLpICneemRwufUzHZwT15ySe1p6wn X-Proofpoint-ORIG-GUID: GWjeLpICneemRwufUzHZwT15ySe1p6wn Precedence: bulk List-ID: tristate.conf was dropped because it is not needed to build a modules.builtin (although dropping it introduces a few false positives into modules.builtin support), and doing so avoids one round of recursion through the build tree to build it. But kallmodsyms support requires building a mapping from object file name to built-in module name for all builtin modules: this seems to me impossible to accomplish without parsing all makefiles under the influence of tristate.conf, since the makefiles are the only place this mapping is recorded. So bring it back for this purpose. This partially reverts commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29. Signed-off-by: Nick Alcock --- scripts/kconfig/confdata.c | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index cf72680cd769..8f4f1b373dd6 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -725,6 +725,25 @@ static struct conf_printer header_printer_cb = .print_comment = header_print_comment, }; +/* + * Tristate printer + * + * This printer is used when generating the `include/config/tristate.conf' file. + */ +static void +tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) +{ + + if (sym->type == S_TRISTATE && *value != 'n') + fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value)); +} + +static struct conf_printer tristate_printer_cb = +{ + .print_symbol = tristate_print_symbol, + .print_comment = kconfig_print_comment, +}; + static void conf_write_symbol(FILE *fp, struct symbol *sym, struct conf_printer *printer, void *printer_arg) { @@ -1058,7 +1077,7 @@ int conf_write_autoconf(int overwrite) struct symbol *sym; const char *name; const char *autoconf_name = conf_get_autoconfig_name(); - FILE *out, *out_h; + FILE *out, *tristate, *out_h; int i; if (!overwrite && is_present(autoconf_name)) @@ -1073,6 +1092,13 @@ int conf_write_autoconf(int overwrite) if (!out) return 1; + tristate = fopen(".tmpconfig_tristate", "w"); + if (!tristate) { + fclose(out); + fclose(tristate); + return 1; + } + out_h = fopen(".tmpconfig.h", "w"); if (!out_h) { fclose(out); @@ -1080,6 +1106,7 @@ int conf_write_autoconf(int overwrite) } conf_write_heading(out, &kconfig_printer_cb, NULL); + conf_write_heading(tristate, &tristate_printer_cb, NULL); conf_write_heading(out_h, &header_printer_cb, NULL); for_all_symbols(i, sym) { @@ -1087,11 +1114,13 @@ int conf_write_autoconf(int overwrite) if (!(sym->flags & SYMBOL_WRITE) || !sym->name) continue; - /* write symbols to auto.conf and autoconf.h */ + /* write symbols to auto.conf, tristate and autoconf.h */ conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); + conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1); conf_write_symbol(out_h, sym, &header_printer_cb, NULL); } fclose(out); + fclose(tristate); fclose(out_h); name = getenv("KCONFIG_AUTOHEADER"); @@ -1102,6 +1131,14 @@ int conf_write_autoconf(int overwrite) if (rename(".tmpconfig.h", name)) return 1; + name = getenv("KCONFIG_TRISTATE"); + if (!name) + name = "include/config/tristate.conf"; + if (make_parent_dir(name)) + return 1; + if (rename(".tmpconfig_tristate", name)) + return 1; + if (make_parent_dir(autoconf_name)) return 1; /* From patchwork Tue Aug 3 17:16:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416923 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 763DFC4338F for ; Tue, 3 Aug 2021 17:16:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5772560F9C for ; Tue, 3 Aug 2021 17:16:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237915AbhHCRRD (ORCPT ); Tue, 3 Aug 2021 13:17:03 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:54734 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237920AbhHCRQw (ORCPT ); Tue, 3 Aug 2021 13:16:52 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173H7S7e006798; Tue, 3 Aug 2021 17:16:38 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=T9nNKGNRU6sXJsi/IiNkFPpEPjrixJG72xyFEQj6SMs=; b=fZtd+u9o+fUIUyqLsaMRoric7jKTXRsNmaF4bkC7hOajenvikhqfctMolRsZ9Wrb6WCm rMt5CI7JtyoJPar/f/Dn67/PFiE4A+bmkV5Cd2rm2M1xLfM7m8870DNaJvBvlLg9oAG4 H/CJzwgfiysoVQNl2B0LYt6rcj48vGCBy/qyf2/VJqLw5dtNx0J5CGGXcAoqcB0Y/dpF Ba/M10eJmPWODcx7SIAle3PIS+JM05V+c8ZuG6bixvfzawHn4NYGX5bm1P7kMcdGLBrv WlPYdEVX4HKUcYJuiFAjKC+py6q4WnmZ8d701I4bZx1sI3dLui7z+qziytMP173ncL3D Ag== 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-2020-01-29; bh=T9nNKGNRU6sXJsi/IiNkFPpEPjrixJG72xyFEQj6SMs=; b=tl1RGufNUksQm3ruUvormfo0UvxaweznZMzUIy/4FYkhBCY08DzTBzWXwolcNw3lJJCJ 989TZr8vgvfNvBY0fTuolytIsHpfeIIC+bwyG7uYJm8XxLjEk0c121BvlQw4RH0U+W5A 3/HaNnQ9oMWvOu2szO0PT1KIpK8y7yIKWur4r8yj24sojJbd66orDdkowg7NHS2G5XcR 2olO9g+1Vt8+I/4d+Nz9kZQBehnTVHMg+7kikxfYs9KozmBvVkGm4U4+okx0/bbYTan7 KfXtSoGmQq1xsNWuV0OLzf3w1pt71KwqkCUDHx0g+ES5BkFvyen3sr7k2HJ5wmUJjUFy BQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a6cntm8r1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:37 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4mT089331; Tue, 3 Aug 2021 17:16:37 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lj9l7syucLen8p6oEkXG6yxTPdICYjpQuzdh1N9O9g/gza1kXu0vd9UtC2Sutq7vLFh6zviKGXwKZeYyAAwDc3Pc2TgXzS9KMEA94ev22AG2krA5a08Lu3AfrbCY0asoIIrwoGFVMiTh3NX+aBeaBT5NB1/WAiDe8s9qYGrcAQhNSv+e47pg4UbhQsdcBq6sCcKHtqNz0pPyw45c1BtA4VnOVF/3S48k+RpdFjsvjszQxh+rimmVQKkTPKHip/lx/f111EHrMp9AF1nIiSpEWyvOlwaTSV0iygvCGua+r6VkmBQAFz9XC2vps6yzC2L2ZTTgyoCyIaALVifXCnUFew== 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-SenderADCheck; bh=T9nNKGNRU6sXJsi/IiNkFPpEPjrixJG72xyFEQj6SMs=; b=TViBS7000Jp2qMsaauOeVS3L1gYErX0tp22ubb3IqOQFjrxYZ+xP8irwkV+0P+Wp16Hi36qFwQeECVlBhcj3tAkaMUdWLjGUqLHZ2KgY3KSNWJdiGe+dNhPNKv7h4E4VcfVZa422u+mc4InhWdAldwOATwEs3WtPZzO/k6aivzh4BTutWi9K4fdrKO96RaPhq2aJMpKp36aFv1lBYEPXLXDNssEVxelTviW3CjSVc2hDjQTZch3l7s5kBXn26Gm8r2l7cPkPmY1wRdg3N/xuNpO5vyOsrL4BCY+F4uV6g0Ge5fBBVjvqZ8/qjo60d4HbY6J3J4VA4ZeO2VqXcsjb8g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T9nNKGNRU6sXJsi/IiNkFPpEPjrixJG72xyFEQj6SMs=; b=LW+4CBrvSuVzTLhmFnL63hCn8wnzShFwDjEVjQPeTSm/alqIMRMFFjKV+uFf9brtCl6QeOLwT3U0UweUT3Mov+9vdSfJ1zJ/UuSow9kLxsTYcnWTlZ6guNEkS+F8Fvnx9TUeAWfuI6sIhQjaL8Sfa+Vkndl/fWl0qkvK2fkvm+0= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:26 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:26 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 2/7] kbuild: add modules_thick.builtin Date: Tue, 3 Aug 2021 18:16:03 +0100 Message-Id: <20210803171608.509163-3-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:24 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ce3d7c10-8b04-400e-7394-08d956a26cd5 X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2JfYYshKeoWMSH0Lt65lHXvt1QTya/b3wvbwv9mhmsA4ycqsOdksaxqOkYOpfzf3zufSqNtcJx+Eio5iPzyD6zEW+9UXYNIPQfWALwKxjzipYEc3eyYv8e5fCGZ0VygwbtzWNYbP6X1EbclhFhndG3uCQMXsMzUSAztEOCHvCu8EZ2ZT4V8RBL2CeiKe1I31we5YMqDdPaqys/tjr11B+pGvGqZ6GuupNO26OF8z6TQ8n+g/nNfF29GFHqx57A44C2eTvxt57J+nJRDKnEOhNiPJyJ+WHTjrum0vhHCStKtqJFHnu5CgjyCI1K/8RujcTjsGBoMXNR9GqRJL9geWsVtCaNxTRGSo/GG6B/PgTeL/3Kt6UhKuzYA00ZSaIqajbGsPPOrk4OiH8BeTOIUolU4iNvosQI0dO5Zj1c9R/1ZHcXD/GDf1N2USSm8fGp7jERv+CEn2bnejfhELn9ayjdCMGjvfaOxpqYBEInEVyfR9jlE/yMzIiux4As/Yjo6zsHn901iaR+f15jX6I7q2ndRDQjby+t1ZlSrnxuBHP/AFN68spaKfVTKAbuMGpSe27NJVe06PYBs8FHjBixbXGEPVuQCUf5dYRmI92o+Mf/mfbRNgrn5trSRnsLZ9z7aSZwPsw9WG7C1hb8ubL4UC8rVShicCW+ROqlspV5106KHKKdaD6ytuqGjNhEE1/I0h 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(30864003)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tFJyfdqW24QeE1nrr44f1B/86UmqDZHiRqNP3w/UzfFmWnx2qDWEGj22cthXhnzM2tZFMmmbxuSHnUKyrxxL4BIrT99ohVzN/R4yJW6iu2D8InvMDIEE5ndC0FLc9n8poD5+7CPYQ1uSu6TUHg54jyGMeQP6vVIafwhkXMKmz64MlB8+XrbPYXc/PDsj5UwCRrNCZx4lKeSVLtoz2UyLrkKMeNV2zcr2iFVs/LJPBCA8UCLUhwqmf/FqjPFg0hFUgOqpLGgtZ4l1MjE/ML245ZKdvUKEJsO6vWk8HO5I1122mlDrLAZaXloDYkoXhEH/88SBzuxVuUTKih28ULCbS0WWVJgM0IdoU9/bNn+b79/voELR2ERE1vPVpaNxiQ+iXIxilJKOFpjNBY12z7wlzYVPQKeQ/RLXH/tjs13e8ahIyuB8C9hmPC7VvkuTIEItwUfOLk5Cf5lgxKI6mzDLYwg/qGwZDgvl5M1x5tWNktsEIHYx8pVprnbe+4zoVcrMxWfzyPHREyIWa8yVgK+j9F3A/Jr51xutU7n4T6uDlCiJcLEcr/RwglKAORZCNtxWR4h/4oyluhAw/U+6FUqXFbOso3DNx4SF2Vbi2L6+/MV6atM5XtUCyUGcVNrwhnb++MTJVmhL2ytl+dxKHjyaMP8N531UlaC9Tf8RWs6au1Wj8zN9NnAYwjb6oXqh+Utir5/XnLYH349NvJDysWq5H0zZRh7GjXTMqA07Xb+buHdPCTAurrN0eB7rfzmBr3R6kfbzvRpQ+AnkkVddsDAZZg+imOevWUWM53uDWOO1l/4ZuZRkD5t3hJ+ZWpNfd1IdckRtbB/x9VT0G5zPiLin9k/FlxWYAW0nEjgm9yISORdrxZLmzov50tBu7xLN7tVjw6Xg7iSlFqAXUlXVXFLhYCNvolp4qZmz6FVStREHAdZkdMgzl6jyA8klYlNuLJswMhGsrbJyqA8ep+L+nRZ0v4hdNLXZQ4wu7YtWvYeMsEs4gG6Dbkm5EIILf0Si1KWGTF6wbWCrgZNZceXtVe2g8SwY0LEKThdTuXOiFZ5bMOvIXsI0HWhkJ6iIi8ihOasHpxVmQhaJTv7bDD5GmVI/3OSkA8XukXeXkn26JyzZpiyZAKOvlm8ECkdImtTa972iARE10GhzsTWIQzxijRa8mTQ6mrkEoJbuhRKR3q78sUFu/Onllksy2VSI7WVyXHp56FgDr8s3l4s0guywwlHrj+Z6x0JHQSv0UHlSuc7mbgK3wuVPkD7mcYdwX8OEVdcvFJfF4JXj/0+TRLU1nbWxaHt5as7HfA9XumCmymBsfSKFyA9fkzyCp7S6vtImSYviBhqGhoK58td9G1M43X+7FA== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce3d7c10-8b04-400e-7394-08d956a26cd5 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:26.1260 (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: 0noklNTz85Xcc9YoJcO+KPDVPreMFIVTGa5pnNgJgHzrf09NcK5uKye4HDlhVDosM+Bhux7aT//i0MgPZyyx+Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-ORIG-GUID: CBpscqIfNUbYJPYAttdW6T_c7T9-GiFa X-Proofpoint-GUID: CBpscqIfNUbYJPYAttdW6T_c7T9-GiFa Precedence: bulk List-ID: This is similar to modules.builtin, and constructed in a similar way to the way that used to be built before commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29, via tristate.conf inclusion and recursive concatenation up the tree. Unlike modules.builtin, modules_thick.builtin givs the names of the object files that make up modules that are comprised of more than one object file, using a syntax similar to that of makefiles, e.g.: crypto/crypto.o: crypto/api.o crypto/cipher.o crypto/compress.o crypto/memneq.o crypto/crypto_algapi.o: crypto/algapi.o crypto/proc.o crypto/scatterwalk.o crypto/aead.o: crypto/geniv.o: (where the latter two are single-file modules). An upcoming commit will use this mapping to populate /proc/kallmodsyms. A parser is included that yields a stram of (module, objfile name[]) mappings: it's a bit baroque, but then parsing text files in C is quite painful, and I'd rather put the complexity in here than in its callers. The parser is not built in this commit, nor does it have any callers yet; nor is any rule added that causes modules_thick.builtin to actually be constructed. (Again, see a later commit for that.) I am not wedded to the approach used to construct this file, but I don't see any other way to do it despite spending a week or so trying to tie it into Kbuild without using a separate Makefile.modbuiltin: unlike the names of builtin modules (which are also recorded in the source files themseves via MODULE_*() macros) the mapping from object file name to built-in module name is not recorded anywhere but in the makefiles themselves, so we have to at least reparse them with something to indicate the builtin-ness of each module (i.e., tristate.conf) if we are to figure out which modules are built-in and which are not. Signed-off-by: Nick Alcock --- .gitignore | 1 + Documentation/dontdiff | 1 + Makefile | 19 +++- scripts/Kbuild.include | 6 ++ scripts/Makefile.modbuiltin | 56 ++++++++++ scripts/modules_thick.c | 200 ++++++++++++++++++++++++++++++++++++ scripts/modules_thick.h | 48 +++++++++ 7 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.modbuiltin create mode 100644 scripts/modules_thick.c create mode 100644 scripts/modules_thick.h diff --git a/.gitignore b/.gitignore index 7afd412dadd2..b49cd96f587a 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ *.zst Module.symvers modules.order +modules_thick.builtin # # Top-level generic files diff --git a/Documentation/dontdiff b/Documentation/dontdiff index 910b30a2a7d9..6a78988547d0 100644 --- a/Documentation/dontdiff +++ b/Documentation/dontdiff @@ -183,6 +183,7 @@ modules.builtin modules.builtin.modinfo modules.nsdeps modules.order +modules_thick.builtin modversions.h* nconf nconf-cfg diff --git a/Makefile b/Makefile index 0565caea0362..94933f01fd95 100644 --- a/Makefile +++ b/Makefile @@ -1496,6 +1496,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 @@ -1865,7 +1882,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 82dd1b65b7a8..b8cc96cd8a9d 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -73,6 +73,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 Aug 3 17:16:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416925 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0436AC4320E for ; Tue, 3 Aug 2021 17:16:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1DD560F9C for ; Tue, 3 Aug 2021 17:16:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237856AbhHCRRG (ORCPT ); Tue, 3 Aug 2021 13:17:06 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:55758 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237938AbhHCRQw (ORCPT ); Tue, 3 Aug 2021 13:16:52 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173H7Snd006793; Tue, 3 Aug 2021 17:16:38 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=pihhv38YYPVPYETSoje2STQy/1VMALYHlHaGOxM1tZc=; b=Ju+vObwU2yVAq+7AxsecwwDk105AdB5NzQGZL5Cs9tefGNaKqRIyvFyspxYG8sCkwD/j EmbYZ1PKb6xTlpHRDFsKZ/lxpdn+fAGOgIDAmYF+SwEYNLlZInkMpxRwERpKqDprC4j3 9EnGwe7o+DH0td3OSFegbVUoyGz2hfum8MHEslHqDu3mvWOtufxQnhmNZcPYMGVQH2P+ VSIeXJLl7oOr69jqLNLogxITC1z82g59yUBp7ZAogRclI0/pZbaN5/2qv6jBsrWnelQT R56XDdfZpF6pXRFIJ8Df2ks/7cHc1eQfVGgpOnhL2fzaj5d1qtCjXqG/p/9Av8phk374 AQ== 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-2020-01-29; bh=pihhv38YYPVPYETSoje2STQy/1VMALYHlHaGOxM1tZc=; b=awep4IXO9AeFWi3H6uWBNgv13eSOjQksqffo8wxaszkwsa5UvaLXrGkEX/2MQO2rwfKu OoNc4rDNtpMVlfgM8WH7PqxeFXRsj75Y5lqEKmxq6QL6SS4suBoYpcAo4Jg1VqkPi2Z/ fAnw2kExa3MiEDO/5d8uq+ZJGk26wrzEUD5TZtAa4mEJN2NLjryFZSCIOpUFy1a3kKN3 LTZMt65+9IYE+4MzWIwhQhpgc/aP2SjSkcfx+V7RG+fawJIzba2rwL3MVZpin0SAs623 nDspKIT969cvE7q/0PdXnKNPaRAYaeDVfrSh/OGz5yS/uCtnSx7ErXEcBbZhtzmFCATr rQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a6cntm8r2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:38 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4mV089331; Tue, 3 Aug 2021 17:16:37 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e3hlKlhiNaA+kXwA+SzMaPgOeeBxyHMYSblumWz/IDmhsB9EAGVsqss4TqzMqpnVbWJe969pm0yqj3WIlY7g8iqz/PJNa0JQHzBhXJiG47TWAy8MCBEKiTiVhJivJob+R/9np3RNTTUT0oC/tKPrGkQumVjE2S4kPaFf95o+XCwbFZ9ZPpU6Qq2lIKmlQrI8g6r6ULqbTBSuVymDM69Xe6NjkPx6qO2cwaiz5iEV5q4n1xzGkJEHtF163jE3Z5KaVzkO7nhekjxYnaV80GGQzeRpT340o9WLEy3uzwmsjEfZMl6FXQIWRJeZVqKf+TxUL5aIxMdF4IoWeLYcC7oEmw== 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-SenderADCheck; bh=pihhv38YYPVPYETSoje2STQy/1VMALYHlHaGOxM1tZc=; b=Wx8Y4HPWxLH/2MVJhK4Q7QLuzZ9s6jYfgSrJyfLC/fo9oQeigL1vdyljYDYCKKBFm6eb2BpjdhEq48Z0sVyYDApvto+Zru4s1hyhavMEpoepd6kvgpJsyyMq89jPLYhk96GHcakyqCArpTq84XLSP8usZkT5TtMTR7hFgAtgyyB/LmSUECZPihXrJCfeyk7a0ahHw+goNRx+AU1MsY0UhK9q31SE2NdEG3oTPCvpa+jSlS7Yau/1Qrv32HkqW5X3EIlwbpDVGgJ+fd2uN2x8te5H3eQjvksi1KptugJbibcB1gT+JvfYLEspnREu4Qy4sM/8/xcemj0m5xyBBlC46g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pihhv38YYPVPYETSoje2STQy/1VMALYHlHaGOxM1tZc=; b=vrUv/JOL3Too0w4vHjNQ2BzJhbRWv+GNkkbDBXPlPZAkQPiyEBSMQWS4Im19RJuP9ZajH+2JNd2o9DdnpplElga18QIn4qo8DZCZjdLL+3KyHKGPZZoTV7eTFI52mvlh7kIXDmdsrvKW5YYmUUDOBbY0TvAg4XP/gOES0Zg9Yx8= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:28 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:28 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 3/7] kbuild: generate an address ranges map at vmlinux link time Date: Tue, 3 Aug 2021 18:16:04 +0100 Message-Id: <20210803171608.509163-4-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e2ccf9bf-4f64-4015-d520-08d956a26de0 X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: grsfBzRR+oZStfgdx2rvJ+cNdBxkfCs+brBHwlUeKU/07mqzjtJmVuD75ex8ZMEOf19EmMTbRTpTsalDG7qCwg3mGtXVcmMsnfJ6xgWtIjgrR8513taq1FFMlW8iUXUqKQ2jeXD7TO+PI+YhNTvHs7SahvdliCvftSXnItwkJR106b/4GZms9lMFEp4PxMy2qDSuRyIyHZQL+W2mv4swNQEUajWjs+oH6ixkq8s7CnNBbauHgsvS/Oc8haKAFiCOhVfR5Oww7ELkPQ2//RHqrpIQdGWRIUtft176tKv+VTCHTHB0fBQNALQ+yuqHBaeG8UcY4shdINPdETgGXOQqbiBPlLnR0vhbVBftuMVoccTpOzB3PFcfJuFvySKDL33IitV0i/kaiAWct82aHv7K0dyfvYD+4eWaJ0MnNQBpLZ9HVAewXOEcXSVIUJRi9Vz2lWWEpDMLDXmR057zabCNeYidkSLDLneEyv2qlyVcqUgFPo1ME4yW4zdUcXyDdMfqaxduWZoAxGzfsvXE8KoUWMw2idfkoH9hLUqsD9UPV/HSr3jnHyTSvO1xjk/s7Q1ZwI9dOc/3unKOxAeIU0bnCSVIdIG9p+MW8thJh4UpKblC5vlfHTnOVS7JsIDp2lq2aqo1w+89mrbkGPdufnSBZw== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(478600001)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2/4pi8Gd9P4bqFG+IWyqrgyzXvKBpVic/o2WJfF+81ZbNECr0/wBAlhc+ruGpwl/bZTu6ckJIMLS840HBt3RolLIghg3kbyIHmjeZM5ezYdjkt6f576Y2W8WMN2sYNRIG4sBe1DLdAzw8ABVMMGUGff+JCLzwavU+Ac8HZ/gJKKQUxzoR14Rqhh68LtM0MKDKDZ7sVF5qlr5/118O+BmPWHUZhukboGwnVOQHVrNk81idmAqTyDCGzneBd2CD9ezP0smvfUljoIYAvrPehnhRSl9nuV5bqjWEhJdaOYUEvOvw8gvdXrGH5esC+uX+aGLrrDNLoELmNiQC+s8A/h9bckLfFkMU3xMlzTd3Mwt4trcPdwPaYVyAK2LLYwMrWpm142ceNDcvF+zsZPJ7xa0ETl329PrNtCX+N0289OdzzJy0+ebIpRJ13/qY4sZSGhNkzPXKM4nqSviarRjTJMVLM4TBI+fb0Q2TpkOGi6sPMR1i9cnXKctQfgce4cui/t5sz6WGudakMV7FlhoyEPDOI5/AXkf9mtIcp7cHXkMkm1VlZfhrsMNfUIFudgKBr/Cgsm3Qm3cjVooWLROi1Yh5ujw0pSHjaO2firrYJtG9B4Wpx+M33MbYG5YUANjbKAZ3T2A9Zq0VKzzJUL/qxIMkOQPk5zR7WklrkjrzsX0yPDBlX9OMz/1lAtwKwiBO2UtSmKTK+LYLEYysflCwX9geKjN0/XHm4jYGlvbtz4y8tEEzFmo6A6afZ+2U/km3HtZUSmOD7145c59YGMuPasINTWBAuh/y5l/359wXLsdObf07319CwHuSBbJuWsuDaCaNM1/NoJjI2inrL3HUABo8T9fOXdTnlWhu1MrhO+yo7yI2N6hZ72Eaulhhqh/V5qT/3JwmRn4aSsTwleKpBZ6rTFdVPsLX5vsIrOkz/2ZLKO991a8kR0pwp+iTDRr6InHuTxP6S4Ra4RTdGkKyu/4EwkO6bwAonBv+KZ3ZMDUNUijCehZ2kA+53p9MpzX+V+Wk3W5h4+wNbspHZfjDQBQ6a0whKWcZ31Y+AfNlg8Uqna1woHDz7ngsJbS93EkBJUbMGQRLG7aI4MtgomnHb7UIBrL0IzlCUyibl525+3Dl7MQBFHoW6eak2L6Jd8AyNSYOwNDHEwmvfOBup04yblESFP8BZOwGW/mcfDLQtVHnS4GCN5NAI5dkiXPY2ts296/3Gk4axWleMQPQ+K232rPkoINmI+hyFgtYfMblVhueptjKDnbgDsdAJ3k0bHyNBe1HgEJjjDrBZwKYJVq5GzMgooNxKxsude/fk3erz2iznWAqLHiIpeVKBlLJZUoR2edVoXqvGiGhAMw/prsuBv0Vg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e2ccf9bf-4f64-4015-d520-08d956a26de0 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:27.8823 (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: 2arhANmFszkQf1nLn5PUPjKd/m2BQyHeN7E5dr7GOTRUjpMYOBIhx22hKJKNzskBS9DBJfp3ZcjD44sDIoKTcQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-ORIG-GUID: qBP32KnZwI-coCr0gryRCCgBc-Ml7j4F X-Proofpoint-GUID: qBP32KnZwI-coCr0gryRCCgBc-Ml7j4F Precedence: bulk List-ID: This emits a new file, .tmp_vmlinux.ranges, which maps address range/size pairs in vmlinux to the object files which make them up, e.g., in part: 0x0000000000000000 0x30 arch/x86/kernel/cpu/common.o 0x0000000000001000 0x1000 arch/x86/events/intel/ds.o 0x0000000000002000 0x4000 arch/x86/kernel/irq_64.o 0x0000000000006000 0x5000 arch/x86/kernel/process.o 0x000000000000b000 0x1000 arch/x86/kernel/cpu/common.o 0x000000000000c000 0x5000 arch/x86/mm/cpu_entry_area.o 0x0000000000011000 0x10 arch/x86/kernel/espfix_64.o 0x0000000000011010 0x2 arch/x86/kernel/cpu/common.o [...] In my simple tests this seems to work with clang too, but if I'm not sure how stable the format of clang's linker mapfiles is: if it turns out not to work in some versions, the mapfile-massaging awk script added here might need some adjustment. Signed-off-by: Nick Alcock --- scripts/link-vmlinux.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 0e0f6466b18d..00ad1b4b0055 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -178,6 +178,7 @@ vmlinux_link() objects="--whole-archive \ vmlinux.o \ --no-whole-archive \ + -Map=.tmp_vmlinux.map \ ${@}" else objects="--whole-archive \ @@ -186,6 +187,7 @@ vmlinux_link() --start-group \ ${KBUILD_VMLINUX_LIBS} \ --end-group \ + -Map=.tmp_vmlinux.map \ ${@}" fi @@ -201,6 +203,7 @@ vmlinux_link() -Wl,--start-group \ ${KBUILD_VMLINUX_LIBS} \ -Wl,--end-group \ + -Wl,-Map=.tmp_vmlinux.map \ ${@}" ${CC} ${CFLAGS_vmlinux} \ @@ -259,6 +262,19 @@ kallsyms() { local kallsymopt; + # read the linker map to identify ranges of addresses: + # - for each *.o file, report address, size, pathname + # - most such lines will have four fields + # - but sometimes there is a line break after the first field + # - start reading at "Linker script and memory map" + # - stop reading at ".brk" + ${AWK} ' + /\.o$/ && start==1 { print $(NF-2), $(NF-1), $NF } + /^Linker script and memory map/ { start = 1 } + /^\.brk/ { exit(0) } + ' .tmp_vmlinux.map | sort > .tmp_vmlinux.ranges + + # get kallsyms options if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then kallsymopt="${kallsymopt} --all-symbols" fi From patchwork Tue Aug 3 17:16:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416927 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE6DFC4338F for ; Tue, 3 Aug 2021 17:16:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D50F161029 for ; Tue, 3 Aug 2021 17:16:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238071AbhHCRRH (ORCPT ); Tue, 3 Aug 2021 13:17:07 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:20650 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237886AbhHCRQx (ORCPT ); Tue, 3 Aug 2021 13:16:53 -0400 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173H7RHt023889; Tue, 3 Aug 2021 17:16:40 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=XuYUppHLJg/9LA2fhiI6Rsoyz3EtxzRT6u2Pfsh58og=; b=T2y57srzI7Q73AF6Iovdv97flO6MqsHvUEMJ2wWg+l0LOyNC0sfeYndhDxshUssKK43o mLts1LEuxRkQ9IfBUIETbTo5OfYt/MEbO+yCmxgqITq+DHLs78rIYdWzzMxskHbiPGqd EXiSNOuMXM6X2rmKBjPTB46MG3x6sMHh7yaXUCwKrWTJlcJFaL1GhBPlDD0K1Q+Rt6zy JW/3f9SadnDnKVvLHOUxF0QUQEQSvIvVoK70FpCttqDXTdIwzF8XHWnryUZNxPVisi/3 XMtyBtN2wkb+gvGKpUO3z7Pse3whjSbDgOayRjSoF73KLE7cVfVY1vH5JIMUpCEy243/ 2Q== 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-2020-01-29; bh=XuYUppHLJg/9LA2fhiI6Rsoyz3EtxzRT6u2Pfsh58og=; b=js7l149FnTy96GAoL33kqDTKgYk6phMKYc3xQfv1+wclUbh1nsPpanH5sCU2m52hcdht DfDdGcFXXnWsL0jl2fABSSwjf8lDWaoWQgFepbeX9RFmGnSWND4Brk6VAIIwG4FM2iKN c8DPbg2SX39VbxAOLAoShKETuIQXqQkpgVyz+u+AyGtI+1rPn1fZdvh+UH3Pbam49Nnz q5Uv+/m4p19n55Za9TG/YmO1RYFDCr4eW7hTH9WWsomQpQ8xvh0fYQFV2Y03P7tPl5Du /jBdY9hkefzbwqGcl1v8PxV/e/B6Nthou1dKAgoTFxl0BMZncbgx3w3Z2f1aRoD4exQs Kw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a65vdcsn3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:39 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4mX089331; Tue, 3 Aug 2021 17:16:38 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=noAxmv/OBAUv7dVVd0dmcEZyY3ou/Mm2iVUcYUTnYcVnPapV7BTkQHUrFmMQ2dyqFGOY6Nt2iw8uccv+KYrepiRm11At9fIHmbf1Tkz/J0pGtM18YZrTyfZR8/G+cDRUy9Mk2zrhNHaHBGfmBDz7XXeAxmp800TMi7StHRY6qXI79P3LyLuhDqa6fWLw4X3YkVRqTPK7QIhmtTFXUPD67mHT1xzmeaWrjPhzFA/C38y9tFEV+itwqZ5Gjhz3jl+Qxu/arGr+7huqUoDBRyWQPUHYqLyDHeSnmC4MnsRY8I0kVrxP5w7QQN6HJdL/V4+lixWaUNe/MT3cQYpSAG1x9g== 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-SenderADCheck; bh=XuYUppHLJg/9LA2fhiI6Rsoyz3EtxzRT6u2Pfsh58og=; b=LWJdz/iI1w6xpX/nsxtlbAmSIc9+zrifFy1gc6c2y/3L4IA7lEBBLJw1xFYSjtxWZAOVUxG+sCdMMN6aL9f5qyAbKUImens/3tmQeSDjWhMTR11Pad97OUTw1bE4kAqGts19Wh1D7+71I2303v+BAbZ6nzsGajC4EMHt7HwVHmWfqlyi0+d3dUBNEw6g3nhPMCKkV2kXkH69tnJVk7744fxqmQJypz0PyioNrV0YT+e4WSRfm4ZdGpwytzxwch1qfEZ8mV2BbxKC8jesbA7hSdkpNrY7wOfmf9xWCyAucS0VNnqFkS3kot0JLMHOlQBMuv/JIcjlnm9fM8DMT0Iiww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XuYUppHLJg/9LA2fhiI6Rsoyz3EtxzRT6u2Pfsh58og=; b=RAY3sW8PnNCygx4Yq8GMCIAbTI6ADDW4CItxXecnMK8G+Xy9Fzy4Ad6WeqNeErDPPD4j8YWQWevlxZLRti/oZEGu0lC1pG8XjYV+bm1OqxCjjksc0rwGYx7aoR5GI/WadBMTQP6b5o7c8ZZknqe6arCYzh5GBQNfUO2+ag3db8k= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:29 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:29 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 4/7] kallsyms: introduce sections needed to map symbols to built-in modules Date: Tue, 3 Aug 2021 18:16:05 +0100 Message-Id: <20210803171608.509163-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:28 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b834ef02-8843-4988-a775-08d956a26edb X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4KzDwM+3ItoU8gaJobb59jYU7qcd/SWBHclUN4TSFZI/P8LZyhoc4Y6kaVkPETRU48EaKU+gs4QSiSWLm8Vy4MjDFtK0P/Wt98y+btcHpMJ9iLzHHzO3jpYQIYYyts0H43cFIYg6ITsv7rU5hAfKqXzThnjILpVQ9n7G2Gm2tNowspUNfozhzELlSUcrFw9tMSZ/WHniLhsvRwHD63sJTb5dulBYdFcXWnbfc/ZzK6iqbneSPJRdJyIjfSirrZb7QP7OKQ3df7RTGRRXd8zzdckom+F4y7y53aA8gkL4cMK5wY+fUFvlpjfLT6W4bN+QVBgkLNBjPEA5SEGssAEQt4HDPhKUxAm8h0cGRi92QCi72cmfFlbdQRN4Lu2tpGy1kQiJw/6oQAcSfRIvYBFzyer0H0+oN0YFe/UsFCXeeqfL6VT2yaf3wZ/evzHTJgz6nS61pqyKf4whbH8sI+3XcNB2a4cvtHu8gfV1U2oJf3wax9UyFOWA2Arx8JrwMnooLG9dZNjIR8lHGZf1eRZ9D68d+923JbYYGshX6CL6/jqdceo733j5ZYgbkCik1HyW7EpFDxynuf+rcmz79/bsaVL57CUluNlzbvbaeAKrPgGE7RVP/AhQhFjt3TiEkLWzPJhw5YKkkrUXCc2m7Mgh8w== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(30864003)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DUscHUmIabY4Aic61oQV2sqZkkv6Qj96apzMzPd3eh0eSwONPHhjcaRrN7NOSQgC9JH5liQj0mnfavlTJ3gaaAPwDEDCZ8YEmpch0NOvuR98sZ7bgTB+vaeCoSIYVM6f4MW/AGG3bTIXcttuPcGxLreufoPVWoQxQi6C7GvXywEX9P8dEsB7RtAm5wvcb8mHpAkpfXLAy/LU7viGXhm5mPhjoDdFEi7utxM60rl5pFNeeBO/nuSgSoBCj/hLbTkYUt/S08CTsXqCFB+oSOK8VHjv8anJ6XX/8hYmU3wLDPj32KEl5Ctlv06io+XOP6APxPIpn2FxkfO2R/wZLYrrRvnIoH16yILhPU1wiGkQKazcZnkDqJkGFxUEv734sFjMYl/tXOipq416y0FdhFmGHsRjMC1Ht8l4f95XF2ZumqgNbnUV02Si8dmvtMZJrSutpIVRNzbbJbuPamk7leopeueS2JI04UatHUWieKAuCna6k0FTr0gWZSUhL9TD0uw8EvvlAfkrjsSWklClalC2FBeUGP0ock2fyOuX7+RXIIgirKSWhYtN8z+cvEEbGbU95ww8XpKFbOc0XbPMF+/+Lv4nBwBdq3V0fKByZqn+YeCB9eocGvV4CpwIUHUqznNONhaGS1vj/89mPLyxkHuAi6YNWzr5KDhEcCr2RVFgehUc0XLV/bqgi7SxotQkd/oL6FMEdWrR8QNNKazV2U/JzCs17nKPbe3LXusCLPbbLijhtdqToAFRCpGTZvsVB/Bz0T7/WC/tJQgbeKUtOSSntOF6Oyq70nwgahZod5mvoI8cAB9MpnkF1w+IUm30UH3V+p6BV+UKyKDQB3Xkvuni+H1bG2xRN7tz/5AeZsAq/L8aRg8lhPUghMJpWLY8dM+QyITnMCXPchBarfc4i8fwstJ+m2NjFJKLYAvZbz/5nN/LhBr3X89FJQaXjjx3PLCQ+8XHeJZuBIMmrOhr/xS0ATJhlMSACcqsDjGcfAF5g4LbJ3sWrtt8Gket7F9SwPVQXHEOGkoiPxn5d8xFDXD71lG5L79sKPEo3pMONuCTM/JhA+RLCez4FcumTFuEdGMAbi0B/kSvf6T29kVVdNbNb8jqTvuQtnrhK3sXnMIsAXGoZvirFRHv2Bac11ilg/U3g2a8Bjp/CYYTGwMF09gouIt+a5UehZ3pIlsSQjpkfeKDO9Ym2RSTXDgSU3KY719asJRROyyZIqKZevMc0a1mSliEyRBSL6niogFr+EuUkQd1pA6uxr2HvUDcBx9oM52Q8+7br58I5h9ecySPHy9JyB0f+2k/k2xMinT6JmiYURKys8sXyN/fQMw9JNmk/T0KtEfx2Z1RVLfgk6m0frSK5w== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b834ef02-8843-4988-a775-08d956a26edb X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:29.5559 (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: 4ZTvX27YKM/12GUulAu0KmR1dXfxtYT91w25/ABZwS0gT5rY6DjYNgJZoct2PivGRSI1LhK/iE5LSFQnznAe0A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-GUID: eugN_ju0g-VuPE68jO8osHNndHEu0Lb5 X-Proofpoint-ORIG-GUID: eugN_ju0g-VuPE68jO8osHNndHEu0Lb5 Precedence: bulk List-ID: The mapping consists of three new symbols, computed by integrating the information in the (just-added) .tmp_vmlinux.ranges and modules_thick.builtin: taken together, they map address ranges (corresponding to object files on the input) to the names of zero or more modules containing those address ranges. - kallsyms_module_addresses/kallsyms_module_offsets encodes the address/offset of each object file (derived from the linker map), in exactly the same way as kallsyms_addresses/kallsyms_offsets does for symbols. There is no size: instead, the object files are assumed to tile the address space. (This is slightly more space-efficient than using a size). Non-text-section addresses are skipped: for now, all the users of this interface only need module/non-module information for instruction pointer addresses, not absolute-addressed symbols and the like. This restriction can easily be lifted in future. (Regarding the name: right now the entries correspond pretty closely to object files, so we could call the section kallsyms_objfiles or something, but the optimizer added in the next commit will change this.) - kallsyms_module_names encodes the name of each module in a modified form of strtab: notably, if an object file appears in *multiple* modules, all of which are built in, this is encoded via a zero byte, a one-byte module count, then a series of that many null-terminated strings. As a special case, the table starts with a single zero byte which does *not* represent the start of a multi-module list. - kallsyms_modules connects the two, encoding a table associated 1:1 with kallsyms_module_addresses / kallsyms_module_offsets, pointing at an offset in kallsyms_module_names describing which module (or modules, for a multi-module list) the code occupying this address range is part of. If an address range is part of no module (always built-in) it points at 0 (the null byte at the start of the kallsyms_module_names list). There is no optimization yet: kallsyms_modules and kallsyms_module_names will almost certainly contain many duplicate entries, and kallsyms_module_{addresses,offsets} may contain consecutive entries that point to the same place. The size hit is fairly substantial as a result, though still much less than a naive implementation mapping each symbol to a module name would be: 50KiB or so. Signed-off-by: Nick Alcock Reported-by: kernel test robot --- 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 94933f01fd95..bbd0b13a8f0b 100644 --- a/Makefile +++ b/Makefile @@ -1195,7 +1195,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 a61c92066c2e..1044b00b8361 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1495,6 +1495,14 @@ config POSIX_TIMERS If unsure say y. +config KALLMODSYMS + default y + bool "Enable support for /proc/kallmodsyms" if EXPERT + depends on KALLSYMS + help + This option enables the /proc/kallmodsyms file, which maps symbols + to addresses and their associated modules. + config PRINTK default y bool "Enable support for printk" if EXPERT diff --git a/scripts/Makefile b/scripts/Makefile index 9adb6d247818..d677995d3f31 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -32,6 +32,12 @@ HOSTCFLAGS_sorttable.o += -DUNWINDER_ORC_ENABLED HOSTLDLIBS_sorttable = -lpthread endif +kallsyms-objs := kallsyms.o + +ifdef CONFIG_KALLMODSYMS +kallsyms-objs += modules_thick.o +endif + # The following programs are only built on demand hostprogs += unifdef diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 54ad86d13784..8f87b724d0fa 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,10 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S + * Usage: nm -n vmlinux + * | scripts/kallsyms [--all-symbols] [--absolute-percpu] + * [--base-relative] [--builtin=modules_thick.builtin] + * > symbols.S * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -24,6 +27,10 @@ #include #include #include +#include +#include "modules_thick.h" + +#include "../include/generated/autoconf.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) @@ -67,11 +74,116 @@ static int token_profit[0x10000]; static unsigned char best_table[256][2]; static unsigned char best_table_len[256]; +#ifdef CONFIG_KALLMODSYMS +static unsigned int strhash(const char *s) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + + for (; *s; s++) + hash = (hash ^ *s) * 0x01000193; + return hash; +} + +#define OBJ2MOD_BITS 10 +#define OBJ2MOD_N (1 << OBJ2MOD_BITS) +#define OBJ2MOD_MASK (OBJ2MOD_N - 1) +struct obj2mod_elem { + char *obj; + char *mods; /* sorted module name strtab */ + size_t nmods; /* number of modules in "mods" */ + size_t mods_size; /* size of all mods together */ + int mod_offset; /* offset in .kallsyms_module_names */ + struct obj2mod_elem *obj2mod_next; +}; + +/* + * Map from object files to obj2mod entries (a unique mapping). + */ + +static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static size_t num_objfiles; + +/* + * An ordered list of address ranges and the objfile that occupies that range. + */ +struct addrmap_entry { + unsigned long long addr; + struct obj2mod_elem *objfile; +}; +static struct addrmap_entry *addrmap; +static int addrmap_num, addrmap_alloced; + +static void obj2mod_init(void) +{ + memset(obj2mod, 0, sizeof(obj2mod)); +} + +static struct obj2mod_elem *obj2mod_get(const char *obj) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (strcmp(elem->obj, obj) == 0) + return elem; + } + return NULL; +} + +/* + * Note that a given object file is found in some module, interning it in the + * obj2mod hash. Should not be called more than once for any given (module, + * object) pair. + */ +static void obj2mod_add(char *obj, char *mod) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + elem = obj2mod_get(obj); + if (!elem) { + elem = malloc(sizeof(struct obj2mod_elem)); + if (!elem) + goto oom; + memset(elem, 0, sizeof(struct obj2mod_elem)); + elem->obj = strdup(obj); + if (!elem->obj) + goto oom; + elem->mods = strdup(mod); + if (!elem->mods) + goto oom; + + elem->obj2mod_next = obj2mod[i]; + obj2mod[i] = elem; + num_objfiles++; + } else { + elem->mods = realloc(elem->mods, elem->mods_size + + strlen(mod) + 1); + if (!elem->mods) + goto oom; + strcpy(elem->mods + elem->mods_size, mod); + } + + elem->mods_size += strlen(mod) + 1; + elem->nmods++; + if (elem->nmods > 255) { + fprintf(stderr, "kallsyms: %s: too many modules associated with this object file\n", + obj); + exit(EXIT_FAILURE); + } + return; +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} +#endif /* CONFIG_KALLMODSYMS */ static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] " - "[--base-relative] < in.map > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] " + "[--base-relative] [--builtin=modules_thick.builtin] " + "< nm_vmlinux.out > symbols.S\n"); exit(1); } @@ -95,10 +207,16 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_offsets", "kallsyms_relative_base", "kallsyms_num_syms", + "kallsyms_num_modules", "kallsyms_names", "kallsyms_markers", "kallsyms_token_table", "kallsyms_token_index", + "kallsyms_module_offsets", + "kallsyms_module_addresses", + "kallsyms_modules", + "kallsyms_module_names", + "kallsyms_module_names_len", /* Exclude linker generated symbols which vary between passes */ "_SDA_BASE_", /* ppc */ "_SDA2_BASE_", /* ppc */ @@ -246,8 +364,8 @@ static struct sym_entry *read_symbol(FILE *in) return sym; } -static int symbol_in_range(const struct sym_entry *s, - const struct addr_range *ranges, int entries) +static int addr_in_range(unsigned long long addr, + const struct addr_range *ranges, int entries) { size_t i; const struct addr_range *ar; @@ -255,7 +373,7 @@ static int symbol_in_range(const struct sym_entry *s, for (i = 0; i < entries; ++i) { ar = &ranges[i]; - if (s->addr >= ar->start && s->addr <= ar->end) + if (addr >= ar->start && addr <= ar->end) return 1; } @@ -269,8 +387,8 @@ static int symbol_valid(const struct sym_entry *s) /* if --all-symbols is not specified, then symbols outside the text * and inittext sections are discarded */ if (!all_symbols) { - if (symbol_in_range(s, text_ranges, - ARRAY_SIZE(text_ranges)) == 0) + if (addr_in_range(s->addr, text_ranges, + ARRAY_SIZE(text_ranges)) == 0) return 0; /* Corner case. Discard any symbols with the same value as * _etext _einittext; they can move between pass 1 and 2 when @@ -352,6 +470,121 @@ static void output_address(unsigned long long addr) printf("\tPTR\t_text - %#llx\n", _text - addr); } +#ifdef CONFIG_KALLMODSYMS +/* Output the .kallmodsyms_modules symbol content. */ +static void output_kallmodsyms_modules(void) +{ + struct obj2mod_elem *elem; + size_t offset = 1; + size_t i; + + /* + * Traverse and emit, updating mod_offset accordingly. + * Emit a single \0 at the start, to encode non-modular objfiles. + */ + output_label("kallsyms_module_names"); + printf("\t.byte\t0\n"); + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; + elem = elem->obj2mod_next) { + const char *onemod; + size_t i; + + elem->mod_offset = offset; + onemod = elem->mods; + + /* + * Technically this is a waste of space: we could just + * as well implement multimodule entries by pointing one + * byte further back, to the trailing \0 of the previous + * entry, but doing it this way makes it more obvious + * when an entry is a multimodule entry. + */ + if (elem->nmods != 1) { + printf("\t.byte\t0\n"); + printf("\t.byte\t%zi\n", elem->nmods); + offset += 2; + } + + for (i = elem->nmods; i > 0; i--) { + printf("\t.asciz\t\"%s\"\n", onemod); + offset += strlen(onemod) + 1; + onemod += strlen(onemod) + 1; + } + } + } + printf("\n"); + output_label("kallsyms_module_names_len"); + printf("\t.long\t%zi\n", offset); +} + +static void output_kallmodsyms_objfiles(void) +{ + size_t i = 0; + size_t emitted_offsets = 0; + size_t emitted_objfiles = 0; + + if (base_relative) + output_label("kallsyms_module_offsets"); + else + output_label("kallsyms_module_addresses"); + + for (i = 0; i < addrmap_num; i++) { + long long offset; + int overflow; + + if (base_relative) { + if (!absolute_percpu) { + offset = addrmap[i].addr - relative_base; + overflow = (offset < 0 || offset > UINT_MAX); + } else { + offset = relative_base - addrmap[i].addr - 1; + overflow = (offset < INT_MIN || offset >= 0); + } + if (overflow) { + fprintf(stderr, "kallsyms failure: " + "objfile %s at address %#llx out of range in relative mode\n", + addrmap[i].objfile ? addrmap[i].objfile->obj : + "in always-built-in object", table[i]->addr); + exit(EXIT_FAILURE); + } + printf("\t.long\t0x%x\n", (int)offset); + } else + printf("\tPTR\t%#llx\n", addrmap[i].addr); + emitted_offsets++; + } + + output_label("kallsyms_modules"); + + for (i = 0; i < addrmap_num; i++) { + struct obj2mod_elem *elem = addrmap[i].objfile; + /* + * Address range cites no object file: point at 0, the built-in + * module. + */ + if (addrmap[i].objfile == NULL) { + printf("\t.long\t0x0\n"); + emitted_objfiles++; + continue; + } + + /* + * Zero offset is the initial \0, there to catch uninitialized + * obj2mod entries, and is forbidden. + */ + assert (elem->mod_offset != 0); + + printf("\t.long\t0x%x\n", elem->mod_offset); + emitted_objfiles++; + } + + assert (emitted_offsets == emitted_objfiles); + output_label("kallsyms_num_modules"); + printf("\t.long\t%zi\n", emitted_objfiles); + printf("\n"); +} +#endif /* CONFIG_KALLMODSYMS */ + /* uncompress a compressed symbol. When this function is called, the best table * might still be compressed itself, so the function needs to be recursive */ static int expand_symbol(const unsigned char *data, int len, char *result) @@ -451,6 +684,11 @@ static void write_src(void) printf("\n"); } +#ifdef CONFIG_KALLMODSYMS + output_kallmodsyms_modules(); + output_kallmodsyms_objfiles(); +#endif + output_label("kallsyms_num_syms"); printf("\t.long\t%u\n", table_cnt); printf("\n"); @@ -735,7 +973,7 @@ static void make_percpus_absolute(void) unsigned int i; for (i = 0; i < table_cnt; i++) - if (symbol_in_range(table[i], &percpu_range, 1)) { + if (addr_in_range(table[i]->addr, &percpu_range, 1)) { /* * Keep the 'A' override for percpu symbols to * ensure consistent behavior compared to older @@ -762,17 +1000,124 @@ static void record_relative_base(void) } } +#ifdef CONFIG_KALLMODSYMS +/* + * Read the linker map. + */ +static void read_linker_map(void) +{ + unsigned long long addr, size; + char obj[PATH_MAX+1]; + FILE *f = fopen(".tmp_vmlinux.ranges", "r"); + + if (!f) { + fprintf(stderr, "Cannot open '.tmp_vmlinux.ranges'.\n"); + exit(1); + } + + addrmap_num = 0; + addrmap_alloced = 4096; + addrmap = malloc(sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + + /* + * For each address range, add to addrmap the address and the objfile + * entry to which the range maps. Only add entries relating to text + * ranges. (We assume that the text ranges are tightly packed, because + * in any reasonable object file format they will be, so we can ignore + * the size.) + * + * Ranges that do not correspond to a built-in module, but to an + * always-built-in object file, have no obj2mod_elem and point at NULL + * instead. + */ + + while (fscanf(f, "%llx %llx %s\n", &addr, &size, obj) == 3) { + struct obj2mod_elem *elem = obj2mod_get(obj); + + if (addr == 0 || size == 0 || + !addr_in_range(addr, text_ranges, ARRAY_SIZE(text_ranges))) + continue; + + if (addrmap_num >= addrmap_alloced) { + addrmap_alloced *= 2; + addrmap = realloc(addrmap, + sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + } + + addrmap[addrmap_num].addr = addr; + addrmap[addrmap_num].objfile = elem; + addrmap_num++; + } + fclose(f); + return; + +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} + +/* + * Read "modules_thick.builtin" (the list of built-in modules). Construct the + * obj2mod hash to track objfile -> module mappings. Read ".tmp_vmlinux.ranges" + * (the linker map) and build addrmap[], which maps address ranges to built-in + * module names (using obj2mod). + */ +static void read_modules(const char *modules_builtin) +{ + struct modules_thick_iter *i; + char *module_name = NULL; + char **module_paths; + + obj2mod_init(); + /* + * Iterate over all modules in modules_thick.builtin and add each. + */ + i = modules_thick_iter_new(modules_builtin); + if (i == NULL) { + fprintf(stderr, "Cannot iterate over builtin modules.\n"); + exit(1); + } + + while ((module_paths = modules_thick_iter_next(i, &module_name))) { + char **walk = module_paths; + while (*walk) { + obj2mod_add(*walk, module_name); + walk++; + } + free(module_paths); + } + + free(module_name); + modules_thick_iter_free(i); + + /* + * Read linker map. + */ + read_linker_map(); +} +#else +static void read_modules(const char *unused) {} +#endif /* CONFIG_KALLMODSYMS */ + int main(int argc, char **argv) { + const char *modules_builtin = "modules_thick.builtin"; + if (argc >= 2) { int i; for (i = 1; i < argc; i++) { - if(strcmp(argv[i], "--all-symbols") == 0) + if (strcmp(argv[i], "--all-symbols") == 0) all_symbols = 1; else if (strcmp(argv[i], "--absolute-percpu") == 0) absolute_percpu = 1; else if (strcmp(argv[i], "--base-relative") == 0) base_relative = 1; + else if (strncmp(argv[i], "--builtin=", 10) == 0) + modules_builtin = &argv[i][10]; else usage(); } @@ -780,6 +1125,7 @@ int main(int argc, char **argv) usage(); read_map(stdin); + read_modules(modules_builtin); shrink_table(); if (absolute_percpu) make_percpus_absolute(); From patchwork Tue Aug 3 17:16:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416929 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 327EFC4338F for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F45B60F70 for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237938AbhHCRRI (ORCPT ); Tue, 3 Aug 2021 13:17:08 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:58862 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237901AbhHCRQy (ORCPT ); Tue, 3 Aug 2021 13:16:54 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173H7Rau006788; Tue, 3 Aug 2021 17:16:40 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=d/37msxlFiJLKgtSdrnOIX6j5D/RTjn/uEhN2RNC/7Q=; b=ASsvXbEFuQwMvSL++qfiDVSUoCYDlT8U1kzhYRBY9sq9M57AhAwNLbnRpOSAB/J1EezB JLU2BjAqDaR+GhOGQDon4k98U6HILPJW0AK5vIsZN2ToTYLkeWYg+nI/Y3s1QZvf3df2 Wvhc6Fgw9K1EcDjMnuIoSYLYguOWRl7cr5VY02aBRs1igjfRb7iw3PIvj22QH60p2WDO YN55KkaN6HRZzk3nWa2/dqkuzlGuXWmFdqr4WIXAliWr0owz0XE2623bFOb8S8aOx7a7 1O3IKg+HYAFH2t5LLr+gC+aBAAdzqNX2x4UmUk9kh2LjsWrVXx9raUZVmAooJMI2P8FH nw== 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-2020-01-29; bh=d/37msxlFiJLKgtSdrnOIX6j5D/RTjn/uEhN2RNC/7Q=; b=D+1m7xyWgd8FUGKJC9C7tfvtFMssniQSqGCQIPthKZ/dfxVzSTbeB3i7jCZx++pnsdZy /HJFYqKxUPQ+8u13GX7/Q9uBcL4MoGf8ZtyQQjLvjNr51emBCpYdX0AZ12tmT36xf+TI azHrI9bVfaFkxL6xMI55k2fDePHdvkWnff3jZqlianqKX+B5Zrc0kv5SZTskdSRcjvzQ PMKmfClyFQZQ1tHSavuhHgN1asYtLjrin442bjsolXhauTGlwq1ao7a0HGsitaZzFib7 nHWhvc12YSUu34Kc6bnHflBoC17LA11OkzBE5h6c69xosZ0+lyo+ymocNvQ0ZKpWDpEr 1Q== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a6cntm8r6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:40 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4mY089331; Tue, 3 Aug 2021 17:16:39 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fmkwUqr+vlVJAY+F+XKua5ytZ2qyILE7lf/nm8ATSoB1KYcOcVY34MjWP1e6mQ+uZoxBFHpJqGjdjebR2TWKpRzMHHIPJMToMsWR0lcrCHAydi/lBMKV8B64bV4hphhEhloYwSkxeZ705Dn88Eufzi7em1o4jrUn23TTG+g2T22XfZDcukJbVyBshCOuNTmnq8eAq3dXocQleE9jpIUn3yS8poXzz1dzzRbSOoRdWBEgTGDcL4NgA8EcZE5YtjcGxS5jrfM1YbYuQzaGSVlNFUQ4ZsB5d9a7shnYz+IOG2ZMDlYQFyuUicwdU6nTmf58/njv394eL92U3B40hqgf6A== 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-SenderADCheck; bh=d/37msxlFiJLKgtSdrnOIX6j5D/RTjn/uEhN2RNC/7Q=; b=A+LkUJmC/oCGr/MFK4US2L381mquR8Y6js5dXTcO3/kvxk+x9+8tpiVoBlKHd3gGQAUBFtluGuVd4v04PUa/KCP8iPruySBEJXDZcuHNJiILL9eKZTiWk6Go2M6HWViRlS+umEe4jJ+TyvakExVErwM/olozTH+oN/A9bTNxKgrfK5i+PvbD+DjeklY65S36NjGXIvwgdoI8Z67McsdS49iW3P1eY4YR0IYbf8inUR0UqYmpU7zUiE+5xVcf3JdGCof2o06zu00sgxpjfHjko5KHSNYGzCd1BQdGTS/ZrCdYk/0fIVP8SAGPCYfzRH3CIIxq3GUe0i6zVQpztl3RwQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=d/37msxlFiJLKgtSdrnOIX6j5D/RTjn/uEhN2RNC/7Q=; b=nHKImNGDzyW+HnTtOrLMf5Vwg5w+I95xqSoF4HlAFsMXVnyPT0MsBieS9p2sFKLbl1H8rncrITpzQV+nJkXiF5A+txiUZzfW92e4Flki/OLWVmG5Pq4x71LldKsD9FFJ/6lgdUdgwHHKO3+gKeg3KPVNVzE5zkKDX3qyFB2vZgk= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:31 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:31 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 5/7] kallsyms: optimize .kallsyms_modules* Date: Tue, 3 Aug 2021 18:16:06 +0100 Message-Id: <20210803171608.509163-6-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 18d1c57f-65e2-4123-9ba1-08d956a26ffc X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Akv1hqHLMOdVWJ8Ew48MT2mouyTQtBOkzBrICQqM5XfdIPjCGcd7fZAgKgBBc1743xl2Cc6hGjasHAee+sf4NbqGmD0wdSWO1FYhfO5fkp2ujIlxiPHe/M7cGnRZa5bDsp34nm7Dk5IKypDr9DykhvdISF9RlYafu8eGd2f4FA2b4BGwfwcaAGZ833IFm0BAwR9pN/hN2flEMvsmmMdWOAWgMZsoRiM79+MM+Crg+EMREejEHY1995YIR/h6PEu+gzx4vCyyytGn5rxx5HyLDhsoh2f/Q6YHRqnFjGhMAFFVz+3xhpHBhzCPtlxfhvqiRqS0+VX+1YS3pzrDMXAoz3Q5kFd2M+u/9XJU/PEiclp3gHqlE2CVjBPk0jGRiWqWck1CN9sAn5+2uTS6Ju5Dh8IXB+bkQCl4YD0S5r902R1fqwWGWnsP/eUtGcZWSxRUoFD/uFmBrNfwBV1lPk1JNXrA2KDCVmvBy9PPh5FS7VxXs2k8X7HyDyAe2Yyygls2cVdDRKzb6A/UsKCdsljf+IoXjw6L1vnOVuGmpoQPbM4MB8Zigo6oOZn5PeQvrV6OcBW+joq80KzHWTnh2/GxmjLGZazMyiYCIew/IyDLR31Ggk8R82IzZmayC60LqADJTT6FSsO27WIrmXnH0iRKs4rZYG02g3qw3frodJcRpeuEzG6OhrsDIjYhX+ZN9vtAg7IZzAGdTAODmbVxqeHnTA== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(30864003)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007)(142923001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wPwNPX8ZGH4FBZnclWXIaFWYCqKQGFkI+7Vpyk6BxKQIcNTdDbF8Z/GWP0hxUG6r+vkHYR2aZJKjHbVHR6YNrQyjybwh7tnF15jyPv7gLkXbOd5D1ap1mRjCoByM2XNFAgHGv2gLqgTaLtmFv1OE+Ik7N1Pdw1s9bcm7SGFlCaCLOsgdygXGqovh+38Z/wH2O+ftBFXGDzt9yAAJOtPNyFYttTJj0OZ25L3I6n+H5KRP2JJfagFz5I4FXDoNYYFsT/iOkEqAB9T5jr+TjhYFsZdo9ZH8AKfcP3KsmczeZYePmWCPWI+Eav7o2pzHX8AIgyJk/6UmdKEbP1eSTNXO7E9EOnBwlfSwBqVc4SfHek9eU65LM5DU6wvcUSfZJHmfetdn/4mrNcHlYoqzzFm+GBX9a1PVwg6OMFO3JEOQJElGN8juqBZIq2gnfzEkhgjUY/uLcw/7A5ngl97BUqvWHofbbM8+z09kBvX2O5DaxUvy11kcACcj6w7oRIgpYTVgkANGSGVKEAnWK37LfeQSeFrKrmwRZQrldGl0DWjWuiTHE+DlhCAbOk8SHRvuKB9Qy/pB8VL6mrOqVvWT9f/iWmTU9R70eZGyRbCJvRlM59FTocv9WwB4TwzRtCTcJgTcFVssdcyevnHXaQWKDXkQM3/IU3ExxChxBtriBjme6Gb4Oj7o4EbQ50GxSiwzb8TY0nSDeIGPfeUR/fbzJlyYhehzl9tJODl1ll4rmtoG+kzK6AVzfeRZ3aY4Pp2MuEdPGXCuJxAVtiSV5mA+JDF4uMBPhZk6kIT06AcywTd5R36k+HYu6VuSqh2jrr3ifGHlEKlrV5tsLCzu2Nrylf+OLLFyivmWvM9Gg6MtrESaAoThEVZnkZrpBLTUFNXVtcKpMiEqFhvdMlQ6ID5oD3R/4JtzVL7rV7q27Se6iX76XZ1DJgJbO0iB2oKk8vyx+0uZ/StQyPk+3y4lwyw1dh/DjrH/r/IsFKebpJkCto4O/HGZ5Ia4iqqTOWVwHM1Mvh6reuhUTOz0opLBvGVTHNoNZ0x2vTb7JiSvEwNunyu7kXBpxGEMHzHKxzaNTOxPt7BGupWCcv7E/qQigLjuHdxQrDs5+ThJDRydjKwRFSUZvEZ6UckVivZOKOSJlbqf7rDwDFVqZ+u7jJPYLLDMqXGqKoR0uP31xbT8eg5thSj+uU0sD8qOO2kNoy8ijHyjlWmuYAvUNa4xOPv1qtSLmaH+E3jWzx/30bNx0My/Y+QqknbmtD8nqPPx4/+029WAPJeR+E0JqnlLZUMMlzoyd5XHAGX4yijBvGuO9aWx1CBjaFtm+2ScMac7kf6LQ9ddyNsWw52tGbr9x0xuih3s23qt+g== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 18d1c57f-65e2-4123-9ba1-08d956a26ffc X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:31.4287 (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: WnLpolkYtsoespSQy2J5w8cAtUlpSeb9Apa8lmlJJ4G3eRP7RbvlE7n0I8CleZrWkKQ8iom0kQgU3hl8cLkKLg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-ORIG-GUID: IeNZaa_rpf9XT7B3fhv3JV9Y5RCDfSBT X-Proofpoint-GUID: IeNZaa_rpf9XT7B3fhv3JV9Y5RCDfSBT Precedence: bulk List-ID: These symbols are terribly inefficiently stored at the moment. Add a simple optimizer which fuses obj2mod_elem entries and uses this to implement three cheap optimizations: - duplicate names are eliminated from .kallsyms_module_names. - entries in .kallsyms_modules which point at single-file modules which also appear in a multi-module list are redirected to point inside that list, and the single-file entry is dropped from .kallsyms_module_names. Thus, modules which contain some object files shared with other modules and some object files exclusive to them do not double up the module name. (There might still be some duplication between multiple multi-module lists, but this is an extremely marginal size effect, and resolving it would require an extra layer of lookup tables which would be even more complex, and incompressible to boot). - Entries in .kallsyms_modules that would contain the same value after the above optimizations are fused together, along with their corresponding .kallsyms_module_addresses/offsets entries. Due to this fusion process, and because object files can be split apart into multiple parts by the linker for hot/cold partitioning and the like, entries in .kallsyms_module_addresses/offsets no longer correspond 1:1 to object files, but more to some contiguous range of addresses which are guaranteed to belong to a single built-in module, but which may well stretch over multiple object files. The optimizer's time complexity is O(log n) in the number of objfiles at most (and probably much lower), so, given the relatively low number of objfiles, its runtime overhead is in the noise. Optimization reduces the overhead of the kallmodsyms tables by about 7500 items, dropping the .tmp_kallsyms2.o object file size by about 33KiB, leaving it 8672 bytes larger than before: a gain of .4%. The vmlinux size is not yet affected because the variables are not used and are eliminated by the linker: but if they were used (after the next commit), the size impact of all of this on the final kernel is minimal: in my testing, the kallsyms2.S file went from 14107772 to 14137245 bytes, a gain of 29743 bytes, or 0.16%: vmlinux gained 10824 bytes, a gain of .017%, and the compressed vmlinux only 7552 bytes, a gain of .08%: though the latter two values are very configuration-dependent, they seem likely to scale roughly with the kernel they are part of. Signed-off-by: Nick Alcock --- scripts/kallsyms.c | 267 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 258 insertions(+), 9 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 8f87b724d0fa..93fdf0dcf587 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -85,6 +85,17 @@ static unsigned int strhash(const char *s) return hash; } +static unsigned int memhash(char *s, size_t len) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + size_t i; + + for (i = 0; i < len; i++) + hash = (hash ^ *(s + i)) * 0x01000193; + return hash; +} + #define OBJ2MOD_BITS 10 #define OBJ2MOD_N (1 << OBJ2MOD_BITS) #define OBJ2MOD_MASK (OBJ2MOD_N - 1) @@ -94,14 +105,24 @@ struct obj2mod_elem { size_t nmods; /* number of modules in "mods" */ size_t mods_size; /* size of all mods together */ int mod_offset; /* offset in .kallsyms_module_names */ + /* + * If set at emission time, this points at another obj2mod entry that + * contains the module name we need (possibly at a slightly later + * offset, if the entry is for an objfile that appears in many modules). + */ + struct obj2mod_elem *xref; struct obj2mod_elem *obj2mod_next; + struct obj2mod_elem *mod2obj_next; }; /* - * Map from object files to obj2mod entries (a unique mapping). + * Map from object files to obj2mod entries (a unique mapping), and vice versa + * (not unique, but entries for objfiles in more than one module in this hash + * are ignored). */ static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static struct obj2mod_elem *mod2obj[OBJ2MOD_N]; static size_t num_objfiles; /* @@ -143,6 +164,8 @@ static void obj2mod_add(char *obj, char *mod) elem = obj2mod_get(obj); if (!elem) { + int j = strhash(mod) & OBJ2MOD_MASK; + elem = malloc(sizeof(struct obj2mod_elem)); if (!elem) goto oom; @@ -156,8 +179,15 @@ static void obj2mod_add(char *obj, char *mod) elem->obj2mod_next = obj2mod[i]; obj2mod[i] = elem; + elem->mod2obj_next = mod2obj[j]; + mod2obj[j] = elem; num_objfiles++; } else { + /* + * TU appears in multiple modules. mod2obj for this entry will + * be ignored from now on, except insofar as it is needed to + * maintain the hash chain. + */ elem->mods = realloc(elem->mods, elem->mods_size + strlen(mod) + 1); if (!elem->mods) @@ -177,6 +207,164 @@ static void obj2mod_add(char *obj, char *mod) fprintf(stderr, "kallsyms: out of memory\n"); exit(1); } + +/* + * Used inside optimize_obj2mod to identify duplicate module entries. + */ +struct obj2mod_modhash_elem { + struct obj2mod_elem *elem; + unsigned int modhash; /* hash value of this entry */ +}; + +static int qstrcmp(const void *a, const void *b) +{ + return strcmp((const char *) a, (const char *) b); +} + +static int qmodhash(const void *a, const void *b) +{ + const struct obj2mod_modhash_elem *el_a = a; + const struct obj2mod_modhash_elem *el_b = b; + if (el_a->modhash < el_b->modhash) + return -1; + else if (el_a->modhash > el_b->modhash) + return 1; + return 0; +} + +/* + * Associate all TUs in obj2mod which refer to the same module with a single + * obj2mod entry for emission, preferring to point into the module list in a + * multi-module objfile. + */ +static void optimize_obj2mod(void) +{ + size_t i; + size_t n = 0; + struct obj2mod_elem *elem; + struct obj2mod_elem *dedup; + /* An array of all obj2mod_elems, later sorted by hashval. */ + struct obj2mod_modhash_elem *uniq; + struct obj2mod_modhash_elem *last; + + /* + * Canonicalize all module lists by sorting them, then compute their + * hash values. + */ + uniq = malloc(sizeof(struct obj2mod_modhash_elem) * num_objfiles); + if (uniq == NULL) + goto oom; + + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (elem->nmods >= 2) { + char **sorter; + char *walk; + char *tmp_mods; + size_t j; + + tmp_mods = malloc(elem->mods_size); + sorter = malloc(sizeof(char *) * elem->nmods); + if (sorter == NULL || tmp_mods == NULL) + goto oom; + memcpy(tmp_mods, elem->mods, elem->mods_size); + + for (j = 0, walk = tmp_mods; j < elem->nmods; + j++) { + sorter[j] = walk; + walk += strlen(walk) + 1; + } + qsort(sorter, elem->nmods, sizeof (char *), + qstrcmp); + for (j = 0, walk = elem->mods; j < elem->nmods; + j++) { + strcpy(walk, sorter[j]); + walk += strlen(walk) + 1; + } + free(tmp_mods); + free(sorter); + } + + uniq[n].elem = elem; + uniq[n].modhash = memhash(elem->mods, elem->mods_size); + n++; + } + } + + qsort (uniq, num_objfiles, sizeof (struct obj2mod_modhash_elem), + qmodhash); + + /* + * Work over multimodule entries. These must be emitted into + * .kallsyms_module_names as a unit, but we can still optimize by + * reusing some other identical entry. Single-file modules are amenable + * to the same optimization, but we avoid doing it for now so that we + * can prefer to point them directly inside a multimodule entry. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + const char *onemod; + size_t j; + + if (uniq[i].elem->nmods < 2) + continue; + + /* Duplicate multimodule. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i].modhash) { + uniq[i].elem->xref = last->elem; + continue; + } + + /* + * Single-module entries relating to modules also emitted as + * part of this multimodule entry can refer to it: later, we + * will hunt down the right specific module name within this + * multimodule entry and point directly to it. + */ + onemod = uniq[i].elem->mods; + for (j = uniq[i].elem->nmods; j > 0; j--) { + int h = strhash(onemod) & OBJ2MOD_MASK; + + for (dedup = mod2obj[h]; dedup; + dedup = dedup->mod2obj_next) { + if (dedup->nmods > 1) + continue; + + if (strcmp(dedup->mods, onemod) != 0) + continue; + dedup->xref = uniq[i].elem; + assert (uniq[i].elem->xref == NULL); + } + onemod += strlen(onemod) + 1; + } + + last = &uniq[i]; + } + + /* + * Now traverse all single-module entries, xreffing every one that + * relates to a given module to the first one we saw that refers to that + * module. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + if (uniq[i].elem->nmods > 1) + continue; + + if (uniq[i].elem->xref != NULL) + continue; + + /* Duplicate module name. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i].modhash) { + uniq[i].elem->xref = last->elem; + assert (last->elem->xref == NULL); + continue; + } + last = &uniq[i]; + } + return; +oom: + fprintf(stderr, "kallsyms: out of memory optimizing module list\n"); + exit(EXIT_FAILURE); +} #endif /* CONFIG_KALLMODSYMS */ static void usage(void) @@ -479,7 +667,7 @@ static void output_kallmodsyms_modules(void) size_t i; /* - * Traverse and emit, updating mod_offset accordingly. + * Traverse and emit, chasing xref and updating mod_offset accordingly. * Emit a single \0 at the start, to encode non-modular objfiles. */ output_label("kallsyms_module_names"); @@ -489,9 +677,15 @@ static void output_kallmodsyms_modules(void) elem = elem->obj2mod_next) { const char *onemod; size_t i; + struct obj2mod_elem *out_elem = elem; - elem->mod_offset = offset; - onemod = elem->mods; + if (elem->xref) + out_elem = elem->xref; + if (out_elem->mod_offset != 0) + continue; /* Already emitted. */ + + out_elem->mod_offset = offset; + onemod = out_elem->mods; /* * Technically this is a waste of space: we could just @@ -500,13 +694,13 @@ static void output_kallmodsyms_modules(void) * entry, but doing it this way makes it more obvious * when an entry is a multimodule entry. */ - if (elem->nmods != 1) { + if (out_elem->nmods != 1) { printf("\t.byte\t0\n"); - printf("\t.byte\t%zi\n", elem->nmods); + printf("\t.byte\t%zi\n", out_elem->nmods); offset += 2; } - for (i = elem->nmods; i > 0; i--) { + for (i = out_elem->nmods; i > 0; i--) { printf("\t.asciz\t\"%s\"\n", onemod); offset += strlen(onemod) + 1; onemod += strlen(onemod) + 1; @@ -533,6 +727,13 @@ static void output_kallmodsyms_objfiles(void) long long offset; int overflow; + /* + * Fuse consecutive address ranges citing the same object file + * into one. + */ + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + if (base_relative) { if (!absolute_percpu) { offset = addrmap[i].addr - relative_base; @@ -558,6 +759,13 @@ static void output_kallmodsyms_objfiles(void) for (i = 0; i < addrmap_num; i++) { struct obj2mod_elem *elem = addrmap[i].objfile; + int orig_nmods; + const char *orig_modname; + int mod_offset; + + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + /* * Address range cites no object file: point at 0, the built-in * module. @@ -568,13 +776,53 @@ static void output_kallmodsyms_objfiles(void) continue; } + orig_nmods = elem->nmods; + orig_modname = elem->mods; + + /* + * Chase down xrefs, if need be. There can only be one layer of + * these: from single-module entry to other single-module entry, + * or from single- or multi-module entry to another multi-module + * entry. Single -> single and multi -> multi always points at + * the start of the xref target, so its offset can be used as is. + */ + if (elem->xref) + elem = elem->xref; + + if (elem->nmods == 1 || orig_nmods > 1) + mod_offset = elem->mod_offset; + else { + /* + * If this is a reference from a single-module entry to + * a multi-module entry, hunt down the offset to this + * specific module's name (which is guaranteed to be + * present: see optimize_obj2mod). + */ + + size_t j = elem->nmods; + const char *onemod = elem->mods; + mod_offset = elem->mod_offset; + + for (; j > 0; j--) { + if (strcmp(orig_modname, onemod) == 0) + break; + onemod += strlen(onemod) + 1; + } + assert (j > 0); + /* + * +2 to skip the null byte and count at the start of + * the multimodule entry. + */ + mod_offset += onemod - elem->mods + 2; + } + /* * Zero offset is the initial \0, there to catch uninitialized * obj2mod entries, and is forbidden. */ - assert (elem->mod_offset != 0); + assert (mod_offset != 0); - printf("\t.long\t0x%x\n", elem->mod_offset); + printf("\t.long\t0x%x\n", mod_offset); emitted_objfiles++; } @@ -1093,6 +1341,7 @@ static void read_modules(const char *modules_builtin) free(module_name); modules_thick_iter_free(i); + optimize_obj2mod(); /* * Read linker map. From patchwork Tue Aug 3 17:16:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416931 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51D1BC4320A for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3C90760F70 for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238085AbhHCRRJ (ORCPT ); Tue, 3 Aug 2021 13:17:09 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:22634 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237952AbhHCRQy (ORCPT ); Tue, 3 Aug 2021 13:16:54 -0400 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173HGRNJ010303; Tue, 3 Aug 2021 17:16:41 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=dk/+K9jub5uRxTCWF09RTLlXu15OTyiWbbzyh9erB+s=; b=0DnPb7ooxNqAG/mRifZ2iqtFTUmj9J/EKt7dZO//hRxcMmRegm1r5Qo/zBang+djiWJo DfUsOVS8mSCXf9ozsdodAuMDOLOTrCW8peALcStfgagBoBzLT+1qbPiiS5jsqVq7LIA9 O/mjQjQv6oLDnmLvJRi0Int+9gr6bTzToW8wbnT/YzLvn61eQt1sACngK4UjzMbK76Gp ozmzyqTXqEw+5KRLJp1fmIZ5DDHIpFoaTKANXC0jBbgIqo+L9Hz6C4n0gJ9aQuEc10nU jIsi1nBcOpsZSGK1rkNg0zhz/jDWm+IWKYd2PVqSwmtzT3R+3Bjf6++/ZepeyoktoUp9 6Q== 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-2020-01-29; bh=dk/+K9jub5uRxTCWF09RTLlXu15OTyiWbbzyh9erB+s=; b=dzi78CeRlTau7d9WnMKKL0Jiq5pwzplujLY53VTpV1GCYtmk4gqLQxBM4b3k/JwcdT8Z QF5FGCtpK5de1NJdtKRVHzWbnkJ/+i8x/8gHW2tmiY9UskQ2XlI//XpgKFl93pFmyqTY VhkeuXO1BhqWs4V65mSm6yic096zXXFrEvLMXSjnMNq3bz0TTLx96QBg9n2rOX78ko3m 6cMLjroiZN58hmZusPhlc1iyqjU37aC+ln0Mx75BvjFUsATtfoJvdMXqRusupxgF9v1f CoXiqm/WN/t/0fb6gdBQeR4la5HMUDaTjP5zrH5rY+/r+lWUdlPvxZM/+Be2G362vwtt 8Q== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a6gqdbsc7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:40 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4mZ089331; Tue, 3 Aug 2021 17:16:40 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g7tRzwtUfyvF+BD87XhuuFN3uBWq1Kd9SGdC+14PnwuaSKn2B7jpKhGOD1vtbpI3Y23xSo2r90Yr+oyWHldTaGNsC6LD7WZgM67crc+irYtJuasnZaniusj5yW3Vz0+kd++Rn2lg9svdP8uHUGM+9KJZxPz7oymquOTsCspOy7X1OaaeyDspJdVadRZnsQOrAOPll4LfeKCY2WhmvNPbIUraCvv1TsqtbND/f7wpzJU03WdPreECygWSl3reHJ3Ka4HiWFzrqG5YWTo4X+L4ESaUZpi+9IfM6TlWzO5USCRROHo8UV3n6fgIkqlUZAJljITTQsFQIImOSuRW7loGww== 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-SenderADCheck; bh=dk/+K9jub5uRxTCWF09RTLlXu15OTyiWbbzyh9erB+s=; b=RRA5ISPMM2AxOI01M8QB4zDlG3EMBNMenAyDKWm8HFvY4qDZvyFUXvOggHe9dj3d3X4cYCHLW1ilUk4kkiZTOyPdNvIYJTuGNbky/aTWuc2UHO/LKUKX62qYRxIiCjvn1Fnm66AIU7qk/25N/1GIkxKRAvvS5Pij6MLnHKBB786YU83fKiZkyXi/n4zvjljEaHyvjyznFS91CJrjxZN//1Qy8Ug9wR8XY2UiGTmZ8ookMX7DJZDx2E4c81jrJ6GUftpNN4CwR09XLy1WMtOoDdaVAUYsc/lZBLnsH8c1gRM55PTJt8UF5JS5e/6EEJkgIaT19vBtvz6ZJuvz0H9DJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dk/+K9jub5uRxTCWF09RTLlXu15OTyiWbbzyh9erB+s=; b=FCbDvoKisdVtocMlWEzjA7vbfWZt2lBpi4A+ytZLa2SxSZt69sjXrFszXEAQokT6rvf1xRh9XcoKCOcPrgWk4tFhO84iTdAtWyLVP56F2q2zqkLXoaF0rXRpHlPagCP0nIVkCpQIW3pmiCNO7DoDp/RhcbhMmYNeFcRxoWqkpFc= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:33 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:33 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 6/7] kallsyms: add /proc/kallmodsyms Date: Tue, 3 Aug 2021 18:16:07 +0100 Message-Id: <20210803171608.509163-7-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:31 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a8e891a4-5805-404f-2331-08d956a270f0 X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True 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: S0Q9A1vRHWg6M4J6nsTSnr4as503V4mYwSDqL7B/q0kYiYyWe8dN6inWykN4VhTXAeAzIwHCXTzwYCKKhIwbvOaomHFjdkT7r4nWsNg4U4IvaYYyhDkoUVGa9gLIOhQIj9tIE7GeFQE5u9Tulmo19AvY/RAQgL2oCYTghBC4g6c7aei9PNL9jSgcy4F0EHijKNZYhRn658tYKScXp4SkxOkMLHP4oOhVdmTdJfOv7ovxVcNLxnRDOFeBqSfyQaxsgvAeMJtoJHvDDOYmRvbkk3HtPHlEpLKg6n6UfV9Dasuqf7O9y4Itz1Pb/LAJ28KO7bjvh113JNmOAsIw5cq5f60WdipT6uldz4nf5nYGgzUjQ13GyqOqJ6nPfv0rfzD2SLwqKnwjsGqS+l4ZcOeUllV96ICu3LSHGwUOPPUdK5u5QjUrVyovx3lehXFWYp3QPCwnNF9RI8Rnkp8Kg0w9G8t+l+xEzPH6nG6wdmhS5hD13HC0RC8E2CbPLw8DnmL94+lak0V6DqT1FerzRHtcls9qKpeYNB8mOCvBD7Qe19Vtxf6RdyVwptvOUt0P3bgPyZGbIe3FpWry6AdgjvzhpbUW+hP2QX0rq78BtgCg0nQmxVRoffLqugv60zBSvJ3cc+eV0dKxmOx/qV2BWXSq2w== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(30864003)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GzGbplweWkypmTbkEYcHZOPDwRK6DOLKS6cqbk2AVSqv8VitUIMTO77p7Plb1rCmVlLzHgXRml4zSLlnYyVFFT2vc9mRT4W7rMSS8QqfkMYo/zCe0d+Jn5p8Lyltae9xGavxSvs1xJXR+cPOCxV+523YRxH7E4fIT+KUAdz9OMfzjCsj7zUxzA6PIL3d11zq0GnBfAROR8jWHEkIqMOX7eZxalUazl7awpzv029ry7BiyxTZdKu1NAf9GAbnEIUpX49AblSJxDotKXuDzwgVWNDJ6Nm+VEHsmaVokLeRXpd/aAjF3Hy0cSAcuSs5SFqw6G7vSgD+oxEMbV47beJq2+423VoNQfcVABgwv4mUioe0j2aGuY9LpQ1Ig1Kx0MN03XNuV5lVSxzeC2EZ7VJCM14MfB9oNUdz5NWaYIMWZihrdrgNZ/SwQq3ITuu0hor84zUpHyOHxMuKekt6gYH7r8CyLNsziZ0h8YgknX+q9dpPzGQOpH8b0FDTdQDsdR2tY1Uv33wYwUsH/b4cWActDvhK066IxurFPyR8sXF0tHinKFrenI9jdCI1XzTI4+NLlHfx8PWKP2ntQDhuwFHPpQZiUO2hgPuPwLcpzR+cMi1FGJF8z9nfYAc64AtJr6MC4h5juMLmnTxemN6dp0oAN48eklR4Ma6dGGyZl9CSKb9eVlU8RFfvm1uqskqZWIm4e8zejArj2q3w0McHaGP7kF2N9ch5NXoe8bHvQ1DAKNu6zO2H5I6QqTZuLDyt+TLE4tFkfW+NKtbkoD+lI3VPcBEobN25mQys/VonCJq8iLcK2x2fCFr63BJnwuomFGjI/qFQfmmh83Zjb0cGlLqc+lZavugZHOrruTkDgarMNfS1VlVsg9M7WO5osxEwFS86u/Zn+E0HRSstXkbNa1Pca+vHy6lArsbOazLntiw2FBLZXnBwudKMU8IEaADW1RD1ngfzyWEheMfuisu+P2zSi0FyRGvfET8DuGeg5trzD6Io6iRsprH/4gUhBRteH6wwUc/NBVj5u5ekPlKxkRRL8H7szPSfwgj0edKqFiQ1u19DNWN0kvbl3FCHBI0TjkiTSGtgYTykzfaQyYQhx3COmc24ZWsZ4BhJwxm6y9p5QhPOa3YXQYJ5nY1vaGQVKRgFnI7/ubXV87TNLAVC/YQ4g4Vog/5sD+tjfYXAKSaFS9zFuq1nvqUxkHEEzqoB5WZvyTlsgwp2Fgd6KBU+QG8foAJKAL+A644I8uAV5/ZzVGfvvW/CCyFloVxRutYTOP14ZTBh/G0CBCzjf/qIiF6tuyi19YkLkeAZPRBvLRm9c5jIBd8r9BXF/YWGjS4XYktx5rrOIHfPMxhB9hNyZXJ9pw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a8e891a4-5805-404f-2331-08d956a270f0 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:33.0396 (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: lZnTTwHfLXCH9iCij8cyYEqZdrtUAukk/jbEX71EoKIambj/aZjW1Lj5ezxdSjoPbx18DwTBebsi3IodYAHALw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-ORIG-GUID: XLDi3hen7CfORTCuvvsPnJIddjRuOb3K X-Proofpoint-GUID: XLDi3hen7CfORTCuvvsPnJIddjRuOb3K Precedence: bulk List-ID: Use the tables added in the previous commits to introduce a new /proc/kallmodsyms, in which [module names] are also given for things that *could* have been modular had they not been built in to the kernel. So symbols that are part of, say, ext4 are reported as [ext4] even if ext4 happens to be buiilt in to the kernel in this configuration. Symbols that are part of multiple modules at the same time are shown with [multiple] [module names]: consumers will have to be ready to handle such lines. Also, kernel symbols for built-in modules will be sorted by size, as usual for the core kernel, so will probably appear interspersed with other symbols that are part of different modules and non-modular always-built-in symbols, which, as usual, have no square-bracketed module denotation. This differs from /proc/kallsyms, where all symbols associated with a module will always appear in a group (and randomly ordered). The result looks like this: ffffffff8b013d20 t pt_buffer_setup_aux ffffffff8b014130 T intel_pt_interrupt ffffffff8b014250 T cpu_emergency_stop_pt ffffffff8b014280 t rapl_pmu_event_init [intel_rapl_perf] ffffffff8b0143c0 t rapl_event_update [intel_rapl_perf] ffffffff8b014480 t rapl_pmu_event_read [intel_rapl_perf] ffffffff8b014490 t rapl_cpu_offline [intel_rapl_perf] ffffffff8b014540 t __rapl_event_show [intel_rapl_perf] ffffffff8b014570 t rapl_pmu_event_stop [intel_rapl_perf] This is emitted even if intel_rapl_perf is built into the kernel (but, obviously, not if it's not in the .config at all, or is in a module that is not loaded). Further down, we see what happens when object files are reused by multiple modules, all of which are built in to the kernel: ffffffffa22b3aa0 t handle_timestamp [liquidio] ffffffffa22b3b50 t free_netbuf [liquidio] ffffffffa22b3ba0 t liquidio_ptp_settime [liquidio] ffffffffa22b3c30 t liquidio_ptp_adjfreq [liquidio] [...] ffffffffa22b9490 t lio_vf_rep_create [liquidio] ffffffffa22b96a0 t lio_vf_rep_destroy [liquidio] ffffffffa22b9810 t lio_vf_rep_modinit [liquidio] ffffffffa22b9830 t lio_vf_rep_modexit [liquidio] ffffffffa22b9850 t lio_ethtool_get_channels [liquidio] [liquidio_vf] ffffffffa22b9930 t lio_ethtool_get_ringparam [liquidio] [liquidio_vf] ffffffffa22b99d0 t lio_get_msglevel [liquidio] [liquidio_vf] ffffffffa22b99f0 t lio_vf_set_msglevel [liquidio] [liquidio_vf] ffffffffa22b9a10 t lio_get_pauseparam [liquidio] [liquidio_vf] ffffffffa22b9a40 t lio_get_ethtool_stats [liquidio] [liquidio_vf] ffffffffa22ba180 t lio_vf_get_ethtool_stats [liquidio] [liquidio_vf] ffffffffa22ba4f0 t lio_get_regs_len [liquidio] [liquidio_vf] ffffffffa22ba530 t lio_get_priv_flags [liquidio] [liquidio_vf] ffffffffa22ba550 t lio_set_priv_flags [liquidio] [liquidio_vf] ffffffffa22ba580 t lio_set_fecparam [liquidio] [liquidio_vf] ffffffffa22ba5f0 t lio_get_fecparam [liquidio] [liquidio_vf] [...] ffffffffa22cbd10 t liquidio_set_mac [liquidio_vf] ffffffffa22cbe90 t handle_timestamp [liquidio_vf] ffffffffa22cbf40 t free_netbuf [liquidio_vf] ffffffffa22cbf90 t octnet_link_status_change [liquidio_vf] ffffffffa22cbfc0 t liquidio_vxlan_port_command.constprop.0 [liquidio_vf] Like /proc/kallsyms, the output is driven by address, so keeps the curious property of /proc/kallsyms that symbols (like free_netbuf above) may appear repeatedly with different addresses: but now, unlike in /proc/kallsyms, we can see that those symbols appear repeatedly because they are *different symbols* that ultimately belong to different modules, all of which are built in to the kernel. As with /proc/kallsyms, non-root usage produces addresses that are all zero. I am not wedded to the name or format of /proc/kallmodsyms, but felt it best to split it out of /proc/kallsyms to avoid breaking existing kallsyms parsers. Another possible syntax might be to use {curly brackets} or something to denote built-in modules: it might be possible to drop /proc/kallmodsyms and make /proc/kallsyms emit things in this format. (Equally, now kallmodsyms data uses very little space, the CONFIG_KALLMODSYMS config option might be something people don't want to bother with.) Internally, this uses a new kallsyms_builtin_module_address() almost identical to kallsyms_sym_address() to get the address corresponding to a given .kallsyms_modules index, and a new get_builtin_module_idx quite similar to get_symbol_pos to determine the index in the .kallsyms_modules array that relates to a given address. Save a little time by exploiting the fact that all callers will only ever traverse this list from start to end by allowing them to pass in the previous index returned from this function as a hint: thus very few bsearches are actually needed. (In theory this could change to just walk straight down kallsyms_module_addresses/offsets and not bother bsearching at all, but doing it this way is hardly any slower and much more robust.) The display process is complicated a little by the weird format of the .kallsyms_module_names table: we have to look for multimodule entries and print them as space-separated lists of module names. Signed-off-by: Nick Alcock --- kernel/kallsyms.c | 242 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 227 insertions(+), 15 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index c851ca0ed357..daf0c763da24 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -45,8 +45,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; @@ -182,6 +192,25 @@ static inline bool cleanup_symbol_name(char *s) static inline bool cleanup_symbol_name(char *s) { return false; } #endif +#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) { @@ -285,6 +314,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. */ @@ -495,6 +572,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; }; @@ -525,6 +604,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; @@ -544,6 +625,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; @@ -558,6 +641,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); @@ -578,23 +662,53 @@ static int get_ksymbol_kprobe(struct kallsym_iter *iter) { strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); iter->exported = 0; + iter->builtin_module_names = NULL; return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, &iter->value, &iter->type, iter->name) < 0 ? 0 : 1; } /* Returns space to next name. */ -static unsigned long get_ksymbol_core(struct kallsym_iter *iter) +static unsigned long get_ksymbol_core(struct kallsym_iter *iter, int kallmodsyms) { unsigned off = iter->nameoff; - iter->module_name[0] = '\0'; + iter->exported = 0; iter->value = kallsyms_sym_address(iter->pos); iter->type = kallsyms_get_symbol_type(off); + iter->module_name[0] = '\0'; + iter->builtin_module_names = NULL; + off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + unsigned long mod_idx = (unsigned long) -1; + + if (kallsyms_module_offsets) + mod_idx = + get_builtin_module_idx(iter->value, + iter->hint_builtin_module_idx); + /* + * This is a built-in module iff the tables of built-in modules + * (address->module name mappings) and module names are known, + * and if the address was found there, and if the corresponding + * module index is nonzero. All other cases mean off the end of + * the binary or in a non-modular range in between one or more + * modules. (Also guard against a corrupt kallsyms_objfiles + * array pointing off the end of kallsyms_modules.) + */ + if (kallsyms_modules != NULL && kallsyms_module_names != NULL && + mod_idx != (unsigned long) -1 && + kallsyms_modules[mod_idx] != 0 && + kallsyms_modules[mod_idx] < kallsyms_module_names_len) + iter->builtin_module_names = + &kallsyms_module_names[kallsyms_modules[mod_idx]]; + iter->hint_builtin_module_idx = mod_idx; + } +#endif return off - iter->nameoff; } @@ -640,7 +754,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) @@ -650,7 +764,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; @@ -660,14 +774,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; } @@ -676,7 +790,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; @@ -687,23 +801,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, @@ -711,6 +869,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 @@ -746,7 +933,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 @@ -754,7 +942,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); @@ -767,6 +955,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) { @@ -777,7 +977,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. */ @@ -794,9 +994,21 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +#ifdef CONFIG_KALLMODSYMS +static const struct proc_ops kallmodsyms_proc_ops = { + .proc_open = kallmodsyms_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +#endif + static int __init kallsyms_init(void) { proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); +#ifdef CONFIG_KALLMODSYMS + proc_create("kallmodsyms", 0444, NULL, &kallmodsyms_proc_ops); +#endif return 0; } device_initcall(kallsyms_init); From patchwork Tue Aug 3 17:16:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12416933 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70B7BC4320E for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 579CA60F70 for ; Tue, 3 Aug 2021 17:17:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238099AbhHCRRO (ORCPT ); Tue, 3 Aug 2021 13:17:14 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:61058 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237839AbhHCRQz (ORCPT ); Tue, 3 Aug 2021 13:16:55 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 173H7Snf006793; Tue, 3 Aug 2021 17:16:41 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=1i/KkD9hbjXrkOyDSekf8lzdMmoeB296k9jItlyWS5k=; b=UaFon67dZ+Hpri73X2/MMzZvpX3aW34pEDsBJobPJT/TLUGAUJyPW0q4/j/DGl98XO8P kwj8MonWZ0NaYwHKU7I2vhYXNYYBmi98dGUjJ/+oaZUIZP5ljs+QTGNjKjH0W+pI+IQO WwY5Xj1exyCifwdT/EbOSqsTJ4GXeDNz01fnQz7BNfHQxlEwb7X+SLGgV4/l8Yo9D/Nw pCyz3k/o388KsLwUYq9ZYek9XjjpZTvGXw9q20O1YU/i72VPgctMF533AOvgPV+G1pbk Znr4XuB48tGgnL238ATpkZGWJv14uostwQ0b2SRqBOLmpVFmB2EjoVtOW+ApIMfOxaqb /w== 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-2020-01-29; bh=1i/KkD9hbjXrkOyDSekf8lzdMmoeB296k9jItlyWS5k=; b=HD2B+h22wWUbXNGb4lDW7n+9eiEQVWak5VvIe1VHVCcJQlZRXvhV87ZSXzqPEbyFqhe/ CtP3JjoFncjBj2kSQFbBgdYANvNkfgruU7NAs0HIj9cuy2rrmtvwG2MsWvI2F/8YIsoH WcA2Y74xTnZ+8Dr18/yzpBGf7F6WY5BNxPvCobhTh+1S7rdL1B+1mJ/zMsiNAyzO0Iga J9KMKMGBFE1LcxjdiiWZ1Z5lj3HUpC9uRf0GftlPZaLN/WMDufAM+RZiBHb8b+zDCTwG MifMx1U+4+T5VSmbdpA3rYIqjHwa+2K1hL4fOWx0x7NCQMPWO2BpXbGLyHiRm5K0J0S/ Sw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3a6cntm8r8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:41 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 173HG4ma089331; Tue, 3 Aug 2021 17:16:40 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by aserp3030.oracle.com with ESMTP id 3a78d5503w-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Aug 2021 17:16:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YzwM+dG7IgOKRkjTKvkwjCzhjqEW5kKSzA/j4q9XmEKeLcwAQWQ1CC0xXX3dYSdc2dHfRB+ahUPM+z4+N+LUNNU8oUt4Slbuo+TpgU53gAbu/15oqQhpMTf86pYT2agIjzti4olpuZTwv30Isfiyun8p1ncla94A2n1gJeVSWYa6G262MCwZV1JqBpBORVlrUGfExiuwAlLpafKnLibVaEBxVU17spEOOCZEv43fxquHeWImHJpB3aT19uLNhO2nnwisMA8Lqr3Vg58dZQ1iq4pz1xupYFcwg/9sYY+ehhLnljRsqxw3t7DlLrYQudLBh6/DhtN5g3flzLw1lG6jkA== 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-SenderADCheck; bh=1i/KkD9hbjXrkOyDSekf8lzdMmoeB296k9jItlyWS5k=; b=HbOtKPqWu41gSicfDlNfFlQRbx2mIA8apJflmvfEzlvyGcPxsj9GTmzH2mI7Gpihr6zJvsmv3+uIgbxPibf1HEA0CWafwcVBPMR/UiMhNL4HtioOS31dlgVotCbE+RZ5P2mHDJkEsfyKXxsj3ViuBCUG2nX3yoId6R1alnZRL9Ui2Ad5Sb2I9RmZDFZmyHrgK2PjES4ZPrXsC/1+fRoyL3nha4jkTDNHRfCHFfRs/w3yhotjlw6ykc/3B8o5l/Fm7rbHf5vxkM4GjmxVWVCfBaUo36wFNOVLg6BDUWMANRApjiXI77NWl4MzGhz8IZQzHdIWVODakMh39+JFbj5KRg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1i/KkD9hbjXrkOyDSekf8lzdMmoeB296k9jItlyWS5k=; b=hoa+lnMJ9knWTsHCfNI/p7A3oZ9ei5/v60zJo5jz6Mn/bFC3bq/jnU49CifGl2EVLf5UFwRifM63Thzx26Dkq65/4nzVzQPZH/B/gTuXd3UIxEVlS9BaemeqQA3RhW5Q3v+c7I89wB7M39D7Wabol6b616XCwRZ3+Yh+H7cAgEA= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 17:16:35 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::956e:3ceb:9de9:8854%3]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 17:16:35 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v4 7/7] kallsyms: add reliable symbol size info Date: Tue, 3 Aug 2021 18:16:08 +0100 Message-Id: <20210803171608.509163-8-nick.alcock@oracle.com> X-Mailer: git-send-email 2.32.0.255.gd9b1d14a2a In-Reply-To: <20210803171608.509163-1-nick.alcock@oracle.com> References: <20210803171608.509163-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 17:16:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: aa13decd-c862-4490-6c03-08d956a2721b X-MS-TrafficTypeDiagnostic: SA2PR10MB4715: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: IEztYJ7TzMYxA/wBxheAjeHCLggAL2B3QGWi+Ygw0VxKTsf0xxHQzKPgAD3kH/mcSSMKq2ycNjvHvrZoAPa9ceb0K2qdww4fQbK64GG0ye4dSBqki6GIcWXxyQw+rqUaqouk1CP2clz+L4Ckihb9IRB0gqv2+4WqcxnalXf+DCrtTpzJyr/kOF9clVWREPd7vCQ6KYn6avRWuth7q74RrUAnhCvaX1XQNIIvEixr5vWsy4pWCMa1b6eFScfkDKD7SispzWVmPp1pAuArF8sgsyRMsD31M4qhwmz4TTWLXQdYZxNXI/llKwFFWWCvAFWDB5NfXfrC5+Vbj9t0vTjW2kM85IwtbzQQflm+xbfHTgHHkDYcsJGn8FF/9H6gfSKL1SUYVFVpDbS1cD5dtI9hAfGi+9r5xHzcKlYOfCMnQhOf4rplay94nWjvepeKD79PBJdpINscvqxP08Ij32dif1oOUIhPfN53f8b3m7AGzTYrvZVI8TFZ/ivX+Y7O2IZTd3GyjviYx1nXyGBzYdkPf0CmV6EFMxDhvnzyCEiNflU0cMBx8dChKFkJKDInYi5LApCe/d5pubqVqNG71hHXAylUEoQK5XpAUttkfckrD+ufs408lWL07hHsz7OLnhccLsJTcYU92rykXrF8i2D+yw== 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:(376002)(39860400002)(366004)(346002)(396003)(136003)(2906002)(52116002)(6486002)(83380400001)(478600001)(30864003)(44832011)(1076003)(66476007)(5660300002)(8936002)(107886003)(8676002)(66556008)(66946007)(4326008)(2616005)(6506007)(36756003)(186003)(38100700002)(6666004)(86362001)(316002)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vkkfCnOtQ8EUbUxnaVBuYXGpzJcpZzDgAnT70Re8LRwNEIovikH04okDTEabbUWHhZjVut6vUUK89CQ40s0r97PJxoFj+a57vpC+5MZnZdb+lyOctW5WMEjAeAFA/Ip9A2wKVJexWoeaokWQtafctnKZGTwQs4M1s+YTaE8mPNuDolQo2qmkyj9bYRV7GEC2nYAFbN8nFJhYkVBRVHqR13WIDlgLL+dJGpIviIW2oq+xJqf7EiK60CH39DXSGvcMqgoXe1ECvUeBut0OFnZz/xMnFJWXHiMgbewxcjLci8hwKxTVAIJjpxmXOLEvhOr+Z1ZrXaSv/jkW8jdKNgk2ATh/ocm8zTfs/6VkDwiFrCdtw/re+zxE8XIaezdfV444XwNRlPH1jQgM/d+b5m2E3+dLk2Dkoc3b58o89Lhw0PgqnG7bY+d90JVJSRBx9SYR6MIZoZ9c3LeeCaB7qVe0W4zaDddQ8N2CCYCBifRqSwIP4DW9asGMrCRnMllbcfxVKGkOOtxGpJW/oxDL9hY+yXp387zKhIrUsgFqaQ7Hi/tpymQaQOfMgztNJFyKMIBMJysfDvMecJYen3xRzBlQ0jx+mKrU0brNXyC419g/oKcK+7a3TVV2wl9KOzUztK7qETA6GyNu1GyZFOonMvLaYVR+QueUS/zBZMjr20L5fPVJfvc0+zDE1oi6A8FNwzjX+l2KCLQtZdWgz6CxfNXaRmGVK8xUDlrkq1NB0/nliahjnpSq5aVW9L9kuNr6u9NsPgSFK0weFmifKdOcWnvTQiD28pbgKgMnkvEfdvm48HJeQvS5ojQVda/udQ+NVwGAIkMBjFLDvQTaQd3t+I7xRKwkT0K0H5Gt/ow6BJBwqFuCwtJ/7UVBa2wcgdaFMyJYXajeVhvxnhLVtTPgukpHjiMYx+S7t+AhGnigU45SOx3N5sw16c43DT6pWHlifmsLNNp3rq3yFwTyP0zze2JrLvBbXSdtE/IIjRVDKcSXXqZwZdr15pAr+dfkuyZAtaVV368BYQnlI9Is5rUWZIYoB535wXdUUatBe6CVXn/DfUM8XHPD7UP/VXCBNe+3U8PsBvj8C3gCT63NmYP+QCRQb9Aapx1p9rabd1NM4+zY4vlYUdCZtBdoTY3lJqNjtxcvi9CwRBARMOCd0qh45p4ip/B2CQ5Ss9GFHlLz1Q1I08IMAr7KhEyq/3S1L93J4d5AwIs+rdX9QMl0Y1aQtvXq8dq6w5hnjw6fFtYjiX2fyLxZXGv6miO9AyOQXcxAuWwd3+DRej0X8mVXXnKpItqcU6FSlR3j12R5bLdufrQliFS3zWbNB5MEchWWLrjEL4SavpacnlkVhzbCBVFFARyXmw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: aa13decd-c862-4490-6c03-08d956a2721b X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 17:16:34.9821 (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: xyKjvgLv6r7sANwWQ9FfTo94l58Q4uTN8IS1OQ3ZmQZ95RFNWeD1o/oxvIbHql25aKQUYDipd4p3/VoeLDYdEw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4715 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10065 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108030113 X-Proofpoint-ORIG-GUID: v0IHa345aq6OquZgFiUu9he55DnTirS- X-Proofpoint-GUID: v0IHa345aq6OquZgFiUu9he55DnTirS- Precedence: bulk List-ID: The existing mechanisms in get_symbol_pos to determine the end of a symbol is an inaccurate heuristic. By passing nm -S output into scripts/kallsyms.c and writing the symbol sizes to a new .kallsyms_sizes section, we can get accurate sizes and sort the symbols accordingly, reliably sorting zero-size symbols first (on the grounds that they are usually e.g. section markers, and other symbols at the same address are conceptually contained within them and should be sorted after them), then larger symbols before smaller ones (so that overlapping symbols print the containing symbol first, before its containees). We can also use this to improve aliased symbol detection. Emit the size info as an extra column in /proc/kallmodsyms (since its format is not yet set in stone), and export it to iterator consumers. The notable downside of this is that the new .kallsyms_sizes is pretty big: a PTR per symbol, so vmlinux.o grows by almost a megabyte, though it compresses pretty well, so bzImage grows by only a megabyte. I'm not sure how to reduce this (perhaps using an array with elements sized to be no larger than needed for the contents, so that almost always two-byte entries would do? except that in my test kernel two symbols are bigger than this: sme_workarea, at 400K, and __log_buf, at 100K: the latter seems often likely to be larger than 64K). A simple scheme to reduce this would be to split the sizes array into several arrays with differently-sized elements, and run-length-compress away the zero bytes -- but that's not implemented yet, and might never be if people think the whole idea of this is pointless. In the absence of a way to shrink things, this should probably be hidden behind a new config symbol if exposed at all, and kallmodsyms just shows zero sizes if it's configured out (but this is enough of an RFC that that's not yet done: possibly the benefits of this are too marginal to be worth it, even if they do let kall(mod)syms consumers distinguish symbols from padding, which was previously impossible). Signed-off-by: Nick Alcock Signed-off-by: Eugene Loh --- include/linux/module.h | 7 ++-- kernel/kallsyms.c | 74 ++++++++++++++++++++++------------------- kernel/module.c | 4 ++- scripts/kallsyms.c | 29 +++++++++++++--- scripts/link-vmlinux.sh | 7 +++- 5 files changed, 77 insertions(+), 44 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 8100bb477d86..8b5d1a7f6d9e 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -579,7 +579,8 @@ struct module *find_module(const char *name); /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if symnum out of range. */ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, - char *name, char *module_name, int *exported); + char *name, char *module_name, unsigned long *size, + int *exported); /* Look for this name: can be of form module:name. */ unsigned long module_kallsyms_lookup_name(const char *name); @@ -756,8 +757,8 @@ static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long * } static inline int module_get_kallsym(unsigned int symnum, unsigned long *value, - char *type, char *name, - char *module_name, int *exported) + char *type, char *name, char *module_name, + unsigned long *size, int *exported) { return -ERANGE; } diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index daf0c763da24..197370f5154c 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -33,6 +33,7 @@ */ extern const unsigned long kallsyms_addresses[] __weak; extern const int kallsyms_offsets[] __weak; +extern const unsigned long kallsyms_sizes[] __weak; extern const u8 kallsyms_names[] __weak; /* @@ -254,12 +255,24 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, } #endif /* CONFIG_LIVEPATCH */ +/* + * The caller passes in an address, and we return an index to the symbol -- + * potentially also size and offset information. + * But an address might map to multiple symbols because: + * - some symbols might have zero size + * - some symbols might be aliases of one another + * - some symbols might span (encompass) others + * The symbols should already be ordered so that, for a particular address, + * we first have the zero-size ones, then the biggest, then the smallest. + * So we find the index by: + * - finding the last symbol with the target address + * - backing the index up so long as both the address and size are unchanged + */ static unsigned long get_symbol_pos(unsigned long addr, unsigned long *symbolsize, unsigned long *offset) { - unsigned long symbol_start = 0, symbol_end = 0; - unsigned long i, low, high, mid; + unsigned long low, high, mid; /* This kernel should never had been booted. */ if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) @@ -280,36 +293,17 @@ static unsigned long get_symbol_pos(unsigned long addr, } /* - * Search for the first aliased symbol. Aliased - * symbols are symbols with the same address. + * Search for the first aliased symbol. */ - while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) + while (low + && kallsyms_sym_address(low-1) == kallsyms_sym_address(low) + && kallsyms_sizes[low-1] == kallsyms_sizes[low]) --low; - symbol_start = kallsyms_sym_address(low); - - /* Search for next non-aliased symbol. */ - for (i = low + 1; i < kallsyms_num_syms; i++) { - if (kallsyms_sym_address(i) > symbol_start) { - symbol_end = kallsyms_sym_address(i); - break; - } - } - - /* If we found no next symbol, we use the end of the section. */ - if (!symbol_end) { - if (is_kernel_inittext(addr)) - symbol_end = (unsigned long)_einittext; - else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) - symbol_end = (unsigned long)_end; - else - symbol_end = (unsigned long)_etext; - } - if (symbolsize) - *symbolsize = symbol_end - symbol_start; + *symbolsize = kallsyms_sizes[low]; if (offset) - *offset = addr - symbol_start; + *offset = addr - kallsyms_sym_address(low); return low; } @@ -569,6 +563,7 @@ struct kallsym_iter { loff_t pos_bpf_end; unsigned long value; unsigned int nameoff; /* If iterating in core kernel symbols. */ + unsigned long size; char type; char name[KSYM_NAME_LEN]; char module_name[MODULE_NAME_LEN]; @@ -603,7 +598,7 @@ static int get_ksymbol_mod(struct kallsym_iter *iter) int ret = module_get_kallsym(iter->pos - iter->pos_arch_end, &iter->value, &iter->type, iter->name, iter->module_name, - &iter->exported); + &iter->size, &iter->exported); iter->builtin_module_names = NULL; if (ret < 0) { @@ -676,6 +671,7 @@ static unsigned long get_ksymbol_core(struct kallsym_iter *iter, int kallmodsyms iter->exported = 0; iter->value = kallsyms_sym_address(iter->pos); + iter->size = kallsyms_sizes[iter->pos]; iter->type = kallsyms_get_symbol_type(off); iter->module_name[0] = '\0'; @@ -794,12 +790,14 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) { void *value; struct kallsym_iter *iter = m->private; + unsigned long size; /* Some debugging symbols have no name. Ignore them. */ if (!iter->name[0]) return 0; value = iter->show_value ? (void *)iter->value : NULL; + size = iter->show_value ? iter->size : 0; /* * Real module, or built-in module and /proc/kallsyms being shown. @@ -819,15 +817,15 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) * /proc/kallmodsyms, built as a module. */ if (iter->builtin_module_names == NULL) - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, + seq_printf(m, "%px %lx %c %s\t[%s]\n", value, + size, type, iter->name, iter->module_name); /* * /proc/kallmodsyms, single-module symbol. */ else if (*iter->builtin_module_names != '\0') - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, + seq_printf(m, "%px %lx %c %s\t[%s]\n", value, + size, type, iter->name, iter->builtin_module_names); /* * /proc/kallmodsyms, multimodule symbol. Formatted @@ -838,8 +836,8 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) size_t i = *(char *)(iter->builtin_module_names + 1); const char *walk = iter->builtin_module_names + 2; - seq_printf(m, "%px %c %s\t[%s]", value, - type, iter->name, walk); + seq_printf(m, "%px %lx %c %s\t[%s]", value, + size, type, iter->name, walk); while (--i > 0) { walk += strlen(walk) + 1; @@ -851,7 +849,13 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) #endif /* CONFIG_KALLMODSYMS */ seq_printf(m, "%px %c %s\t[%s]\n", value, type, iter->name, iter->module_name); - } else + /* + * Non-modular, /proc/kallmodsyms -> print size. + */ + } else if (kallmodsyms) + seq_printf(m, "%px %lx %c %s\n", value, size, + iter->type, iter->name); + else seq_printf(m, "%px %c %s\n", value, iter->type, iter->name); return 0; diff --git a/kernel/module.c b/kernel/module.c index 927d46cb8eb9..0229d91241d1 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4332,7 +4332,8 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, } int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, - char *name, char *module_name, int *exported) + char *name, char *module_name, unsigned long *size, + int *exported) { struct module *mod; @@ -4351,6 +4352,7 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); strlcpy(module_name, mod->name, MODULE_NAME_LEN); *exported = is_exported(name, *value, mod); + *size = kallsyms->symtab[symnum].st_size; preempt_enable(); return 0; } diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 93fdf0dcf587..fcb1d706809c 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,7 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: nm -n vmlinux + * Usage: nm -n -S vmlinux * | scripts/kallsyms [--all-symbols] [--absolute-percpu] * [--base-relative] [--builtin=modules_thick.builtin] * > symbols.S @@ -38,6 +38,7 @@ struct sym_entry { unsigned long long addr; + unsigned long long size; unsigned int len; unsigned int start_pos; unsigned int percpu_absolute; @@ -394,6 +395,7 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_addresses", "kallsyms_offsets", "kallsyms_relative_base", + "kallsyms_sizes", "kallsyms_num_syms", "kallsyms_num_modules", "kallsyms_names", @@ -507,10 +509,11 @@ static struct sym_entry *read_symbol(FILE *in) unsigned long long addr; unsigned int len; struct sym_entry *sym; - int rc; + int rc = 0; + unsigned long long size; - rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name); - if (rc != 3) { + rc = fscanf(in, "%llx %llx %c %499s\n", &addr, &size, &type, name); + if (rc != 4) { if (rc != EOF && fgets(name, 500, in) == NULL) fprintf(stderr, "Read error or end of file.\n"); return NULL; @@ -548,6 +551,7 @@ static struct sym_entry *read_symbol(FILE *in) sym->sym[0] = type; strcpy(sym_name(sym), name); sym->percpu_absolute = 0; + sym->size = size; return sym; } @@ -932,6 +936,11 @@ static void write_src(void) printf("\n"); } + output_label("kallsyms_sizes"); + for (i = 0; i < table_cnt; i++) + printf("\tPTR\t%#llx\n", table[i]->size); + printf("\n"); + #ifdef CONFIG_KALLMODSYMS output_kallmodsyms_modules(); output_kallmodsyms_objfiles(); @@ -1189,6 +1198,18 @@ static int compare_symbols(const void *a, const void *b) if (sa->addr < sb->addr) return -1; + /* zero-size markers before nonzero-size symbols */ + if (sa->size > 0 && sb->size == 0) + return 1; + if (sa->size == 0 && sb->size > 0) + return -1; + + /* sort by size (large size preceding symbols it encompasses) */ + if (sa->size < sb->size) + return 1; + if (sa->size > sb->size) + return -1; + /* sort by "weakness" type */ wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 00ad1b4b0055..85a2754fdb6a 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -288,7 +288,12 @@ kallsyms() fi info KSYMS ${2} - ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2} + # "nm -S" does not print symbol size when size is 0 + # Therefore use awk to regularize the data: + # - when there are only three fields, add an explicit "0" + # - when there are already four fields, pass through as is + ${NM} -n -S ${1} | ${AWK} 'NF==3 {print $1, 0, $2, $3}; NF==4' | \ + scripts/kallsyms ${kallsymopt} > ${2} } # Perform one step in kallsyms generation, including temporary linking of