From patchwork Mon Jan 22 09:08:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 189982 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2452902dyb; Mon, 22 Jan 2024 01:13:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHwHPp9pHRNC+unwA8ko48ViBvx+rA5IvS8b7e8f5AAWIHXYDWscWQGRhc0+ZoFSTtcrPn/ X-Received: by 2002:a05:622a:4c:b0:42a:4456:15b0 with SMTP id y12-20020a05622a004c00b0042a445615b0mr437151qtw.36.1705914804860; Mon, 22 Jan 2024 01:13:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705914804; cv=pass; d=google.com; s=arc-20160816; b=IVHa3l5KAr2jcd9PD1jD8POjKrgWhZG/6XzCwNhkmXcT91iX0rucQft2F0Cxuz1vdE /NyFXL5fWHABZgMPMt92/Fz8hkNmYDk0g7yvwHa1NqFU8EOnSULBa+AYTpiExxpPWiaw wq6Jeqy0IFbS0upV8WzOgwYMdh46xgcika++QfnTBSmgQpfez2z2I79Hw5qyKhqFeTnu 2g+xti74MQ3BxSTHYfDqu5dedJBoCf/zJ7gqz90haLrzKLcFSkY3XujPIhAHg9zXA6d+ xYVK2rV/IR+HCoNRJzvTWpZBvGSU00P0YXi8V2rQNaKr7x8s66k5RXf35QsZhjcwJ30J 40Bw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=0Ue2vmRkHm0fVc6V6NYaJ8ToLI0DQfe3Y1iY9J4coyg=; fh=5AiHdFowod8yCNdy/id3xiSSfKdDCkZYEkI53gyaq68=; b=0W2NJqusSr5wUmUZF1ZUBvbxGvm/o269s5lbOCCgdGKh3vXEnByW7dnl7L3jWhXLtG xT8xp+QFErX6Pcl/z4fd3HVok/u4Vs/vQCty6eXuRVKouoUsuqGL5IJDizlfbHsaZgNs LmEOWJDbfVt04hei4qVKNH06EJTarAgkLT+KO9C11GWd89/pp2khdjUhD7dQauy0GZPe s5Anx2IERud6UrUjx9XawPxmcKyEAf6WrZ+Ao1Wvm1D4UMdiFL5AVvHSh2X2TlLBtEkU Vvtvpd1HJ3XHPoHYJ6xYF5g+NQ/H7IJx0TzIP64e3BLcNDOu9SJ7mW9UPzBYRfhEXOJ7 pfyw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SG6i9JSp; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-32599-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32599-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id p13-20020a05622a048d00b0042a3090e7fasi4660325qtx.683.2024.01.22.01.13.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 01:13:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32599-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SG6i9JSp; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-32599-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32599-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 97B191C24270 for ; Mon, 22 Jan 2024 09:13:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A384439FCB; Mon, 22 Jan 2024 09:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SG6i9JSp" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 797FA39860 for ; Mon, 22 Jan 2024 09:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705914768; cv=none; b=XUTbtJW2FJJDSm0fDHr5j3kfhhzYxFrnKz8uiPBRPhahPvvAyVtSFP6d8rhbU3TaBGuQ8HqwwT+Y8sPvsoVxe0jd49iJX+JiPR7pfPRuYoqhC5ussDspfMj8CUDYJvexgTa1C7CJlfuxti/C+FGymFoUhnFUZ+Mj6VlL7Q2qcsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705914768; c=relaxed/simple; bh=NvZ9QYXHyD9CvB6BGA5UKBh+CJlv7ohKGlZB0ViTzZ4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vDyl7lakLiVkDiHSTIY9U4v8kbBb5CR9LgIwlyFDRt9be0zfWVp+Is9ISUBdTg0s3OBwwbbkyWuMKNDQSP+Cn8Vt3JRG37/qJi76UglLIuMpV46+Pbib6dOstUL3ysztRDw5AJHuuX8a4edmvE9fgJ/SFU30Jb02Qh9/KsqyZf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SG6i9JSp; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc237543b74so3348656276.2 for ; Mon, 22 Jan 2024 01:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705914765; x=1706519565; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0Ue2vmRkHm0fVc6V6NYaJ8ToLI0DQfe3Y1iY9J4coyg=; b=SG6i9JSpVNdDkPcVZQkC/nQclGrDc5CAbladqHJL+t4v3HiQDAHtFiGRuoHF6fo2ss rW6XcRNyFJ6wB4S4HkYqFLmbJEAHQ07qAcEFsYBVDUNceVFoT36ilXncUOBGmt+9IJvz B3GNOmzvUkYPx3P7k8M55NqlcrU8Q7aZWZyB63YnNUkJZYy4ucrb+cEbOiQAO6rXfYvc 4N4aKLL5BFr9gt0MPMHmhuC2wbT7WuXOcm6gSvXY/dYyQTT3qybvimFbEbl/7D6v/QTs JBtCp2lxahkwcSA0qn055qk+OcaW8DX+BKVZPtv5jzPAKP/+j5H0pxuwIxxEG5Fy48um trig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705914765; x=1706519565; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0Ue2vmRkHm0fVc6V6NYaJ8ToLI0DQfe3Y1iY9J4coyg=; b=ISc9EyMf2M+KmuDggwWfmMxOXtnx/EFGn9HuLfn7AmTTLYISANqCMWp4bP3XL6b2cV URZyzAgw4KRI+jOQIJJRrzhr5r50+YK6py4HlC4RQCjBOtTG5U4RNV9xI0pYqbbVDUAU Ncm6qVHavMSCcwzg0v8j95vwRtDlefe0fERX4GY2UNdmQS1ihE86Anrvf/bbTrFZ0dk+ V91AlsbNd9/B9TkEFt9C+GUMY/pO4Hzd02BrrVriEcsBoFkgqEdnyQZkUElSd43SIuxV jiJEthdIVqERebxyYeJ1CPDW+HboCEwowIGSR5l/2hcZeO48yIT9Iw5r2ILpfTTgqu+v /PyQ== X-Gm-Message-State: AOJu0YxNTfM2kPSDVpOlEG/RT6yx/R2Qr6PQFu7pABDl9CQNp/ritbyu HlA8JRWHQWreOJtX3KHzn4Sos9plgbvDn7trsE3CnburOBOXp6premgMHtYDzghnJOKuTSL434h wXUzqbEJNgUxImCH1YZrwemLgM+k6XoW7kNCQUmqXxkaMjJA2FEjn3qQbgPyKTxK3bf+klSjjJ6 2LLSHPEHrxsOr0MpI5end8QOmqBfhObA== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a25:11c1:0:b0:dc2:46cd:eee9 with SMTP id 184-20020a2511c1000000b00dc246cdeee9mr238812ybr.4.1705914765393; Mon, 22 Jan 2024 01:12:45 -0800 (PST) Date: Mon, 22 Jan 2024 10:08:53 +0100 In-Reply-To: <20240122090851.851120-7-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240122090851.851120-7-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5349; i=ardb@kernel.org; h=from:subject; bh=8Z7gd3kTb2u9hK0wTWYrEf9GDcgvcakQPNv0R7gN2o8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXWdwdIsmzezFAp52a+9FVjSY79C54j1MiVvl2f/V+7/G aRm/e9hRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZiIeAfDf48/O7a+4GRIKPuy hm/X71tsG+701bE6Fof27rFf/Lzz7E+Gv+Jm0Z3bW9gd+bzVvy3/bZN5q07v2ZbXOzgzH2kK7Fj 9jBEA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240122090851.851120-8-ardb+git@google.com> Subject: [RFC PATCH 1/5] kallsyms: Avoid weak references for kallsyms symbols From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Kevin Loughlin , Tom Lendacky , Dionna Glaze , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Andy Lutomirski , Arnd Bergmann , Martin KaFai Lau , Nathan Chancellor , Nick Desaulniers , Justin Stitt , linux-arch@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev, Kees Cook X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788781322552062251 X-GMAIL-MSGID: 1788781322552062251 From: Ard Biesheuvel kallsyms is a directory of all the symbols in the vmlinux binary, and so creating it is somewhat of a chicken-and-egg problem, as its non-zero size affects the layout of the binary, and therefore the values of the symbols. For this reason, the kernel is linked more than once, and the first pass does not include any kallsyms data at all. For the linker to accept this, the symbol declarations describing the kallsyms metadata are emitted as having weak linkage, so they can remain unsatisfied. During the subsequent passes, the weak references are satisfied by the kallsyms metadata that was constructed based on information gathered from the preceding passes. Weak references lead to somewhat worse codegen, because taking their address may need to produce NULL (if the reference was unsatisfied), and this is not usually supported by RIP or PC relative symbol references. Given that these references are ultimately always satisfied in the final link, let's drop the weak annotation, and instead, provide fallback definitions in the linker script that are only emitted if an unsatisfied reference exists. While at it, drop the FRV specific annotation that these symbols reside in .rodata - FRV is long gone. Tested-by: Nick Desaulniers # Boot Reviewed-by: Nick Desaulniers Reviewed-by: Kees Cook Link: https://lkml.kernel.org/r/20230504174320.3930345-1-ardb%40kernel.org Signed-off-by: Ard Biesheuvel --- include/asm-generic/vmlinux.lds.h | 19 +++++++++++++ kernel/kallsyms.c | 6 ---- kernel/kallsyms_internal.h | 30 ++++++++------------ 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 5dd3a61d673d..a39e050416c7 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -448,11 +448,30 @@ #endif #endif +/* + * Some symbol definitions will not exist yet during the first pass of the + * link, but are guaranteed to exist in the final link. Provide preliminary + * definitions that will be superseded in the final link to avoid having to + * rely on weak external linkage, which requires a GOT when used in position + * independent code. + */ +#define PRELIMINARY_SYMBOL_DEFINITIONS \ + PROVIDE(kallsyms_addresses = .); \ + PROVIDE(kallsyms_offsets = .); \ + PROVIDE(kallsyms_names = .); \ + PROVIDE(kallsyms_num_syms = .); \ + PROVIDE(kallsyms_relative_base = .); \ + PROVIDE(kallsyms_token_table = .); \ + PROVIDE(kallsyms_token_index = .); \ + PROVIDE(kallsyms_markers = .); \ + PROVIDE(kallsyms_seqs_of_names = .); + /* * Read only Data */ #define RO_DATA(align) \ . = ALIGN((align)); \ + PRELIMINARY_SYMBOL_DEFINITIONS \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ __start_rodata = .; \ *(.rodata) *(.rodata.*) \ diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 18edd57b5fe8..22ea19a36e6e 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -325,12 +325,6 @@ static unsigned long get_symbol_pos(unsigned long addr, unsigned long symbol_start = 0, symbol_end = 0; unsigned long i, low, high, mid; - /* This kernel should never had been booted. */ - if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) - BUG_ON(!kallsyms_addresses); - else - BUG_ON(!kallsyms_offsets); - /* Do a binary search on the sorted kallsyms_addresses array. */ low = 0; high = kallsyms_num_syms; diff --git a/kernel/kallsyms_internal.h b/kernel/kallsyms_internal.h index 27fabdcc40f5..85480274fc8f 100644 --- a/kernel/kallsyms_internal.h +++ b/kernel/kallsyms_internal.h @@ -5,27 +5,21 @@ #include /* - * These will be re-linked against their real values - * during the second link stage. + * These will be re-linked against their real values during the second link + * stage. Preliminary values must be provided in the linker script using the + * PROVIDE() directive so that the first link stage can complete successfully. */ -extern const unsigned long kallsyms_addresses[] __weak; -extern const int kallsyms_offsets[] __weak; -extern const u8 kallsyms_names[] __weak; +extern const unsigned long kallsyms_addresses[]; +extern const int kallsyms_offsets[]; +extern const u8 kallsyms_names[]; -/* - * Tell the compiler that the count isn't in the small data section if the arch - * has one (eg: FRV). - */ -extern const unsigned int kallsyms_num_syms -__section(".rodata") __attribute__((weak)); - -extern const unsigned long kallsyms_relative_base -__section(".rodata") __attribute__((weak)); +extern const unsigned int kallsyms_num_syms; +extern const unsigned long kallsyms_relative_base; -extern const char kallsyms_token_table[] __weak; -extern const u16 kallsyms_token_index[] __weak; +extern const char kallsyms_token_table[]; +extern const u16 kallsyms_token_index[]; -extern const unsigned int kallsyms_markers[] __weak; -extern const u8 kallsyms_seqs_of_names[] __weak; +extern const unsigned int kallsyms_markers[]; +extern const u8 kallsyms_seqs_of_names[]; #endif // LINUX_KALLSYMS_INTERNAL_H_