From patchwork Tue Nov 2 18:38:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12599527 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 69D92C433F5 for ; Tue, 2 Nov 2021 18:39:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54AB46103C for ; Tue, 2 Nov 2021 18:39:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235568AbhKBSmS (ORCPT ); Tue, 2 Nov 2021 14:42:18 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:6006 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235563AbhKBSmD (ORCPT ); Tue, 2 Nov 2021 14:42: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 1A2IIxuM018455; Tue, 2 Nov 2021 18:39:25 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=2S16jvK2Qb7tODtOWWJAdPpeUNnYefz4pv8Xi6Gh3W4=; b=MmYWDVhYOdhMNIvtRuWRT1bf1skBahCJCjpIEGLtRWzKjsh/6GXdfOEsLXx6GKC0SpJk kxkgkYkaN2RFjRH8Pzpp3XTneQZNEGejqwHeY1ABlhGLU9hB9emu6LCciu0xyVbshL1a xkSzNYTkhJcNX80Tf1I7L9+ieJOoaZkLBVfzPd3WTrdQIsABBxFirDENOZS3gD6+B2Il H/x7Lc3ZDCvv2B3qLOMr5GvdUwf0RrxNDWDOOFLLZd2k54zdrkcf1YBNfu8zer5QEXiC VV5IK246pZ0WF1lmB5eAxOXkVn0JcaYZ6hbmGYJInBpxhoEuV7G995me1HiqFF+N7F1S 7w== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3c28gn960k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 02 Nov 2021 18:39:25 +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 1A2ILtw2175523; Tue, 2 Nov 2021 18:39:24 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2170.outbound.protection.outlook.com [104.47.57.170]) by aserp3020.oracle.com with ESMTP id 3c0wv4su88-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 02 Nov 2021 18:39:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HraKb2bAJH1wTyrGWgij2GLX8a5lFy3PKit6LTXKlEQe5xvXUpzZJ05MfZlvnoTYuwpJbwgI1/JeDtuQlb/w67ds6rLhMh2IDkRh8qBjG6Uy4WE9qH2kCmW1OmGaX9SQgMF7y1k3/kOZPD0FoEUt51SwosagRBuRWbkew0QmPl2oBMEPPU15H3z0WRz6l7Kr/MMCqSo8MO/nGcNj6S1P+pucqhnzXwCFHEXHDn7mlMSaimE5VCdDX6Adhxk7KqUfrtyM7Zj3Yc9WwPPJGpUvDH23tclocYkwBmPl3jqepVnCM5nfIpHtfXTudo4qEzeqL1TwvoVPYRWr2Ju8zQCtfA== 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=2S16jvK2Qb7tODtOWWJAdPpeUNnYefz4pv8Xi6Gh3W4=; b=KYdoIyWRABiITLRvjKLPyVNGafZtLGeb327UJuXOvU19/E6sozlAG47iUxJ7dLG4EK9QTQQPwjtLcVF1u8L5UJc8s3TzFBx8khKxUJlMBhpnWpYb3HMDkvUuUcBOIvV+N3eIYIUFwMejFbuOdkfWmos8yw1xCPAt0qPYL0qOdk0orfOPpJ7FwCltUeNLVk234DkHyJWH6/TckGBKIRbkQppxFIRpuCH6r5FUBHwWTTGVvcHdiu4m8HoBCimlWJpUwjdAEuwdTYw1eiSr5mCOS/kZaoy6KJz6jfje8T19PMHJkLh0EF1peiUkYIyC92wH9yQn3nhswjx2KhpAnHGqZA== 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=2S16jvK2Qb7tODtOWWJAdPpeUNnYefz4pv8Xi6Gh3W4=; b=w5gp8lbC6riLAUrDm2JU05YaQDlDdm98bOj+vHWLPNzHQL0VQ5IY4Y9rKRdswz1uMuP/O1BNS3jWQfpBPruQehLjSx0q3/Y6HWpU0t+jeJGtTKOCbqPrBf4wBXL1SQpNKqkejhPr3yW0rd3nTazyTLQOqeYbBhWRM5NxiN3dFEQ= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from CO1PR10MB4706.namprd10.prod.outlook.com (2603:10b6:303:9d::11) by MW4PR10MB5679.namprd10.prod.outlook.com (2603:10b6:303:18d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.10; Tue, 2 Nov 2021 18:39:22 +0000 Received: from CO1PR10MB4706.namprd10.prod.outlook.com ([fe80::1d71:7751:c11:e12e]) by CO1PR10MB4706.namprd10.prod.outlook.com ([fe80::1d71:7751:c11:e12e%5]) with mapi id 15.20.4649.020; Tue, 2 Nov 2021 18:39:22 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v6 4/7] kallsyms: introduce sections needed to map symbols to built-in modules Date: Tue, 2 Nov 2021 18:38:48 +0000 Message-Id: <20211102183851.168676-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.33.1.257.g9e0974a4e8 In-Reply-To: <20211102183851.168676-1-nick.alcock@oracle.com> References: <20211102183851.168676-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P265CA0001.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2ad::14) To CO1PR10MB4706.namprd10.prod.outlook.com (2603:10b6:303:9d::11) MIME-Version: 1.0 Received: from loom.srvr.nix (2001:8b0:1101:10::2) by LO4P265CA0001.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2ad::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Tue, 2 Nov 2021 18:39:20 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e86aeee2-b626-47b1-984c-08d99e30167b X-MS-TrafficTypeDiagnostic: MW4PR10MB5679: 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: pxWDUcB5FWISCWk3RYPhL1bvJ2VQFbVzxum9hfA8DVnCtPQ9Miw/RayERnASZvmVukbSj6S/uBp9kwk4EDaluO8GpDzJF2rUFEQPkxVcVsu2Gylm6cjGtSjijGL9RKoJwsYfcYrru4kAe4pQcxuNIaPRmLRXkf7bqivPB9+2Ch9LCB+cFGCBWB/mORZTbCsiVGcaSiApee8fIFx7BwAoCcReLu1swOVYr/lK0quzZBhhiGX3bK8prGnEp6QtGOehwgOoE0MaJVP9noatXEbYJK2cK1YStv563Ddg3+IXicYY3Ta9NrElGB0pyewdUEEUlJmlERMei3vonqQnG9OaL3aFiqPFUqO9FguFnAXo5rPEmRPCp96o/0p0rWB2WVnreMuLkEJaFOU7UGbR2J9hA7tNrakWN3uwZgJYv0efxYrFu2wuc1xIhNJx+61MkcmZgXXGFNrwy0d8r+kWSzmLnjB2+S8cTED82Jp0GICoaZQ03dRDcAuDYuihv/1b1lB1pOY6G7KvbaGl0npBCRgNhYXHJKIq8+6UQfLlR3By7m+BWAOxdrTGttIM3Y9SeUucYtEmeYyJ3JMhf8f/1OIFLCbQibII1Tgurk3/k9wKiSMiS3yZHsW2gYprkANUyfKr1dXvDn4Uqsk/fLHiW9G6fw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4706.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(508600001)(6666004)(30864003)(8676002)(36756003)(86362001)(186003)(107886003)(316002)(6512007)(1076003)(5660300002)(52116002)(6486002)(38100700002)(4326008)(2906002)(8936002)(44832011)(66476007)(2616005)(66946007)(83380400001)(66556008)(6506007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: h2jFqCWJZBMhcCgRxmq05zMi6dGgahTDx41dNC5br+dTeWridtfoIOW0Ed93zoMzNufjuAZne1SoYWG5QKN2fJ8wuKQKiI+X+ptncIGtYmvdXCw1/Krc3H2sTVF7LjIkcmPlhTT71fyNus+s1hrlVog4u33jNCPuE4O+Kt8oTfiGhTXnKk2BFePeHtWeNKyD9FuI6PgIgpZTZ8JFh1S1V1XYQ9PI6W66NVPO9Rcu++u/bRAMqyZO+Gi1u46uZaLTOZB7iCoIzZlOmGjeY++k98M9wzbrh3CWj34C3s+XYKF/B+Uma8uSPkG8Mdbx7QGXlEi81uv90Z8o6T0dPevIke0TWgwrMpwT/rly+I+760pQKd8Q/aHNdTnwYb/e/9DGU/5d+CQ+DtX68kLXwNqbjaKt3YyIJKU3M5RX0VBfguf8sBwI8/JWpTn4y+i/5Rqb9Y1Y4cdUYrfCVjOBEXp17O+Vw/szfNRXeKGrc5UoDZGtWV0N1H8eG6tCv1PsUTb1AwJbZcNcFyFi7ibmxSRPoPncRexFCWbP5f8XjZH8oc9Jix1FRk7LVygvL26072/TTv0h7VjavjnjZzrVAbgz+XRUD7jbeG6MwG73rm012GAw6igHrFMAUh135PhjbKrNy2vuPUiWM5l9DfSmnBJ+DVacQ/OvmPHgMzrWnm+KoiAxkaVJq1xWBDFNBplv8sTVj5ygWa8jCmqUfw7Jc7In3Iu3xgrFQVPFGlfMXxxF+u1humVK4caBfhjSMqDMt8kBOeKOqQ5Gwr8enETR/d6NndE1nU02fJPnhu9z6fUe8R3MiWiefSBoBhpDbyjmj9EDRtlxAIILXQo/S0f90X6KJruG6+MufCdZHESscJp/LVPYaSSKgSEYCf1vi8Spd3Jm5wA/i0CkhZW+gtxuZlBDxwBMMtsSLJXxWzXVT9F+GJNmn3E9yPd1qnFIRxPUuhUa3JWV3o2REX3TKHeNpIt8SDcqg/qnuZqCHRFOh1bNr55y6B0aMqoplKTLxWhXbbEiuUuA5ekYjBisIqEOFxD+oeuMfO8DTB0eyYAa3K0m+D/RDEs4rO7CJC4V+7MvY0t9i74EYnxMvlbuibQ6h7yj2ozl6P5tv/yLhNAqyFALKmdmc3GMOlO+AmY/6tUQU6G8ailhz+44FPHiNYRZ0gaUizkTJnhYRGR+0VDvVh2wSXmreexrK83o1FpmHEGWuTC+Tq42kNyRYxqDaXR/+rBXJBYMwQ+DVbSWals+DhPw6ky1t+9oINCqJ5NKpamijKo5KEa5E7rQ+s+BrIkhQjmUgNsuRB1wjlVdH8cvd/5RlDwerUxLJUpOFIaS2QzPO6eQiEBp6oY2BiGlGD0LvGwI4jpoQlGtzxhmIDVjuA3EzPyCxW7riGCe2QOazFF3xceOHt80eWIaBYACxepjbLVt/eDM0AKIeZE4h4nUrLfDH62h/ad5ixmqOGlUL4DK6d6oNJv1t2ytlYKZ94p6tbGvj8KqKpECU1f7F+NXvZDt+ijWqQU+FRDabxQkPAQmWNs0jBuq0o4Mt6Qx4aTzuM3+HVW8/rdS+VCFewBX9DGHh/3WSWkHU9pWI+EU+9iOyNcVTNrxbvplOx/adMzKDOt8QPPcu3quta38WHOHRL/AUHxwi1M3jg8b1L4tvtNt5ZbS X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e86aeee2-b626-47b1-984c-08d99e30167b X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4706.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Nov 2021 18:39:22.4076 (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: 7MLtpBRk2VBMFiX5gemfBaIdPlLwA/xZ25edCLPu1FvoUI6rn2TOKssw7o+lph17Vh3OjZip+idGJJhsvUPFjA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR10MB5679 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10156 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 phishscore=0 malwarescore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2111020101 X-Proofpoint-ORIG-GUID: AJXjQdIyYJsDyXvJ5UOW_uGlCR9d9G4Z X-Proofpoint-GUID: AJXjQdIyYJsDyXvJ5UOW_uGlCR9d9G4Z 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 aca770cadd80..635de8ac87a8 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();