From patchwork Wed Oct 27 17:47:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12587661 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0C72C433EF for ; Wed, 27 Oct 2021 17:47:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DF7F610A4 for ; Wed, 27 Oct 2021 17:47:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243326AbhJ0RuB (ORCPT ); Wed, 27 Oct 2021 13:50:01 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:19020 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238579AbhJ0RuA (ORCPT ); Wed, 27 Oct 2021 13:50:00 -0400 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RHVEZD004973; Wed, 27 Oct 2021 17:47:31 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=NOa3UXtxF8rBy0wRwDON0hJQFWHlXomEbJXPiw/4UcI=; b=UaUGHffwn2GA0hN8MZmSFKwU1e9uJShaJVmP0sJ69AJpBgfYWAZDlrLsPGrk8gI8n/es HaLuZydHnWWHWPP6VQj8J3qZHNmn5oXtaNcUjCIUAPKeXtuQaYhupUwXMQ1jezuwkPK1 GofFba6UoiApzLxySoVfyJ/9SSTFr5hEB346zj5a1/mAXW8WLoGI7kApf5ESY9g97tdt xNuYT8YhNylSZuuSMdNANx/RAUpJgpsqGB8SuudvQqHnDFLoPglwOVbwMtfuMWVulAfx YJsLjMb8BcrAE1QYgimhTn+cEufkT0/2qugIGzd/Qa2SEL7w9G2b3c2VRQvi4R0KbMjF Rg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4fj4pmh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:30 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHfIVQ128934; Wed, 27 Oct 2021 17:47:30 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2171.outbound.protection.outlook.com [104.47.59.171]) by aserp3030.oracle.com with ESMTP id 3bx4ga9wd1-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h75FwizpPZyt7IcCDAz/YHCkAoPl5310Ys3peUuZNZUq9Bxeq5EbqPrB7d3iR6QYXRZauhRUC787CI0yQQFmkw5QrJui++3Rv21ulwh4TtkndfOcMzJ6BltXwhto0QwoOjQHUvMqy7Y+mYvKs4iwUC/Eq5HW2yeRjFB64KOTSvsmu9HVU9HdYhyBMehyLKsv2TrGF2alDn71phDKXQS81eC0VRuYk44wtLpFYmt+6b9ao5YbIvR9JSQzWGZaNGRMlSt8f2gFSdQZ69zlpTsg5jUFngzzJ/GTGD/hp2Fgb9eF5tVsiGZeEd4xhQ3ms9LWKeQFeIXa0eULd4Yek9bm1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NOa3UXtxF8rBy0wRwDON0hJQFWHlXomEbJXPiw/4UcI=; b=OeVapGt5SOD+9MfmlCiLpRsCu9ULabj6eufeRYxApRehWObN1z2qHTYL4+RAMHlj/lbVSkb8cyRoFwgtjuLrs8wUX+nZR7CCopixJw3S94y4GfQW62rDGaA+ZVNEemsr2p5vtb4dJSlPYFAXHxToyd7DtbziVEVGqPT6WrnjVG2D3VhcVdDHHDhp0FEIBemay0fjR+oAHlryQ1QTZAWYJfD3m/g2Pmb9JGYdxTaVLActxMjwIYMuh8Xl1IjOcUPtkMN3M8tWNqJd/b9D9DtId7wjWpVfwy2YROk1mOn+nLuEv4LQFQ4qss14gc7cooTF9f3VbwONH5RvDZN5DRUuKg== 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=NOa3UXtxF8rBy0wRwDON0hJQFWHlXomEbJXPiw/4UcI=; b=e+4E2jRt23xy9Jb+xEEAP37jxN8X0N0+64KFo9QD5JUqgPo7MqBfOtZiQPJhwt9O3BYEepJ+1AhJImpuk981MIxkxhlQHjGYQDaFZL/0Ol3ljH+Hc2ATCSo1sFVUIKOUNKH16g87VJ+jkpEuN047zMFKOtnbV7/6F/2u2RvAvZw= 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 SN6PR10MB2925.namprd10.prod.outlook.com (2603:10b6:805:da::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.18; Wed, 27 Oct 2021 17:47:28 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47: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 v5 1/7] kbuild: bring back tristate.conf Date: Wed, 27 Oct 2021 18:47:00 +0100 Message-Id: <20211027174706.31010-2-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:27 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f25ab43e-c5cd-461b-4faa-08d99971d7e6 X-MS-TrafficTypeDiagnostic: SN6PR10MB2925: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:541; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FyC8PzfQfgolek1eSjFa4eVPuKq+bNatPNi4/y1e3OiSXL72IrPgPJfmgrgzv3BdeyffNYqedBx1YoqsFSH5VFhRmT8b1vjC1GG+hMvKhN6ZtwKMl30RS9EeXALnkFBcb7UdUu5ymApkz1gjnhzsbLe8jG81I01VdVNHz/sSvvAO0Pqp9G3pw09GQsXcq1fPKMEwTNZJhHk5UQUs5c8kQTE86zmXwZCzcl84VnYnpEr3A/VKdANxk2NdrzEyIkWHnPog5XWaRJao0dwb6bGAh+TZkTDLq6AT8UixtWgDz8hBTtfYebzs3NTUVNIV7LGR4l6IwyvVb09NRxOvbYiBVNzZzGRpzSiSuR5/xnYMQxZswFgBEVSU/d5fqE2PA6BHIzZks7O1iZvr+fCuF94dhsFb0fggTGS9/5RFRXOP7puuGpaadn3qcMe9iOioyEzTNEIerzTY+KwWbnF9/rCBnvCd0JOtbqTTHyht0jiaY7+EmfpDz+j/cl49dsIHEiTIj0KbJtS7d5P5X8GmyuRaD/odKEJ08d8qz1h3Z35Th6/mvAapmyrFrnS89aD342cVwcnEqgedo7FCV8NOAkpRnAYWhYQq5ey7sg4XTEEwgjKO92Ckxu+Sl9zUuTpvQesSH8T+B+OJnRRvyqIvL2lMWA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66476007)(5660300002)(66556008)(1076003)(186003)(66946007)(86362001)(44832011)(316002)(36756003)(38100700002)(52116002)(6512007)(107886003)(8936002)(83380400001)(6666004)(4326008)(6486002)(508600001)(6506007)(2906002)(2616005)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Odzlwc1lSjj6LF//UW5zKvEXsT2r39yFUnILNerwzWQhQN1Sh88SljToRhNGT9b+L0hpi3WmJcXtgF6GClPNNaeLqf5qlnv+j3Q40Mot07VXaTo9KHosBOuya/BDXegqwqWPfz7qWnt3vBNmNF8pXtQYjrxQhCzRUt0HahNhQE5uDaE2+X3TAYJxo99+Ouji0hXD+jQ4hGoKORiCyq61vAOIS7klFPWFNi1r7fOokHP0UPVnVztgp2D5S7vhvH5OQl4IuaYIXL1ean9fTMslUhZAfiOwDvVNEeOu5/N91TnkhYMMqbweYbfvYUCCZ2zm/E1/NjgwQGQWFBTVJ6FGAp6KclqXGFTScOJxU8tAhwjEk6eGOe55WexPY+Xci3a+hCOeOiTJZDGUJulWgdq4N3rpjV4VklD519MDNzTPRGwOTpdBqeAJ0xr3Ce8t2HfcnvZJlqizNMR7ZwpxuAzfAnn9n4fuvw8qokTbh5nhp4L8Hlm8GCoyuMkaAPzVIQUSZFgDeSaIOsIwKHu0E1epoZ67iu5peteK8kEXAZRf9GrbETHBwsG5iaWLU54jEHKZQoufijF8oH5/s3dudLJL12Rh8WWXc8Yzz6v1fRIeK5jTxdTeIpsZYfjwOOfOhn5tcwNKwmhrfnUj1Qe0RLIJpb9342P1MVL0vbAKB2/4BOGCjLF98q5lojU9RPcwzIzK3I2BS9xKtPMeJEHbDfQ4GviH3samRp1j66B2NlqmXphd0TtOH8TINKjjV5utrJHy1ctvKrJarttKnYTTz5AUB2Yh2ZJXNWdLnwQJX1hQGBJ9nmx8wZGaXeIcRwauupOdP2PBK8+UW4w3QuPnlCjtJ37lVLcS8pILQ61I7h8Yzqhhv1fKuohGeeMeINM4hpdJ5Py2V0+cdh4Hs04Lp9DxBASZ6pjGdQeOOERo1UvD0mKZ5vyv/yf2NEL9NKJhoRALv8sghQm4c8ODxsjql60QxA9BVYUQJ9LLYl55qLzLxxfqJUkv+SCvzVVQXOgo4UOssKJO9iylt7R3fnmJUtxFN6dpO+Yi6WF46ssYFk8o9DmpfJVl3+1NW39aEwaE/gO8QPWSh1VVRAuPOziBwbTkGHYYOeKDirN9bG58VIEeYAmd9lC0NpY+FCSFYIeHOab25JcJfWHHFL73vG6aJpw9cWZfQ0C1xs09aRA6ZztIM2bPZUEMqyR/X6CYrWsPB0TdonaQXjx1tCFloI6iJ/NOVTKV66CK6APJ/uc4PO3CnATLQ81siYT6/edOHd/owmcgJZD4Ue0tJd6VFliYMLXiQa/wyaZjwmL0nZ3IweGFO9KKKpLs9fcvS1NbBt/Iyj/Si+iHm/+cuVb+wy+SSXvGgV3zo3LgjKKnx95JN52lP+STs7ceJXDhLm6i0UJAK7lyspsfCeJfSd/WDGeHZ3tWsv+x7Nsd59IBdwGctwrpL0/aZbzQ1ly+KJK94FiXOJQJOwwTc+MWYmbzN1zv1fNZewhCuOBK6dZ2D9x15fFtbRKfO5UM7SxZZEkVMBEDZYPfB0+5OBfGTYZ4rw6PwYEJiXm7Y/5Zvfvt1KG9zi41go9Qobn9e4oUJztlM1CM1jhehZAOltkv5L46cVwGMysJCD/38Y5uyr9UxkTHxhxfrRFl7rlIw29PAGtmXEHHbyll X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f25ab43e-c5cd-461b-4faa-08d99971d7e6 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:28.6555 (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: e9JmqEZVP6jSF+HhddCzW2n9Ro5mvdKYCuciD9v2VaLMBWhX48HDAdY3jKiWlx9zHyzeH+EzhgqbVO9R167nmw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2925 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 adultscore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-GUID: 0iV0RnlfvAHWDKY4F7NFY8JTDByuUZwO X-Proofpoint-ORIG-GUID: 0iV0RnlfvAHWDKY4F7NFY8JTDByuUZwO 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 --- Makefile | 2 +- scripts/kconfig/confdata.c | 41 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 437ccc66a1c2..77062b44a4b5 100644 --- a/Makefile +++ b/Makefile @@ -724,7 +724,7 @@ $(KCONFIG_CONFIG): # # Do not use $(call cmd,...) here. That would suppress prompts from syncconfig, # so you cannot notice that Kconfig is waiting for the user input. -%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG) +%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/tristate.conf: $(KCONFIG_CONFIG) $(Q)$(kecho) " SYNC $@" $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig else # !may-sync-config diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index 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 Wed Oct 27 17:47:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12587663 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC065C433FE for ; Wed, 27 Oct 2021 17:47:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BEBC260EB4 for ; Wed, 27 Oct 2021 17:47:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243395AbhJ0RuJ (ORCPT ); Wed, 27 Oct 2021 13:50:09 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:25280 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243375AbhJ0RuD (ORCPT ); Wed, 27 Oct 2021 13:50:03 -0400 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RHVEZE004973; Wed, 27 Oct 2021 17:47:34 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=B5os/ALLZ3fEQlkrlXWMgkJ1BX3RBm/OcNXMhTN5u1Q=; b=TCyH7RRipclNFWLj7lUUoL3oxRNkoyJ0Cz8G7cSdjJZZ4aLf8+15tJ7XwkfY0AOTTsFf lV0IxZaoC00gTshprBnercqjyp5tPfjvgn+Wu1NK8th/cwe28Ry5xVz3aaqr6wkYqoYJ PcHCUCR1EiIYfXvvlWG5eL6/4diaqMpt7h+W5gcaQpNOxyv+/B3jKoZpPaR53faaU/dw Ss0YDLKlNUchuckyjSi2Jm4af2fwYy36gJu/Bcmm/16veVgboRXSIyoFe9TJKnHVsysA NXKH+4OwcUbxHdeodZ1r2K31kTvOSUL+XWOEX8AIL3dty33JRwTXKEUVKy/a7tgUZTdK wQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4fj4pmp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:34 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHg13n049457; Wed, 27 Oct 2021 17:47:33 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2173.outbound.protection.outlook.com [104.47.59.173]) by userp3030.oracle.com with ESMTP id 3bx4h2ruat-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ErBeXfMai58U/gmXuX+6jjAvn1MXmBnZHzGCwF0NDBWVEdPqXLDU2Ovf0wZS3SsiPcSKoSay+Yqtm11DcbNG4otdARTHklkSQIgxDcqr3Dxe0INHUmKpP+KDi//E8rYylw5t1jK1nvTQJL+kUgTpHLM5CGdx4wX9QoqkzeQM1S25nPQ8ZM7l1VzRsypuLzNOYVXYHmIJeBHcMafe6YIRvZYDC0zaTwPNwDpOCRMTRERRA0zdLCO15QKzUd1ptGXSVkCNuunI/iPHG44j4viwYCi2PA479iniZqN2Ehlk/QQqUSFMVA9sUVqm9k8+OTfuVn9KbS6MGGudf7FO9ibSkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=B5os/ALLZ3fEQlkrlXWMgkJ1BX3RBm/OcNXMhTN5u1Q=; b=LPD02lIcV4qumdDbZKY0ewyrW9xcvxcs9R8jXx329Qn3fz056uSu3EWbGcsA4pJImb+6uRBx/D/qBz7lJF26KSpES58js0Ghf57/YfZr2nYXSq0aVCZqgoRI8saGjFVxGRLT/tih+d/H9nQnflegeo/AstvjBPHJCrAXXCmdroU2MmKTxw9nesGDk9UFSM87yPMTypzZg//3pPaT907/3pc2RmzgWOkqhQvc1tKNAeQHtJ8AjsDxpwIIq85UTtLtdwJc/SCxrSZfvuFurMRrMnHwtxw+VrWFsPPGyfgCXySEJyNcm/LWRx5VvB8lZYYAZY7XJom+TCLECLYOuXliEw== 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=B5os/ALLZ3fEQlkrlXWMgkJ1BX3RBm/OcNXMhTN5u1Q=; b=TqtqBrSMF7yzPJ/frBElT9TrgeAWHwyq53ZkZp7kS45Qe7AhXAwVLMfuTPAQ8rD1QejLUHWXI+yr6406UENJW7AvZ2p0CV85x8paj1kwITenQekYYdLac5SZ5u1alGL0rvOfkXpWeWG5E++vbhMBeCXXxojmkSphzeko1GyHKuc= 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 SN6PR10MB2925.namprd10.prod.outlook.com (2603:10b6:805:da::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.18; Wed, 27 Oct 2021 17:47:30 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47:30 +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 v5 2/7] kbuild: add modules_thick.builtin Date: Wed, 27 Oct 2021 18:47:01 +0100 Message-Id: <20211027174706.31010-3-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:28 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: af658b01-305b-4201-0323-08d99971d903 X-MS-TrafficTypeDiagnostic: SN6PR10MB2925: 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: lmjSjluGFTr7MU74JJjULPfuMs67WlQQRD0dFwCZ9Em5tkOYGz1blhI9jbdGChWzOFwBssPyb5T0ifhjtEtnkiGHWLQydYiU6+rt5Xf6D+YoTehGxAf0L4uM1NEggNxy4Y8Upl9ktAlw49ykA0yXIzVIEQQ0d0XWObkoEQVA+ZPs3hXZa3ZauyymFqhfFMcZLTW1ntm7JOW/603s7DOlvILhGqh68lHWHfO+DlQ/PUROUAl4jrDpLuu3PvchPEVwbIa0gmY1s6mAr9duO1u658GC0zsOJkwszv3nd3fD5csVpjXCo0hew/G0kp8PH2wz6lbvqrlq4nDf0Af44O8djaqfwGLqywDsuWMWF7PhQdNhDJLB1RglfgQblwfFWd3Ft1XBGF24aTrsVwUWO6Cfc1UXFV4EXOwcuO1DcRdr8vjaMYeEMJB+ax6KRGXP6SQAzVmwmbrZHKTyGCwAoChHt4MmzzugXcGRAZgFu+e2cRRNad4njsiDJglypJa+oQZHsCKak0jTPZmAEkZ/VzDd+xzZ0Am75yx0+eKBSYcLfUEK5XJ+2V7jJEtw01guc0U3YKBJaAPb2p2TkYpwMHTWjlpU6lB5MWCO70XvcLQxjc0hyCodAMO+D3FRtz40H8r7NLY86YO2QbGWFzoxS0+kFLQkP8geSxqsk+XrGCtu1pP+qGqHSVsD2cqA7hPxYx3/ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66476007)(5660300002)(66556008)(1076003)(186003)(66946007)(86362001)(44832011)(316002)(36756003)(38100700002)(52116002)(6512007)(107886003)(8936002)(83380400001)(6666004)(30864003)(4326008)(6486002)(508600001)(6506007)(2906002)(2616005)(8676002)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DPvDX+wEpN/Crvi2b1NPVEB4jWkSp0uxP3cWqKLsSIx3wJ/1cGlJIsN2jsIDViFj/WYcWf62FJgWDUNjOwqNuIWy1M01Ddm3ivlTKi5RXZ13OJcrgDhQFyM7pfF4N8V8RNvPflpNwgk0sGf8liQFlMOPMwqMejiwnFN3XlBgbkcbrZ6hfPLRGeNZ6qrX7o1U59+KigDDx/RcWV7NoXEUh7WYR/D1TU1qijTLMH7EFC/Q920w8gIO+AhSqoJHXA94ktTWMHHyUkvdGsuU+bQFiXLBhIZKlP+rfjOk6AEUVJEtTZErxR/YMIP7wU2NRpX1EGhVqZjCydH9qdRgF+hVHENFkEPYwNrz7N68fIJVuhPPIw7pz5XUyfzI5069lDbu/Pr9woDyNxsx4w7CoFW8NVLhdsL+ClzHO1Spwskp4CcwDlScHSbbySF55VfDLaBKmehCAs0aBa3XvjOQ1S8Q/8m0N5W2jYNtL0nntJKzW9F8NK1NfJnQPAjdipk2XHSPecjKV6cX8GvabMxdGWRKRVr9atsMo8VPgDpXbgf3PGYoW1Q2Tcx1ONTATT97wiGjfI/q+h7XDznN70pW9zPhYiIH6uMzadwL4oA7vLbA1aykIrLUgrOqpKJUjj2TNY66j4CR1Tht+SEt7+TCjpAS8S5fe/ZGHXB3QM2kwjPm49uenlmzSoESAOmyspFdz1dgUwKUCvhZ4mEpwRJ881Tcc6qQMhJefWrJSSPmiGECgSZXlF18E3fx9SUn/RM5b8KTv+DO3bARYAMKLSB2xavrfKFvI0CEoacOQxyG2+3KQSQTmvMhwHvht+izRUdH0VXjK9HZtVxx5lOq1A9NJ27XKbALqmKbxiySywaODNU2aJ6kwVTLap0pJBa0hY6ikMvCD090Y9RRj+OTSgDfD5mz3fJfk0vjOCDm320+7YDCuXNF9CtDvfmOgt9NWPilMAVEnhmQU0e/MpvR2FlLxOOmWiOQON44f7c2mWMb2xqV5iLjPgcpA9ou7Kuzw/dFFPi8dnmfQdmGMBOdGkLLzAgTUoMiN5Fcklo3Z8UyZ1nuYaC0OVjex8lRdth3gH5eIpWrr9eHh09szotcpkd69+savrno0n5X78jUJq+SePAQcVc9jGZYH1CF5EtjJP/KOPJ9TXoSEvL4dQYwMDBmILHWlKWfdCv5k/eQNKMqATD9u74AHzvv8BOkgAgizoL5VFs1uiYc59mMN2sB9p1OmHD8KROLCE3lorEqCaOD4jguti6VyngGn24rR9D9m8F4sa2oNROOwWzGNN41nzO5c7TXxw02MytI+6JZWMFFBWTavoXnF7tuS5eMA/8RnIMoEQWZd1BzI19hFVIulHaXXLJUGDtyYlVKS84u7XOikxwzOMFYjtqOeRwiFhHp2d5ZC01/LYjKradEhIZ6BCSwXE7CDfO53+wDU4UcsexXba3EgFE9OcuiVNIKDIu+dbphTMwNxFW5TvgMi7cg7u6e0DWtQmsxZ5yc4Epd0k+lw9eNp+PvQp+M5ldAhHkRbLc1G9x+eI+fV+v2dxxWoPcIyQ3WjxM5zgYq1CrtSsEp7icWKMkG/djf/rQTyaqHLPOYCY2qmqjYKaa6CYgN921Um6d+QxfUcBgAtbshsFO4lA0PpvvRPR6s84o1e/mvd1eX7PPF X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: af658b01-305b-4201-0323-08d99971d903 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:30.2018 (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: wPR/g4+g13gkJdd8iuyKKsLBKuCpTok6jifhf+f2o7d+EkJnnuHdKO14f8l1yL+t98MjUYYuXwpfmhcFzq3yNA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2925 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 suspectscore=0 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-GUID: CzaSKRroAFaluN5LpTRBnuLi8a-zqSL3 X-Proofpoint-ORIG-GUID: CzaSKRroAFaluN5LpTRBnuLi8a-zqSL3 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 77062b44a4b5..880b3e67a47f 100644 --- a/Makefile +++ b/Makefile @@ -1507,6 +1507,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 @@ -1886,7 +1903,7 @@ clean: $(clean-dirs) -o -name '*.lex.c' -o -name '*.tab.[ch]' \ -o -name '*.asn1.[ch]' \ -o -name '*.symtypes' -o -name 'modules.order' \ - -o -name '.tmp_*.o.*' \ + -o -name '.tmp_*.o.*' -o -name modules_thick.builtin \ -o -name '*.c.[012]*.*' \ -o -name '*.ll' \ -o -name '*.gcno' \ diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index cdec22088423..ef68635831b0 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -74,6 +74,12 @@ endef # $(Q)$(MAKE) $(build)=dir build := -f $(srctree)/scripts/Makefile.build obj +### +# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= +# Usage: +# $(Q)$(MAKE) $(modbuiltin)=dir +modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj + ### # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= # Usage: diff --git a/scripts/Makefile.modbuiltin b/scripts/Makefile.modbuiltin new file mode 100644 index 000000000000..a27b692ea795 --- /dev/null +++ b/scripts/Makefile.modbuiltin @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0 +# ========================================================================== +# Generating modules_thick.builtin +# ========================================================================== + +src := $(obj) + +PHONY := __modbuiltin +__modbuiltin: + +include include/config/auto.conf +# tristate.conf sets tristate variables to uppercase 'Y' or 'M' +# That way, we get the list of built-in modules in obj-Y +include include/config/tristate.conf + +include scripts/Kbuild.include + +ifdef building_out_of_srctree +# Create output directory if not already present +_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj)) +endif + +# The filename Kbuild has precedence over Makefile +kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) +kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile) +include $(kbuild-file) + +include scripts/Makefile.lib + +modthickbuiltin-subdirs := $(patsubst %,%/modules_thick.builtin, $(subdir-ym)) +modthickbuiltin-target := $(obj)/modules_thick.builtin + +__modbuiltin: $(obj)/$(builtin-file) $(subdir-ym) + @: + +$(modthickbuiltin-target): $(subdir-ym) FORCE + $(Q) rm -f $@ + $(Q) $(foreach mod-o, $(filter %.o,$(obj-Y)),\ + printf "%s:" $(addprefix $(obj)/,$(mod-o)) >> $@; \ + printf " %s" $(sort $(strip $(addprefix $(obj)/,$($(mod-o:.o=-objs)) \ + $($(mod-o:.o=-y)) $($(mod-o:.o=-Y))))) >> $@; \ + printf "\n" >> $@; ) \ + cat /dev/null $(modthickbuiltin-subdirs) >> $@; + +PHONY += FORCE + +FORCE: + +# Descending +# --------------------------------------------------------------------------- + +PHONY += $(subdir-ym) +$(subdir-ym): + $(Q)$(MAKE) $(modbuiltin)=$@ builtin-file=$(builtin-file) + +.PHONY: $(PHONY) diff --git a/scripts/modules_thick.c b/scripts/modules_thick.c new file mode 100644 index 000000000000..9a15e99c1330 --- /dev/null +++ b/scripts/modules_thick.c @@ -0,0 +1,200 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include + +#include "modules_thick.h" + +/* + * Read a modules_thick.builtin file and translate it into a stream of + * name / module-name pairs. + */ + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file) +{ + struct modules_thick_iter *i; + + i = calloc(1, sizeof(struct modules_thick_iter)); + if (i == NULL) + return NULL; + + i->f = fopen(modules_thick_file, "r"); + + if (i->f == NULL) { + fprintf(stderr, "Cannot open builtin module file %s: %s\n", + modules_thick_file, strerror(errno)); + return NULL; + } + + return i; +} + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name) +{ + size_t npaths = 1; + char **module_paths; + char *last_slash; + char *last_dot; + char *trailing_linefeed; + char *object_name = i->line; + char *dash; + int composite = 0; + + /* + * Read in all module entries, computing the suffixless, pathless name + * of the module and building the next arrayful of object file names for + * return. + * + * Modules can consist of multiple files: in this case, the portion + * before the colon is the path to the module (as before): the portion + * after the colon is a space-separated list of files that should be + * considered part of this module. In this case, the portion before the + * name is an "object file" that does not actually exist: it is merged + * into built-in.a without ever being written out. + * + * All module names have - translated to _, to match what is done to the + * names of the same things when built as modules. + */ + + /* + * Reinvocation of exhausted iterator. Return NULL, once. + */ +retry: + if (getline(&i->line, &i->line_size, i->f) < 0) { + if (ferror(i->f)) { + fprintf(stderr, "Error reading from modules_thick file:" + " %s\n", strerror(errno)); + exit(1); + } + rewind(i->f); + return NULL; + } + + if (i->line[0] == '\0') + goto retry; + + /* + * Slice the line in two at the colon, if any. If there is anything + * past the ': ', this is a composite module. (We allow for no colon + * for robustness, even though one should always be present.) + */ + if (strchr(i->line, ':') != NULL) { + char *name_start; + + object_name = strchr(i->line, ':'); + *object_name = '\0'; + object_name++; + name_start = object_name + strspn(object_name, " \n"); + if (*name_start != '\0') { + composite = 1; + object_name = name_start; + } + } + + /* + * Figure out the module name. + */ + last_slash = strrchr(i->line, '/'); + last_slash = (!last_slash) ? i->line : + last_slash + 1; + free(*module_name); + *module_name = strdup(last_slash); + dash = *module_name; + + while (dash != NULL) { + dash = strchr(dash, '-'); + if (dash != NULL) + *dash = '_'; + } + + last_dot = strrchr(*module_name, '.'); + if (last_dot != NULL) + *last_dot = '\0'; + + trailing_linefeed = strchr(object_name, '\n'); + if (trailing_linefeed != NULL) + *trailing_linefeed = '\0'; + + /* + * Multifile separator? Object file names explicitly stated: + * slice them up and shuffle them in. + * + * The array size may be an overestimate if any object file + * names start or end with spaces (very unlikely) but cannot be + * an underestimate. (Check for it anyway.) + */ + if (composite) { + char *one_object; + + for (npaths = 0, one_object = object_name; + one_object != NULL; + npaths++, one_object = strchr(one_object + 1, ' ')); + } + + module_paths = malloc((npaths + 1) * sizeof(char *)); + if (!module_paths) { + fprintf(stderr, "%s: out of memory on module %s\n", __func__, + *module_name); + exit(1); + } + + if (composite) { + char *one_object; + size_t i = 0; + + while ((one_object = strsep(&object_name, " ")) != NULL) { + if (i >= npaths) { + fprintf(stderr, "%s: num_objs overflow on module " + "%s: this is a bug.\n", __func__, + *module_name); + exit(1); + } + + module_paths[i++] = one_object; + } + } else + module_paths[0] = i->line; /* untransformed module name */ + + module_paths[npaths] = NULL; + + return module_paths; +} + +/* + * Free an iterator. Can be called while iteration is underway, so even + * state that is freed at the end of iteration must be freed here too. + */ +void +modules_thick_iter_free(struct modules_thick_iter *i) +{ + if (i == NULL) + return; + fclose(i->f); + free(i->line); + free(i); +} diff --git a/scripts/modules_thick.h b/scripts/modules_thick.h new file mode 100644 index 000000000000..f5edcaf9550c --- /dev/null +++ b/scripts/modules_thick.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef _LINUX_MODULES_THICK_H +#define _LINUX_MODULES_THICK_H + +#include +#include + +/* + * modules_thick.builtin iteration state. + */ +struct modules_thick_iter { + FILE *f; + char *line; + size_t line_size; +}; + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file); + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name); + +void +modules_thick_iter_free(struct modules_thick_iter *i); + +#endif From patchwork Wed Oct 27 17:47: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: 12587665 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46E85C433FE for ; Wed, 27 Oct 2021 17:47:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 285426112E for ; Wed, 27 Oct 2021 17:47:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243386AbhJ0RuJ (ORCPT ); Wed, 27 Oct 2021 13:50:09 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:11382 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243382AbhJ0RuE (ORCPT ); Wed, 27 Oct 2021 13:50:04 -0400 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RGaHaF023023; Wed, 27 Oct 2021 17:47:35 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=4GJ0zwXvo94iuocSxhRRma7AXGekiaAKM5zlJ5h9obg=; b=axCflIaQpBiR5n37f0OIGDKdrcVlQDow+1QQxu5MO9BOuiNESPTV+mQYWSgpGfQeXSTN ZzhfN5OMVz0islkJrkcPI2AifoWj1jLfN9rIWvRxZVNIeaBzsb0UMF4vACXWCKyOMIGT fladmJXcNs+r0ucuu0adVgUhUk1S8aIRpAzALPBYkfj86qo5FhKqpSqjnhTomRzE1gFS RjIhKhlIjvnHPYSgbsPTiG2k1quuIsGWn9VN3m4691BqOdKtE9ZtWIxEE9oRs64449WZ xm9yGpeXMFrowYd2LDhaUVKqUoNNJWstF7lv1IHYL6227p5MGbO3jrjQM6AO9jsztnNV tQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4g1baqy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:35 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHg13o049457; Wed, 27 Oct 2021 17:47:33 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2173.outbound.protection.outlook.com [104.47.59.173]) by userp3030.oracle.com with ESMTP id 3bx4h2ruat-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YxtpPiRWRZ2UDOXc56RER0Gnte+UuytmRTfQQc+ydSjB9kIsJ492KQ+kOUMGfSAym6FDR6OFg+wSP3by8mLphVTqOIKbZ1FOaYBKMjg3l5QjNYkCXBAaV/AvEPUCv3akBm5CAEsCbK/6cZEeNiNRc+42Q2D4bb/pavUEZW5x2oLvs/8SE1KQDjxW5lgvlN+fUM9K2LSK5+nXOJxbVv8Aqt5myBsGFtiNHyd2yDJSjub2AWfJ279K6mx06ugQQf7E4Wj5haAQqbI5TUApSvdjNabfAeQvj6p+sh6G9p0QhI4pDnEoFcqKMJL2qUJAoxxA4Gvl2CEBnuLlan2hICENFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4GJ0zwXvo94iuocSxhRRma7AXGekiaAKM5zlJ5h9obg=; b=WmCHZSYlrXGzgq55yN7rzglfpm0/UKd+ZP4ehMAMFQ/V2YfiI/M+pmrUPM1Kd6PM0Wv5fj6IScQhH88LQh221OpCFAoGW17qAS7M+EKZzh71xwbYJZGqJmku1kWX45wXCmpAf+mW1Y3WDSZaM2A0H6NB0yfwPA8Ocja4bwfjIfw81a2fQG8hyiZvBnTMsDRQzfbyOVloxfHBzKSot/ja2SARv8Hw1n40IkbiVGjz+Tu+1bvliT//ve/CV1U7kUA569Gq1VNR4paEAtTdcMNtjV8pxfSHip90HcLhON1F0FRUmy2pTxksIETIQW7mozv6D59eQ23hZ4UNFS3UAMQ9wA== 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=4GJ0zwXvo94iuocSxhRRma7AXGekiaAKM5zlJ5h9obg=; b=imXFNIMX9ZbrMmJw6WhCvFix274uhJWk6YIJcbAVUhQa7WFP/GUIfEVccZLu90+nsoW4A1Y3zGIec6Y7gbxeIYzi3UgY+B2YtvIGJcSYPubjLoj2KtN3fTIxiKVnLfCzGnVgzZrp5aUSsj89W9Fp8omvDIatVEnd6jXEx3lB4XI= 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 SN6PR10MB2925.namprd10.prod.outlook.com (2603:10b6:805:da::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.18; Wed, 27 Oct 2021 17:47:32 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47:32 +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 v5 3/7] kbuild: generate an address ranges map at vmlinux link time Date: Wed, 27 Oct 2021 18:47:02 +0100 Message-Id: <20211027174706.31010-4-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ea1de9d8-23ba-490c-a1fd-08d99971da01 X-MS-TrafficTypeDiagnostic: SN6PR10MB2925: 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: tbIVGRhra8bLY/B+TQX91QjpyJtRpiwNOs0uAgl+BrR6aJIDMVc8Dc5OZkRn+6o3sgO/3ec2BbRNMbfgpKSJGRozSUbI5euGwQd7+nOPVbWbghlGZp9HJjNq8njAwOyumea3lb7lGu/Ia53k4lk12vjrYfbTv8pqd1lVaj4bhuVrqqlZhAzl+wJ37Ji8HsSbBbHw4MjHJvi6KKxJnNECu4U8YvEeY8gZcR4c1MjyOfZ/MRGv+ce4/+bxNaOauxKXQGUZxWW3LJmj2odLbIAw9PlAgroVt3rdFdnGaNt2Afv0tKyyLYxt3uX0JFldjh09GxUy0KAmDtIFISC7CiK1uuN5Amh/dws72JvHfo+fnVI48S4oo1JqZPBTS76hb/FTHsVs71ZxL1X3x3gbyQum4xUYdUcNzF7nD/3Ynph23qklE+bESM1l5FH7g/+lS1NAozxBh1+1c+DmCjhaS83K6TBJh18rrRQkjScV6V3l6byx2OPxAgEMRV0Jd0DnJrsPOTB5e0Ip0MpSP1oZwK1qhueFcg+KdYVghLgiEI7kcDtsppvIUgWi5GgNj1ydFSSPRIUDShm0sCSX1H0ox8dAXYWcDbcUHIlDA5qWIUVchIYkIMCvDqlnXztMjWHtzxWuOIQhm2oQSC2Ym/Wb/ik6rg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66476007)(5660300002)(66556008)(1076003)(186003)(66946007)(86362001)(44832011)(316002)(36756003)(38100700002)(52116002)(6512007)(107886003)(8936002)(83380400001)(6666004)(4326008)(6486002)(508600001)(6506007)(2906002)(2616005)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AP5UgVGRowKJF9lfs9ptikJs8ZjoeqqIRO5OzKqeq65AtO1rVTz6+kqKcQPEEVuNEPTAZuOtVIaz12IUPR2eTg4n+JbBo5gnaxgcHkbcr3Xy3G4Ck2iwkGV+VFXtiVEVSYacZY9Ifn4yIEscbDb+6+HHPgNgMfORzfKV1hxG7StE09PMZVBd2xwkN/9gWgHSDLE8/KEddh+FHOxyinaZJ3SvOJ4AF/8MKNYRxS1/t2sj5f9m590Jo4H7b60av7bV1wm+ssBc5Zqf2pAITyLvewvfQX2vVbGwpZPtdU0zH2K5pEDf8ymBW1huixaAJbUNxfsGLloBVb3/Yu3g6fF6aS+WZC2Aptbn4anPmukgpxcicJZErvnD9Ejxd5R2Q+oiNp6NaOLhBu4YWn61h20g11BMqt3ctO33agxtH0cDokXJccBISICW1PAwctMIcrBYVuI5nztCTWbZd80p8TA0m3w8SPq1rljXvkwtstPednix0K9IdtRWzMMauDk1MdO/LOQ5rCEaPdR5ySFWud3aOT6sut9Flp1oj8TGFoCgqVysqOYe/+4ZFbpJYdVsHOOkl90N2+2vq21CvGMd1Qg02dsaUBGezBVTvtJbo1wHNrS/kcjiitegoOht3qgDAtkEbWl7613jIUqqzvv3SZKMlW30GlYlfztMRfJUM4qgppMqFvclm7JnrafMohNnbmR6bTEe+Tb56/wbbQejD9EgcQrs8xyvs60OVSBt/UnGKXrrLr7IbgYW3RoMChUB9hkbB218dMIvui7UVUHhp+rBtr+ACAm/xwq1u09EF7q9Ylo16mUqthHlF9bW4Bb9H7/9Xr2vKLyGxync7DabPB5WuWJE0LJMA+sGUx9ihVP3qC8iMe/KPgbNRVGUvADKH96KAdrq8RnX8Uds0B7F7QmGHxQNfMjYFEjUiXyErNTQrqvtdQ06n7ikjxUuwT4Ta0L/gjsEGbvWax8VCcbAlqEwOWID7/jKneAthmt5r9ljDoFrdZZp/zYab+NpOf3GiBriKRvCzk5dudHleFb9v7avyAosEKwZkyCKD2l8xHaS1swFSgu36Nry4kYQKua29uKUyMG7stDz2bp5sA+8eanKgfvfnDwzJYolYrZj7Ja8lOpqwRNtKGurm1wPkHi9N4pAwSXMeSkwkw8wRPevZEA3dDM1fqyW1PVQ4YC/Ft8hh1YdrsPEEcYKTUNUw+rcbFJrxKJ0yV3nMXDzdet8xn5gvhqbDcI7LB6s94Pe8SWVhG1LdPa1BK6GZ6z85u5+AqcezDlPON9QwbENZnb5jN9yOsim7wsWjpen1RLECdX83ku2l58k8b0ca6+kVxGsDk4TquqbU5ntruqHJtEUGDlyvdXE7Km7sFN8RpC33n34I+kXrDZC+Sw5oGUo285MpYCT1j2VGxNv/NLQyCINpbCZbFlo0hhsxxqqtLpvZ9t2i1LPMjSKo2fv+GYpxzmIy0EhC0CpB0JUCX0QzFIpRX2RnF/jggx2ANb7R4GeseRIZhBCSCuX6UW6xmhiVXjEnyRrSEYL9tjBB/RAIxb9fadlPayUWoobg0yCHAGSSieit5V4fkDdQ0z6eLHjnFp680HOTnr11ri1QNe/zSTDwQZW087sNPMl5hFo5ivTxaJmog9CSOooJ8FZLwYD6MozqGye X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ea1de9d8-23ba-490c-a1fd-08d99971da01 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:31.9033 (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: 3b9mNxm1Cq74OTlMcZQ2mZbzbSrDbUDfLbS1yCM19k3seo86PFaDQyrZIGr4YI0vPC6BLQykHUK/X1OtZpIfGQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2925 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 suspectscore=0 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-ORIG-GUID: BPDkYy5TwAffcdr3pLK0F5ebZJeqlhtO X-Proofpoint-GUID: BPDkYy5TwAffcdr3pLK0F5ebZJeqlhtO 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 Reported-by: kernel test robot --- scripts/link-vmlinux.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index d74cee5c4326..a30075c14a25 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -196,7 +196,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - $@ ${ldlibs} + -Map=.tmp_vmlinux.map $@ ${ldlibs} } # generate .BTF typeinfo from DWARF debuginfo @@ -248,6 +248,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 Wed Oct 27 17:47: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: 12587667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11EB0C433F5 for ; Wed, 27 Oct 2021 17:47:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EBE25610A0 for ; Wed, 27 Oct 2021 17:47:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243415AbhJ0RuN (ORCPT ); Wed, 27 Oct 2021 13:50:13 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:34022 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243391AbhJ0RuI (ORCPT ); Wed, 27 Oct 2021 13:50:08 -0400 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RGVFrK017554; Wed, 27 Oct 2021 17:47:39 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=5aZJaUdVlRqLRMmExFcUz2pSHzaGMhQRM3UreJKADGI=; b=dwW7vqSV3Ry1ZLAYG8pBLsdDMHiUQqkcA/Ufj6M/+0fGdaKT0sQAOlRIfH5xvNNAHQcI 3y4fKQxRvFPs3eXjcgv9Fgv+ArEZ4cPqExpoMqMjXYRfCv2EjjXcBfnMyZSAr/fUw2CK cv+3u4Ugoy1T8+7bBEf62AuIqW5CPnFYyXHQYjuVELLvS6OBUnrv54xfQUvn2pnoi7WO l2/rpy4m4Ls6CjSAoCA2F6DtAO4nr7xFly8znqnIBvFO2Bnjh+alIN9yWot2k2acG2cE 6CSVY2f26fUA/cz63OMYhmyRokE3hGjl1n8/jmYWd1udcJKg8O/6EQI2gBJra2LwAqix 9g== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4fg3srp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:39 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHfvQB056661; Wed, 27 Oct 2021 17:47:38 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2168.outbound.protection.outlook.com [104.47.59.168]) by userp3020.oracle.com with ESMTP id 3bx4grfju7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e0yNsyBWSQOebHjg3jeDH+iD3PPO5mVnLQcr+hNOXoQg/jSCLSUOfrooN6edRnvjezvihw5Ia12p8zmZuybqnnTwACTPC4SaF/+nJvttmt2cuty5sESzrV/H/5SdEAF+W5IJnEZDgqee6QvtZF4YS45WodcO3PTI9Xve1lZ6qcQbA8Xv94Rmym7fbn8wPVjDw7pFHymf2G/kCBNEj0dJjubLEkr04GeTDJfZjL8F/7uEkR6ulQKPrIccpinEVv5e3yj2zDOgARrD2JALqQyk7MXn7Bxc/7mfXrbKL5iF9cQL+L9hZvUM0W2gWmgAMfdYwee8JDWYxLJkkZmPdsXOfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5aZJaUdVlRqLRMmExFcUz2pSHzaGMhQRM3UreJKADGI=; b=D2vw7BcLOn7BAGlvdt+cJyleYOIfe7au1DDulFuuR/nznOC3PHnK1G2O/+gqawlJWClv2pDayXbWkXzrHWsNjLmd0ADikSznthCWEBHFHXthrhzxbOHMEAw9bLqwGrAoPUvqUeWBB0AmWXFANObAkbk1ojYogoUS9Q0w5djq4uVbqCaCPkxwT5rnVeopVQYvbFSBVLgUffq5ZygexszIDPLt2+QHHczQAR6KuTofTE1sWpV/xOQzp9uo1v1Klht10we6AzoGwR/oKdMcx/LLKX1l6TrOVAoVd0ezkKhAuoZ9iN6z19e93EP12n/7UmgpheZrNNwN3dAIHXnTm8POAg== 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=5aZJaUdVlRqLRMmExFcUz2pSHzaGMhQRM3UreJKADGI=; b=Owhqh6fOlSrElidRMhPxrth1DQeU2VT1coTsyB5pwUd4JuDKVLFbtU4GVjR0ah4Pv5CNKTPdm7riI36QVS8xI11a22NnBZimQKYbiDx0qbELLNNQWQURUKZu72bWJYIQwepMPstDX4B58LuAXHLLZKSLcztCO1CyRJfRPbPqir0= 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 SN6PR10MB2925.namprd10.prod.outlook.com (2603:10b6:805:da::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.18; Wed, 27 Oct 2021 17:47:33 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47: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 v5 4/7] kallsyms: introduce sections needed to map symbols to built-in modules Date: Wed, 27 Oct 2021 18:47:03 +0100 Message-Id: <20211027174706.31010-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:32 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4ad1b90a-c9f8-4826-f978-08d99971dafd X-MS-TrafficTypeDiagnostic: SN6PR10MB2925: 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: dwecPUALNNmM51qbslPfPIJaK62nH6t6q3lMoGsvGm/mjBL0vZ61YnYmfuJHHJfjegt18go5tg05/2kbL6ayDdwJo8q1pFz/aEaqWlJ1SMEMcDctnCyxgEkZuFFOTYEBFeYpRA7y7PmZkjxewSwi+QQTCwGKpAD5X+9djyhrgnjqK9VZdnfDT8Nqf2yyeinrk5B4Ubwtr15fCpNfP0yHNptbOKc0Ysqe2ahwfebEmnQ/WWgX6aqSOZlUOElKqFZwDdkObNEv88y4II27/LIxXaLbKrsrK6LsThfVa+IRTm3SWXOYP83YbrrYGIlmnvaXmOwMMHWX4Y1yWKmqhpXgSnNBw4dgH4qEkcVo5d3Bv4KI04sbqrBm5oG0gjgcnEQB6d7L+qjvYjB+pEmSJuSchQp24+AczaBqj4UhrIN7njpB68VpVYIKXqUhzOSR8kefnCWHrQ8iXdmvnhkE95UztbsngugHAnboRROVN54GqnNz83HP0YFtyOGUmXZQ7zOSXCQK0CMQcPUYHHuH+TuKSaowcMlPaXjzcdi8Almts/FbCGsTz3rUEhqTqxfLwnqP1sz7q1e3PSFwA0zo5DisFl1FwRnbLgv6n2cifQbKP3LbxTthjiS65r8E4t8ywTK90T9sEUH945ug1iNBfIqCEA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66476007)(5660300002)(66556008)(1076003)(186003)(66946007)(86362001)(44832011)(316002)(36756003)(38100700002)(52116002)(6512007)(107886003)(8936002)(83380400001)(6666004)(30864003)(4326008)(6486002)(508600001)(6506007)(2906002)(2616005)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xdc1+WUBIwlbHk8t28zv3bzJAaUSgvQK4aT7Wiy2qVc4U6ssdK5ML1kPAoImDZCvBL+Mlly6yklm0QHPwzKs0p9is/WZND8Lt5pcf9kcw/BOtnhUTAxzqvMDw9ahR/cWx7BkZBupWC08xvNdDdEs36v6cyuHC1S7qTsMLBG1ro9xw3cPxCeUbEbNrgYObdaTT0mcF+EtvPr1G95/+RPdolsLrb2DKAzRO0mExesTZcvF4WMq/E9YN1+6BeGuIIdPtR6PHLMF9ss1zCsIWCVRE5V+AfIGxDBdLR6ECNWNdRLyx5Fg5jNPQkKin95y+0zya+inize1UEA1ulhGRSLXWVM2J66H90jZSHY5IPNIeWvrcin14a3n1pnvqYXqBA+9PFIBBWPIIfmv2CKjnCYTIFoeP8Y5iZDDGOleBjVYZeTeDJm4jw7dEBHhvWJwhR+r3AM56rRwldy34ir2PnDH7mbXMh1uxPmt5YcIVbaFt8q0dPWma6M5Q4KUSeK2/9s7g3IZ30HABbBnvMyhuKeAja59yvF2v4pyZEdik9h63SJieTjpW2ed6Lfo54nOCCyiKNC2mkNP4OYcV9C7mANXiL2qFGlcmaCNOZgttWE8AmfadyoIJCS2WrBNvqiDbB9uJh+YCOiYg7ZocmJ43XvkBCuhOO65dIIAmOej0HbDkxUSEtYpmzxaMmpEywVQLM0bMcuod/Y20JH7wWsh0NxmA4+ynoCV6vDwGQG6wafhxGOsqMus5Hahm+oZa14omtZLptpBQvsZSrYtW+23+PGx0ezsRXmqclwc+Oa8uU0RMhRpmRa3lurdlKdo69L8g9aV1HbN5H7FI2/u6dcM0q/JTPz/4wjgxUCs3Qvm50pNnhV3PC8ffuN28TpP1x4Vy3BdXcNkBO5SJlBZAMgxgUh4eqL3NaTQc862oGwvMw9lhcepE5UHPtQcDxE2ykF5wU2haosEmqc4lGfsT/J8aoWZEUNqYe9Ddc30qDCpVyhxT3VoBGymWs6rfhv82czAbLSb1fiWd6YXAN4DY/JchQ9k5uCD3BZ9D6uh4929zrsBZqfl/wiFoGLXYmiRlsQShmjphoZYbc5todp1aaKQgaA8042As2gyR1HiFuS9Uo0LsfgxetMcp3i/jnuTp5rLXWF/Xt2v4cvrerMO0JChO9PSkZQxG3e1xWSa8GLqBXf41xgXzgMejdef4NFkDyVP/dJa/1sEefzb6cKNhkLn5znfcvFUuFGsbZLSJaU9hNlLXOJnzZA8mB78eiLykWrla6LrrOA6cCMwbRCpibMi3hnH5Q0Cip8dg1Ux1uH+Wp/mTLAud9JMDA3B4ykGB/IdySRx1Yrn8twzegepkkAHN5M3U36Z/UCP6VNgJg0ETDM9bfxsOCF0/AqSV4KOBcIV0aCIi+HvnQXppR5pE2kPmmQWhS0sDdDJXn49MEJrawxG75jznnz6MuzndwYD///7yOsABJuvH7t3bwqiq6ZjylZaNV9OLJF3YajTMk0Mkgnas4UKIBw8saiaCz2zs0nBdO+dXGLnYmi+xQSrAAs7UThGCZ+uGRQtexP7LXxhcernXhxazzsTzX/cVGCF+acAcMnvy37VhldewwVY8Mn48yQ6fCab9EZ3QlvHVmiNXP5nQYWENI+JS0OJssjCvphwZs/K X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4ad1b90a-c9f8-4826-f978-08d99971dafd X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:33.5391 (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: 2Kzde+ty+uPaQviiD6M7c81cQ3RdVhFSZjlxY6+275DbYm8B6Cvi+F+C9bwuA/WZtzIasfI70rBtxnQHPSfkJg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2925 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 adultscore=0 suspectscore=0 bulkscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-ORIG-GUID: Ycf8FG_eJEjE9ZkC7L2QdNC6vIVvlteW X-Proofpoint-GUID: Ycf8FG_eJEjE9ZkC7L2QdNC6vIVvlteW Precedence: bulk List-ID: The mapping consists of three new symbols, computed by integrating the information in the (just-added) .tmp_vmlinux.ranges and modules_thick.builtin: taken together, they map address ranges (corresponding to object files on the input) to the names of zero or more modules containing those address ranges. - kallsyms_module_addresses/kallsyms_module_offsets encodes the address/offset of each object file (derived from the linker map), in exactly the same way as kallsyms_addresses/kallsyms_offsets does for symbols. There is no size: instead, the object files are assumed to tile the address space. (This is slightly more space-efficient than using a size). Non-text-section addresses are skipped: for now, all the users of this interface only need module/non-module information for instruction pointer addresses, not absolute-addressed symbols and the like. This restriction can easily be lifted in future. (Regarding the name: right now the entries correspond pretty closely to object files, so we could call the section kallsyms_objfiles or something, but the optimizer added in the next commit will change this.) - kallsyms_module_names encodes the name of each module in a modified form of strtab: notably, if an object file appears in *multiple* modules, all of which are built in, this is encoded via a zero byte, a one-byte module count, then a series of that many null-terminated strings. As a special case, the table starts with a single zero byte which does *not* represent the start of a multi-module list. - kallsyms_modules connects the two, encoding a table associated 1:1 with kallsyms_module_addresses / kallsyms_module_offsets, pointing at an offset in kallsyms_module_names describing which module (or modules, for a multi-module list) the code occupying this address range is part of. If an address range is part of no module (always built-in) it points at 0 (the null byte at the start of the kallsyms_module_names list). There is no optimization yet: kallsyms_modules and kallsyms_module_names will almost certainly contain many duplicate entries, and kallsyms_module_{addresses,offsets} may contain consecutive entries that point to the same place. The size hit is fairly substantial as a result, though still much less than a naive implementation mapping each symbol to a module name would be: 50KiB or so. Signed-off-by: Nick Alcock --- Makefile | 2 +- init/Kconfig | 8 + scripts/Makefile | 6 + scripts/kallsyms.c | 366 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 371 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 880b3e67a47f..572861a7d578 100644 --- a/Makefile +++ b/Makefile @@ -1179,7 +1179,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 11f8a845f259..613dec86f5eb 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1526,6 +1526,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 Wed Oct 27 17:47: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: 12587669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF8F3C433FE for ; Wed, 27 Oct 2021 17:47:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B5EF860E73 for ; Wed, 27 Oct 2021 17:47:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243424AbhJ0RuP (ORCPT ); Wed, 27 Oct 2021 13:50:15 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:36268 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243393AbhJ0RuJ (ORCPT ); Wed, 27 Oct 2021 13:50:09 -0400 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RGTnee017567; Wed, 27 Oct 2021 17:47: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=uvvQtessmyoYK1pqUcup3bTjRF4Sbj20I9i9NR9B7U8=; b=qU1INEbnldQR/XRf3Uo8bSVSLkLTHn6HDdulE3BTS7thdwtXjhoGl3B4yQEwAhdU8dsD 9UrK4aBDKLk3h+F73EE6fxiIJ7/u4oE3z0F72dlA2Wbs8zI2lo18y4109CTYBUsqU/t/ GVs95HqvtriDtEe14erDNpzqRfhnQswtL4GONa5UYF2+kcZ/iVgPCT6sHrerenVQNPfB Zi/xRPZqH1G4Yashu0PaFFMGWq5Q7s+SEghgaKf3B71V1Cbv+I+xh0PQlqBtfpWrhxEQ U2KQ6PgQIaLgRDpgGZjtSng4A/whrzuuhODu1oHrv8N3a7xBKoMMQq/oLHE6oQD07VPV 1Q== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4fg3srr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:40 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHfZaI186349; Wed, 27 Oct 2021 17:47:39 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by aserp3020.oracle.com with ESMTP id 3bx4gd3a2u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JD9BJtuh1GhvnKBCyQMm+3Mv1QM6U2Un9zu0H3IWrjPY0euSpAV40mxz9c92jLmQ1GSqZgEvWTi9Y/P78I3DwaT0u1e62m5dUorVvI8ZngT2lO5KI3W3Y4hvAwyid9rnEkbdVgvyXyGL/w4dpjdBbHZNsT3ZPLmOMtsdrVcC7rBtlUCxYvy8GPmFhFjayhYpgs4X8hU+I6lG31idCc+CaArd8ytKGIFFl21RMnuWo+zd6tMkPIKbqvFm9QRNFdIooYoGE2zXXI7PuEpAIrKq14fO67eavtcuJUxUkrozUpq9zR9/gW/28WFNYuwmIsWx/bwa/IhYL0B0Ro2RaHlLJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uvvQtessmyoYK1pqUcup3bTjRF4Sbj20I9i9NR9B7U8=; b=WR21UjmZDrDWBL2RqLFoh2DScktKx4/XbE8J6fnaPOPpx74O/opF1k9TVPWk2PFGQxnixcNTJLc9sUSWM5eMkzivo7tM9p7yRotRZohC7gCCRGf4GseLYdz1HXtHlv+hojTxn9kpCmKLRpuRW4YQwavf5PGCh7FTvaaD3ZqZ66ekar0ZOdVzp7mIwcZE9vHPfKOtS81Z6PNk/zkbKGJaIh67YROc9DnlKd441zURi7b0sOI9nwfqX6wqCUmQ9i6/ViS1oew5igYK0cM4KB97mwt90QGhmRHzCIJSxa+otGT8h2lVJnbGhN64B4u9dImNjBYXmVdEz1iOocgM84wXkw== 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=uvvQtessmyoYK1pqUcup3bTjRF4Sbj20I9i9NR9B7U8=; b=j+U68u+/yF/mHaBIjHHayW/Y3Cq8fBQa8SlwpOb7DYbD6+Nsj86NATjgBfnZceH/g4jJmG+1rgnOO38q0ecWHJMt7ryKWKFzGQdyJB+TypKPIyP7qO/8t9h1jWAOoPba04Abjes1zgUwV6z87T8cSKQQL8EsuEFz3AW2rVDs/lU= 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 SA2PR10MB4410.namprd10.prod.outlook.com (2603:10b6:806:fb::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.16; Wed, 27 Oct 2021 17:47:35 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47: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 v5 5/7] kallsyms: optimize .kallsyms_modules* Date: Wed, 27 Oct 2021 18:47:04 +0100 Message-Id: <20211027174706.31010-6-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c05fec18-3bdc-4dfe-2a89-08d99971dc06 X-MS-TrafficTypeDiagnostic: SA2PR10MB4410: 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: aSUJloEzRx6Z1j/8jk5Dp/gu9zk5XXYHQX46/vCAqfxTGYCzfIvecTNM0zzPm+kyIY60OQZ9wMlbcmLy30qFglb3Ze3eTTz3X2m8OZ6rC3NiLwXh/eJGCbx9RE5lulyFIDg2H6YZx2zGMn1WdVEVuyz0GPwjtJwWwIjcrWvscDl21yDugRnGskC9fFi/EEFFSjnWKTBUAO7D+i6P0nU7F5bjcZxt3yHduNR3RRUW1khfNAK2+HfitnhFbI1tshhsQKJUN55EJQALh3WzNGS7QnUAVgKe8PjBiWAs40imLtdj9CkccBDe7rk8wNl+mldIvlY9ek3NCS1VGWxKSwK4YVarAVZDYiSFgLK601XZBWe+L1a/f8qVM6YYtBNgMm4JGTTmZCcrle+RIw1MjXzMFRzwiaaWABHCqhxmyAvntZPvrrTewBlItsoABykMP/gskbn7iIAyoJMkYAO9ZnnY56xNKaLGcD2bqCG1KS+G8X6OyDN5Y9k5hCI0YJFhb2x6IhAWbewxrs9/NM7qF05lp4pluIqhypyp3ThTg+VxVPvx/DQ1uzr89lfl05yIv7CuuadAtYUZ4AcpY9eRCvPq/nx8V6kCnqS4lpniKbs5hfxvV/IQYaTkvLAQOOuV+0qAPp++ecR47qMk9TXC50LZvFlasHSW0NHWPSummthckFnTHXWrX01C2cfa+9BqyyDSusT3wyNOV/ChYnrhPlbm2g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(316002)(107886003)(30864003)(6666004)(66476007)(44832011)(1076003)(66946007)(2616005)(5660300002)(38100700002)(8936002)(66556008)(4326008)(8676002)(2906002)(83380400001)(52116002)(186003)(6512007)(6506007)(508600001)(86362001)(36756003)(6486002)(142923001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wxnmzWpJRkN8MZv0TqRZnCJJK7JIC1bCVrEQYZMOlByFUYS00x00MBkbI3wnqwBLtkkgkFp9QrEwbT1rzcXmiWalPCU9OLYdfFPT3U1LsQT7o08/vFnDQ1kuEJtzNcJufIpzrYEMunIQkSzZoCiRZLWSml/tB76tAWTX+NQlzwJAkU3U6VbZktZAXrwCM0FhqLh7dRqhvguQcSYqENB8NZvFASSygzCT+N7orYCZNnvJ4n5L1OlwdC1jyCTr9I2Y3XG8pFv8/CGb/mdZQHcQNJLd1+nrW0zPls7biCq4dMqsR4I8lUm/kBgvig68yeW2jweC+Q8irB+vjdsavGxHw2Q70EHEpG3xO/ebBeRP7vXQdwQHmjhzPAydcrmM/71jJ8/EWNFXiCGKZbRL2beI/gqKu3dXUBbKq66WQe5CzBE3pGLsDZhzFb+sTrVb6T2BHrXl21NahAEfbZfHSYAVTtysSFKnjH5ixXsq7yKKXJfECU+zV2znEDS8dRH8wlSXx0n/K2RLf+UPXUuPUWX5FI9flpCkK82vjKmZybwA59070onnMJIDriLcUlfrvOMGT3lkrujXV0ZOrW17zmNeGb9yZfx9fTewVg7HRha2tyTUnV1D90GnsGcB5uO/MSwwrG5XnbcUQgMHhD5NbJobYRChjuy+Bmwejwdrh3iSHiCE6FnDAbjnlpqCyVHoUz+yitZAanmOOFCV/A3Rbp+1aKlsqjLYx6SLMjd/5ihBdQ7szfAxNiTATxyfiU6iOEnEFMrWTjwctPWAGtuiGAbreqwAcm6wYpHssDwzArbv02RnHwDR2Oo5gm2lm5XnaihvrfEKR0IsjAd0vuVckegdDZR3Qt10uBJOQz1r7iYFsyRXiBR326oK9Y8Xlcr8XRtntXGhtVX6HHbYThOTKlOObNn8Q4Lun8+ZCbI0QFTaBcmuKlzlC/5dOylaTkMTsvZR5/mySZMrE6K/al60t1J6Y+v7OthvsmKKnjzCva7jreVt9pIKyppF3YQyKuXD5ip1yHSx1TVNm7eANvwvOOz0jZRjqceMFFoiU8tEaf5MmpfiDvjI8dH9FSB4vlnsvhWXVdVJAEkJ3+MRz5SSL6jvE6JcJVi2LxDP/yj8Jg7AuidZN814oKDt2OvzF/t+2e3qSSX+kESSYtjvDdUMGaKdIu7Alo1zguN3OoBfCKCcTX00R+2hezgxryX/j9ZeJ3WidWScllZNGulol7F+jxL9G0pkABNz0IKEk8qwndWymkbCsxps4Zpcm9RAxOBMuiiDl6FCmnSEnI1UNFvIoZzvF2jB3QfzpQWQgG7c2VGsSvFMxsQLSv+RwSOW3uk0lmjkQAuaoNefa6NkSTYzyVD0DrmgHMW+WMjUpFMWGN8Hi9MdiSe1ECGUrRyCREKWLNjX+9oG1xJinUaq53jhWFhuXUxs28DwXuFJqLXyZFF/pXymEJZRWP+5Y0iIuUrMfpBB7rcammflbXt/G5QQUIAt7yovSjugEkf1cVV4UliqSSYoZyX5SYoCd7Dn6oSFYMlBCZQjwrangW+wOTfYTJT6yUR6fhK3T058cjEe6zP/nfAFfvRMzFkQ+pysqJTAnaTkECmu7rtur2DALuhUha3kMUlJ8D3ID/gLbSo0hFg370W/empPBBKT4yKv/kMO2OgL X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c05fec18-3bdc-4dfe-2a89-08d99971dc06 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:35.2466 (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: GVbZ5piuYFdUzdhQzBUtFByGyzpZXWWVOPvN2svcmS0OY82DZ13wO07mqcvVDj8c6NOsX4BEc0Mvj5GhQ6yx6A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4410 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 mlxscore=0 spamscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-ORIG-GUID: 7SsDGp5qWhY14pe-WrrAl2mMqzcOBIZr X-Proofpoint-GUID: 7SsDGp5qWhY14pe-WrrAl2mMqzcOBIZr 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 Wed Oct 27 17:47: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: 12587673 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AA70C4332F for ; Wed, 27 Oct 2021 17:47:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EEC9E60EB4 for ; Wed, 27 Oct 2021 17:47:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243455AbhJ0RuU (ORCPT ); Wed, 27 Oct 2021 13:50:20 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:26614 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243405AbhJ0RuM (ORCPT ); Wed, 27 Oct 2021 13:50:12 -0400 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RGY8jk023028; Wed, 27 Oct 2021 17:47:43 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=cDnlvyIOqvnHQ/aW7BgZReFoQjnr+wWq5+8W+U6dikQ=; b=pw2rrEsa7/+4a5VObO1Cl/uHOKcvZhNa7FSBO7fr9rOxfiYOAuwhvvmc5tFvT/FbpG1n GJ7+D2Lhfy71dSXpMy8LTXvuqfR3PS5NQZD2k5VXWX2gdOFIgOGti9uXnOI3/9pzLMhW TMlWfhA3vuPXkVehqHmjZ/gXKc2EexHQHW/r7BCi4jfncZTN0UPNR4DFaHCFIsv5hM6S Qn0s1ojzeOQ+41VyonG6B11H2UNl5fW7fIGnEeaKntZVBdWFYnI5Bzlr/6ve72bKzK/Y Bp5BCjDqseSXOJp9Z66XUhKpWYXV4cauvJ9o4hwSRkCJZ1c6tyBTrFBuu4dI0echnp2Y ZA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4g1barg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:43 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHg1pS049470; Wed, 27 Oct 2021 17:47:42 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2174.outbound.protection.outlook.com [104.47.59.174]) by userp3030.oracle.com with ESMTP id 3bx4h2rug8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bmYtFroyX6Rao4HAfhalPX+f2P4nf4DSx7HO3FA8HJDbCeLinZ9wpikJLer/ZElGX7p/l+SGLmK/dNWkY/e3hlnwBcmJVYb8vY7yqwI7iIUm+FzeAN5Mif5iA8vksQO1PIZ3rm063OnMa22A7fsxqpZWCJuYgMGcTNuVeMP0KlMHQppbUV0YXmdglIbkAfARKy5cOwRFOrT84GJ2gs+fG3Q6plXVh6Ub5EaQAzUcR15CSSI/uflccR4hvE1Tlt00XV9FDy7mbbOkZtxC7b2D/Sj5MbX96idfQ+VejZjhJIL9c/Re9KBPOnIZNZ+v+cBOcaG7afIwRZlMeR4koZ2AYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cDnlvyIOqvnHQ/aW7BgZReFoQjnr+wWq5+8W+U6dikQ=; b=aXr6D6S/v0ewpktjWkFWWBZfKHXeNvth53CP9Y2HWtHadYbapS1LTbc5Pkb+epL3xXSM8dyTk2ZkQupcLPWl36WUlX26cjwsp6EC36ySebrjc280cSZI7mXNLDAfEhOqw+zFNASQwxLP6t8wVkS7kyp2V2XTJzrRj1QrbaKtpwe5SltPeAKmTJW9rok/Xg7+V7tyW3VjvaTrlUD1T+761AJAA5GIV44dlEFYKGqrAHzcWjLmpSutq5Ii3I2T41TYgAkSDuieWEPlfxHw5aixvYYAvHc6wXK01A1tEdT78/Zt7kGG1DiLaaVHa6Je4/ZZWw0QDcD933Dh7GfEagz8Rg== 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=cDnlvyIOqvnHQ/aW7BgZReFoQjnr+wWq5+8W+U6dikQ=; b=eEHLFMnv7noCihx6paIeY9oBxk8paqml/S8mD3mxjV4YlunU9Cwdo8U3mXhIimFetOiq3cC1d+xyzVJQ1n03Ol5+OD6iUrsooUrO0dLG8hW8slZhDzsV4UApPSWvUcq2AGzu1BrlUIfhiHNWZtcAaRc+WQPJmSfEI7ftZqbD9YE= 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 SA2PR10MB4410.namprd10.prod.outlook.com (2603:10b6:806:fb::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.16; Wed, 27 Oct 2021 17:47:36 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47:36 +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 v5 6/7] kallsyms: add /proc/kallmodsyms Date: Wed, 27 Oct 2021 18:47:05 +0100 Message-Id: <20211027174706.31010-7-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:35 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 80ceac3c-5309-48e6-ebef-08d99971dce7 X-MS-TrafficTypeDiagnostic: SA2PR10MB4410: 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: S3q5jaqxOVRJbkj/oqpXxOb2fOLiwpArPifU6o53T8KU+G6ekH4Lp+ewmZmmfhBwuvWiKekaUXxaNWWrTIoIlox/+bENS8/5ro4MG5PxIFwHzkMgtFRcMx1Vs0JUrjF7/P3GB01Hbyx+zPpZbPx0ilQkmig0+rna/9JVjVZ3Eyde9d7r4qDfICXIvb2wQlG7fT2b8s94VFw/F1Gky1Ih0gmyLyZ/8ohbBaLSnloQeSPJVWq0CUJo+J7Nvq4afnyuEnufQpBVARV687FSucRGASl6rmN96uXSNGPd2DyWbjQ2fpciJY3guzZY2t1fbA+k4E6wvg+Ts1jlm/a5jeSvGhpizaN/zOW6tcfzpqK7bLamn22FQTV82wo8/TTU9399GjJRI1jOyYBP5i/Z/MkvgOtwO9Wk2jaryJoujSt1eGt8YkJmZ+yrtKfF+09HagN2qi60kIgMjsH/DEzj7NiD5lXG+v7JB256lLw+VidfYEshaZMqa0HGCVlK2xZni3vPEotaGzMuTzbLF/dgTm7EckqrXvDDGaOjEM0lDdg9ZwD0ZPV2Oz1UcGQnyRe/fzPifes3aF0Y8x6KGVCcTzxZ6vNiRaLkS+5TayCL7VufD0WRLXtI7+yky0n0x5C6qkjfcHVFQDpycqQqtjhRqkHARA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(316002)(107886003)(30864003)(6666004)(66476007)(44832011)(1076003)(66946007)(2616005)(5660300002)(38100700002)(8936002)(66556008)(4326008)(8676002)(2906002)(83380400001)(52116002)(186003)(6512007)(6506007)(508600001)(86362001)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WHehKtWPL7S4k9JSR90qU8GzPo6Q5pVoGB0nLgH6g/22yZBC549S2IUZ4URtj6XahsYAbnbLJVruseBHD1s1knbDCc/PhMPgDp8SZtMKmcMdLKatK9IkYeExgD+bx7fOKphBErxqWnZUE6VdAz01buw4dlXfjeOb0DEftdQOYMO30deIPfKxKDoOGx1mB3VXHtb3e2lCJo8jEPwG6E2PbzDPCTdmyayM7TH/E4Vmd10qTCMvJkFpZpAQtC++h315Yk/cJWE+36yHD7sIzJGNfNAi6uT6B+khFrCGSmvaopbO9OZ2kkZiCnu02XgdQPGAjzLFrrd4fq4QLNcvpgz5qV8/JbTO2AmF3T8hW8AmXX+lyRdeBFc9AIpD0Rr3KA8Lsw07HMsKv5svN9kZIy9FP/HKR1K9o5/9qGUYzGv850vx245hgoFhmdz4dwot5meh4QNfbEQCvpUmjlFajeQYD6pxZdpjdKfA9hOen9zcYk1TK37zvUcqKitp9beBmnS2puqpoUy7jGvmmqtpAUorOtp6taOQaS6gs38dGpG7aM47tJBIL8CP2/PHcDZJFTcE+KMJp6vmzb241OdHxXvizTkSISuACxjtw8P3dHXIdBhCoCxDQ3YbzfXPDGlBV2aER4gmvp0CXURfw4zAMzUU4WKYF9YJ+9OacRvmMkxBg/Wspr4Psr9+vlRpbWNWbhn9Fd73ecnMkCJpbQVielUmEML9pQiQbLA67XMMFKKL3cqQUuRfljMGMhh1ltO/ZDxAXpLV+G80UkDWN9zHozdTd9iOkTCOdcIk0lNMbj6WSi/iaVtWr8gYKtakbLda5lUxlkpkyJXRrKjdSLo+aurbCMmCjLJZ2HVCQmgwaggfVD/e8AvvPP1sjS9z1CFFEjYtOfIVySG3dZABEU7DWmCvwkmHAW3cDFOi9JhlOfLH62cz10Pbn5EeZnUh3H72DxAAbFAjr3GVUeyKtKeWjYzPyFFEPrZkJaujkxS80/DchW6f4jEefkzM7JWLEkyepyO3w0YuX3+FQMLlIFwRVFouF1Zmmu6fGiYNGrDB3DpQCCDm9QIUWJRWVcdcIAL1PgIDhMz4FxMVpQCBAL991IzfR6u9YEbXNH12yf1udY0s6st0NIGuGZEcNRIgfyW6nVq1ZAsZV3C/y9otg+9iaPpZo5fnu6YjASHX2EbWD+DZ5etuADRGdAEYHmacmeaCIi0HwUvtfDCe5lXaKQ3oaxIn6iIKSQRCHE4RdoF6m2FuD6mPnhx2FTyrJYnnI7dP+dSi5edHaWwia7qQNDgsCvfqz0Q6CHNDqpzJ+GOwwFk5B6xvqUCjEulxNxYtDRkcfoUrLKjn6IG4jhl+DCXkwmewVI47r0Sd+pP0u+Ai/SZr5Ls6R7UOpKjqEk2l5Ig4DG4ev7pLqySlEPlfbBW7fYdd7/OXk1cuX5B8uJ0bqXi5SvZnDx7pvFFGoKcRh0kzasD052vHC00ILg40gC6ACGPLe6QZ5rOiYkRrAmV5u8nWKhuQDgejbgOiK09sP25k/coxlXojSMHICm27osueLq/aTpLDSGXccOWo91nzJld0RhYNX9BXPKMippGG4FWyZ7omHtowcK9mSBTP8UoSQMQtzyiBuwWWXGjmYQjIf0qxwlEzwwO9cn76D2iEruoQK7Rk X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 80ceac3c-5309-48e6-ebef-08d99971dce7 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:36.8277 (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: qMm8nNNreOR9d3vOoDV7/e+PacfRAF+IMoRmpfC36RiKnT/BibZRBR3qBdnbEQle1JmwbhovUiCeFv0hQv4oJg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4410 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 suspectscore=0 mlxscore=0 adultscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-ORIG-GUID: dIlib6yUGZz3mxDhaQhqHzKciQdn8OIn X-Proofpoint-GUID: dIlib6yUGZz3mxDhaQhqHzKciQdn8OIn 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 Reported-by: kernel test robot --- kernel/kallsyms.c | 242 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 227 insertions(+), 15 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 0ba87982d017..38109bb02bef 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -48,8 +48,18 @@ __section(".rodata") __attribute__((weak)); extern const unsigned long kallsyms_relative_base __section(".rodata") __attribute__((weak)); +extern const unsigned long kallsyms_num_modules +__section(".rodata") __attribute__((weak)); + +extern const unsigned long kallsyms_module_names_len +__section(".rodata") __attribute__((weak)); + extern const char kallsyms_token_table[] __weak; extern const u16 kallsyms_token_index[] __weak; +extern const unsigned long kallsyms_module_addresses[] __weak; +extern const int kallsyms_module_offsets[] __weak; +extern const u32 kallsyms_modules[] __weak; +extern const char kallsyms_module_names[] __weak; extern const unsigned int kallsyms_markers[] __weak; @@ -185,6 +195,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) { @@ -288,6 +317,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. */ @@ -559,6 +636,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; }; @@ -589,6 +668,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; @@ -608,6 +689,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; @@ -622,6 +705,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); @@ -642,23 +726,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; } @@ -704,7 +818,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) @@ -714,7 +828,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; @@ -724,14 +838,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; } @@ -740,7 +854,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; @@ -751,23 +865,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, @@ -775,6 +933,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 @@ -810,7 +997,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 @@ -818,7 +1006,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); @@ -831,6 +1019,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) { @@ -841,7 +1041,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. */ @@ -858,9 +1058,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 Wed Oct 27 17:47: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: 12587671 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A79CEC433F5 for ; Wed, 27 Oct 2021 17:47:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9338C60F92 for ; Wed, 27 Oct 2021 17:47:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243402AbhJ0RuT (ORCPT ); Wed, 27 Oct 2021 13:50:19 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:41416 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243403AbhJ0RuM (ORCPT ); Wed, 27 Oct 2021 13:50:12 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19RHT3dG028933; Wed, 27 Oct 2021 17:47:43 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=MJIIpG5J/sZ/qkqMAjoO3Bcnzu1odC9OT6db7awLjKM=; b=zOJPn8XftF3Nf5SOJSO1DsNeT+CCTR7l6mZrErqqjlwscYsbBHcHoa50kaLPeDjuN+mA blDqmtgyu7/57XBjZ9jg85Px//0i8zyRjzCsqXJcMrQxdqOwcbxcYyL8FNG6efbSKZJX 32fPXEDe9zp5qeuCTzjimnUu8LgCk6mj6FAehJH8NTqIcE09L1rD41rnuYZL5atYNUfR 0c6ztDYG2dtHMhB79VC33X+jzDNZKS7ObVhzsdITR+2dHwnbq5gAoGqEyor8ZtpMzySy bdhYMTF3DzpbizdlREXECv3BJWwylGf3vgxpouAfM3CMb5IJqIJ5+gYyf6PjQ4U8L+eO sQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3bx4fjms91-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:42 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 19RHfwo4056701; Wed, 27 Oct 2021 17:47:42 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2172.outbound.protection.outlook.com [104.47.58.172]) by userp3020.oracle.com with ESMTP id 3bx4grfjvr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Oct 2021 17:47:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VVdehk3NxA0fbxCpWMXW0SAJtNfc3hNkNEHL0VCcU7ybC5qK5D4S7tgZdKatPE8fxaf3jG/b552wRVg1uYsPITSAXlkyd5QqmF2sUttlZp1Ls3fRw1+ohza34B60BnvSnUkUrt9/TAx/9OBba4ZSBGaR2HMgPl3nFOxYMWd58rhjzyFUJMSGwnDLsr+wvD1Z6ousd54jNWvw9Id3XomthXyv9mT7nT30TFiRM0UVeJQvQ/S6d8zvsosxUopxpNhuF5gB4O6P/bBO2WJ7KL+ll9KHU2uGJhMiGHTDB2Pj14wtWCdT0XKhT9Ulqq2x1H61GWPhsItno8WkT8sdacDCzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MJIIpG5J/sZ/qkqMAjoO3Bcnzu1odC9OT6db7awLjKM=; b=Jaa8yMOr/7wVvRc6yMeuNkUepijibvB05ZePN5vVqvWQsC8bTajkVpAN8jPIgAkX/DuNWIwAT+4v2JpjlsRfjWvIv+WKUyNyUa4OsaCRm0yEchQc25oxO46zeTpzx9heyFtHJN8I5xqzDHq29a17CcuwPTMkZdJtcqufrhX0ahVD7FckF8a8o2cYnjHfB/Gu+PrRbTcMTxXmPcy7OjJTUNy4yo1BfU3UO+ioCPvI9jwcgqmdqtydPJjtUNw+xbSwKec3Vup9+O/AnWZCFBDKQweeAYvx054IT3jWWJovHxGWE11JTvPeB0qGY1xpoTr9zFS1RIIL1Kj0vND0V89QHg== 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=MJIIpG5J/sZ/qkqMAjoO3Bcnzu1odC9OT6db7awLjKM=; b=Ro48sIZgt4Vy+GXtmgt805IIGzrgg5EE47sLIh85f4lkhGlDScs/X2KOXURJTVp/wJeUOFZ3KkA275tzEWQ5uMDgaB4W0XXPt+7kYDFrhYFbtwpvk3kEhINzQmmOYpvLYlzhYgyCRCeSMTxOrOeIR5D2ER2spojv3kBsJ1iuWW4= 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 SA2PR10MB4730.namprd10.prod.outlook.com (2603:10b6:806:117::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.16; Wed, 27 Oct 2021 17:47:38 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::6887:4e61:85f3:a83c%7]) with mapi id 15.20.4649.015; Wed, 27 Oct 2021 17:47:38 +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 v5 7/7] kallsyms: add reliable symbol size info Date: Wed, 27 Oct 2021 18:47:06 +0100 Message-Id: <20211027174706.31010-8-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211027174706.31010-1-nick.alcock@oracle.com> References: <20210929215154.300692-1-nick.alcock@oracle.com> <20211027174706.31010-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO2P265CA0493.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Wed, 27 Oct 2021 17:47:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7a6f25a2-6f63-48ca-198e-08d99971ddeb X-MS-TrafficTypeDiagnostic: SA2PR10MB4730: 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: jIkwqSgSJ8MD1iulO0lXzQfwQx8ks8ykCgpGIDq0ZOk5KkeieUw4Qe8ulKWfjydh1vnFWWc4HsirBhE8yTtGfThdmlej1skQklmC41TCY7KPCo9T+wt196MA7oYetQF6BtpBFqRVxOOSLfW0CWBZq1FymkuV/yYbAjbWHXO6a908oAbtEAxESnKQRjzU0Gq+T299sy3njWEbqOwNHSFSnUcqGMPpaaQjSDhM4nLDh5OL3r8VeICfMo9g1xfU5dCqa0n0DXzAFO0AOU3qeJMUkGnt4eBNqGUZBRPkfRIpSyghptVc18AdBjhZA8/KVnhho+1TD2OMTqyxpncopwbI7dNfEyWXGdisJ2gXi6wNtK0wRScRm0zhZ080tgXG05Gsl353u5o2vryZ93Bb8n3vU5IcRWk+hnTA5VZnz4vAOtZCAe39GVjABjLnGscEObak/gOt+BHb8l7zY4QHLwL3kz8oj7a9Y1j2aup3cQxZSAFU+VUxj1DJSHZ2uX+ljH1ldxTvD8PDd8etaj9EBnxH4L5bDiFgtt6BrmyKgUNccDkF7s77sxu2GYkexKso4njaO/+angAPznDggCjUDRpWOQOG03x+giINgGfYSTE1pQ5SWkwxstK3SLp7IA/up5xOcplYIbbqTRQdrRiOvcDD1Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(107886003)(6666004)(1076003)(66556008)(36756003)(6506007)(2906002)(6512007)(86362001)(4326008)(6486002)(52116002)(66476007)(5660300002)(508600001)(66946007)(8936002)(30864003)(83380400001)(38100700002)(186003)(44832011)(316002)(8676002)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DY6CJcukuhiToj6Fm1/wr7L7ACiBamUCBf1VjTshrLyOn23zpDk9To79CheJ0FS2ia7NyhpGPnn49XywxRkLbHe4+fInUQUjJrnnOhZTp18Bncpm8KCMR/u3qd5WgH111w3fMwtFxTMChRo+PDx0HEWdwhNBioIEa20FjR2KJBUhzbD+3O8JgjFzfcpDfmtBnXebkQBWp15gCO4w7d9QgBS1N0TzlUUoZ0MkLrP9lXe0DvTinxF1VvcFBArXZlVZDC38VaTcJm+6V0jMb6VWvKz5po8OXqAFaAF0qhEeL/Q1ZztkBMsvNQGlcs62lRMv2u36DjkqGHVj4eiGGj+iRjwp45QX2RHqAsT6pIMrTqh9lzSjwhy9gT1Tu70Au3rQUw30vjz6MdYaSoSy4stxdErBWC2Nstg+n+ijnIkSqUGdBax3RwQ9xGKNvlphJbS5KH/TXXn7FS1DzhSyaAinm+8FA+e4YMQW40KoqGxQb1rtVLoMic29Aol0hIW2KJdAMvHP6P8Vb1CL8PcBqVRfEIb71hBuCunjgnZ5FrFZ8iIrBtMyaZy4DuHTCf1IgOuQ1yt5vva+63304RQmm12M42nctLtD57OReEQi5xy35XN9kxQ0k8/YJNpyumRhZGFxlckHK9cySa70PWzQWIbA7idiqQXCSJlqUqY4tgezZKe+ObWEVTCaEwgQSSXJF8hSFIlx4F6DeNeWA6/XHptMM7W80YL9RXOS73ukcpjfesPQBDDOAjf916NYYJ6SSA7oG+9qsL/rld3S6yOXz3d0YQhI3vWtuB/DdT0X5LoMsQDWOtY05C8ReNBoT0e0evwyOH9WzU+Dt/hzdptTvrrSLQsZSdHi1UcXr3VHbMEZ+IzL0Yc+H52jO8Ht9mSmcxjAOdArT00kuj1NR99zPrQ1fCyyaq8tq8iAppwbknr/h/0Z/mZr+LPAzoatZTVl6h/wa+aklnbsHvgMMwaiLCI7ksX9hyx6cp5mAl0oW5KoRVUjT69FYkpmVAZZ3MY6CRTfBvpSB6nO0od8CutMSaIAlEWsfUh1qOQ+/krX/TdPWJViGRpR9kI05y72ZYDLx91WgIniGW+Ji3SJkZ6i2x4+4yLccTzvLh9lfr/cqJ6ANwK/Sxjw+iX3GKYIWrvovym7w2GHitUPcBUyMG6P15C46fmN5dYpfqoScKRPkJE86cTnE/ORB1msTYwOxT5ORi3EBHH3r9azUfXNe8xU1bwwvKL10IbIuS6O9bNW0mvYXC8l+63avI/GQSfKFptSFlOLlY+GjXT1Wr582fbP3zUqpiBIwNMpnT6/YLhZDu/zVPVSqdPwngJ0t7fxeM9UenCAswfGm9ySkXexIzy1AEcWqVEU22vY797DvDldsMsHSYA2K6Tvj9BJzw0pbcGdhIZU7YVVFYBG2lZGn6/Qylljl2pprs1LJQ64+To5xB4NWF/H1ul/xHXeOX4DuI8jh1y2r1SoSD3GjHu6+5Uwl4xTbJJCYNZJrdZ0Uz2pLL3jHXuDrb/8N0NrPTMC+uL4Ih5cYjcBpYXRBsSeNoMt9jQc011N/dNr4hFPTeKDxGwmKNrgncKxmty/jF/ZeFkdG2IcjK/uvkNQ0ZURn1TLKMHhSIaWosD/zqtcrWNyDOEtQPT6D4Zn7lt5n5Rl9WSz1jzf X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a6f25a2-6f63-48ca-198e-08d99971ddeb X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2021 17:47:38.5292 (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: so7zOcbT2/FEwtDqw03BBTzPHKtJK8bAUV9aRZ6TBphwBtEncjUU0nXE5UaYOv9vbCQlIwKqnfv+oBUqoPqn1A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4730 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10150 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 adultscore=0 suspectscore=0 bulkscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110270099 X-Proofpoint-GUID: i8K5H9AfOdWhCSoZDk-SxYcIW93GByI8 X-Proofpoint-ORIG-GUID: i8K5H9AfOdWhCSoZDk-SxYcIW93GByI8 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 c9f1200b2312..b58f2de48957 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -590,7 +590,8 @@ struct module *find_module(const char *name); /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if symnum out of range. */ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, - char *name, char *module_name, int *exported); + char *name, char *module_name, unsigned long *size, + int *exported); /* Look for this name: can be of form module:name. */ unsigned long module_kallsyms_lookup_name(const char *name); @@ -768,8 +769,8 @@ static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long * } static inline int module_get_kallsym(unsigned int symnum, unsigned long *value, - char *type, char *name, - char *module_name, int *exported) + char *type, char *name, char *module_name, + unsigned long *size, int *exported) { return -ERANGE; } diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 38109bb02bef..c87a593b40ea 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -36,6 +36,7 @@ */ extern const unsigned long kallsyms_addresses[] __weak; extern const int kallsyms_offsets[] __weak; +extern const unsigned long kallsyms_sizes[] __weak; extern const u8 kallsyms_names[] __weak; /* @@ -257,12 +258,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)) @@ -283,36 +296,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; } @@ -633,6 +627,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]; @@ -667,7 +662,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) { @@ -740,6 +735,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'; @@ -858,12 +854,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. @@ -883,15 +881,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 @@ -902,8 +900,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; @@ -915,7 +913,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 40ec9a030eec..3653a7de9b41 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4368,7 +4368,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; @@ -4387,6 +4388,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 a30075c14a25..c780f2a5d10c 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -274,7 +274,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