Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp2043125lqe; Tue, 9 Apr 2024 08:04:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVKh0Dg61uD2N5VQy5dJ/O1ilPMiqbo+2Z8EIBMgCvyNNjvn1UxV6sFwVA3IqJ70CEU7731lGulIG2xPvK0+JcIt+miozJevFE2fB/Ynw== X-Google-Smtp-Source: AGHT+IGqQXWF96oTYkjYTrXdj5v7/Hs5xdPJUz/Q535FhssR9l1h4Sxj95X/1K2r1pTyO8Yfn85T X-Received: by 2002:a17:906:78f:b0:a52:5a:de45 with SMTP id l15-20020a170906078f00b00a52005ade45mr955381ejc.12.1712675047408; Tue, 09 Apr 2024 08:04:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712675047; cv=pass; d=google.com; s=arc-20160816; b=dhXBw6Bvc8mB34UuSfKQb9pPd8BNgusZ5bdxFFtfu9+YZ4jU8Nkt+hdmmkwBE20vtx Bz6lII7i25Ay37o2lGaeuwddR9l7Gp8LyMWWQeSQW8yisViayZXFLIctGFGaxOlvBOvW P8DzwyXiNptVKpXb4q3RWZ+K28yfHcnnlguvlU8hG0R4BvScdo3ieioC6i+sMYc45pf6 4fU29O/P6C5Fm2RUnP7kkjLUC/8DOPCGuGP1ea4gRNswID3fxedKPWftb+N+eDfUrOco 5RKfO6cEeDSeIZeU2mtcgmVauRqXwZ1ZaY4fheduXYGDlVhuFyDmIu9NOXALt9pOJ2Uo j60Q== 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=fcRm/bqwNQMHH+e8ox8c6p1gyU6LuJWVGjxyAVGlxGQ=; fh=ik+0uoShAg2Bx5AfyQysT1w0C6AQh/fl+cv55O8QP7k=; b=VRoONeQBhoYewL+gcBZFZLxjhd3Fcfc93w27OmAfgzeQYv/n7DDu5Id41gK43kWOzp Cjpb2W4oFPEUhZoIIKRzQUd4dZaKZCW5UFj7gzssR+7rMdDdbpjX4KSQ+hpz0GPz3zqS VZHSkQEu854B68nLlqtqRWcIxtxynGPJIaLBhEO+mfpgpPnz/L5aC5VJq9rcstIk3to6 IEQ7GMLtAebpzjHG5DcDkTOtvmgUnw7MJfKStwN355UjoDQS7xsU7o6gEzBHgbpO3Kiq 8H46hQr0ZTKp6fdZHteJop6xTjDZuyO83HGhbRRRivVQqf1dnDZSf02zXZSJTiBCHvsM 2Y4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zzKXP78O; 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-137149-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137149-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e18-20020a170906315200b00a51c13876e1si3484549eje.372.2024.04.09.08.04.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 08:04:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137149-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zzKXP78O; 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-137149-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137149-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 5981F1F23019 for ; Tue, 9 Apr 2024 15:03:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B0C4131BB2; Tue, 9 Apr 2024 15:01:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zzKXP78O" Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 D9090130E33 for ; Tue, 9 Apr 2024 15:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712674907; cv=none; b=Lxt42Yp6nJlD5ScM7r9C+cfkXsSs41iVb9nBWlgrK5iUVRO/f5Nmtun/M4fSFwMvyuFGsRswO7YKXNRD/qlgZXRPi3a2L3gp4bc4xJrYukF0m9M2DnzDwI3WPnBrBWiyg0IuKG/NVDMxKbn8SWbUi7UGsselOyel9zISSnYyWIw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712674907; c=relaxed/simple; bh=7xZ8SeIpLR8unxHGJ2waGwWOo71Rkq91p6J16D7wil4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E+pArgDQfVGsx6ErSr5LYOrioNHGZvfzJWCLcfAEKAvLvUdEzMdTro4Vbp+JgcThyxqzyICXiystGcLnj3jLxFdv7FlPqs0sPC6ATG2QLMXvd/SQKAttqT3PtYIPZuLoFiND/YwqrJBhaTIy/0DOa3rreUkJkNRVUCWnLaexxsg= 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=zzKXP78O; arc=none smtp.client-ip=209.85.128.73 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-wm1-f73.google.com with SMTP id 5b1f17b1804b1-41663c713b7so11998595e9.0 for ; Tue, 09 Apr 2024 08:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712674903; x=1713279703; 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=fcRm/bqwNQMHH+e8ox8c6p1gyU6LuJWVGjxyAVGlxGQ=; b=zzKXP78OGssbn49YEx7ASSJKAkICUMbvHjA3xWaeNcMV6IJ8hJ/VvHNNOctzWtKIgF qPjKG3NJmwT0Foc6BBG2AYBZlGcyfD4Wsk3d7JFZMIa2ATp7O2S1udYEOCPN7IYqpxxz 1lma/Ey+DYN0ikCaXQGL86WQWLXVY3nXKUKLcUhDT2I/E+ZwCl0BMxio8Xr3TXitrKs0 reDh9/xLqOwxbnOIFmXiXhyrmI/1AWHiZtPpjMfOR1agNIuL7wHJktMeuWLvSJLFl+ku KazRILdeVd7+OV1MH911NdhsLeKKrs6N9MjWj6SMK/+i0+RtrsFdqFtj6ock2bxjO0RU 0fxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712674903; x=1713279703; 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=fcRm/bqwNQMHH+e8ox8c6p1gyU6LuJWVGjxyAVGlxGQ=; b=FdLHE875jhmUFE3/ZIVvFGAGyLk2GOEUC+TLX1NW+ny2Epg+fq5P9x+vuMehpFlf/g 9FX4FlRjAD7DzZ24IsokpVDGGpPOe3ZhSqw8zH2ojYTcpFBMch+2wBYXS+pCWDzaEWTy ed+a6Igx8FOsZjvQOL/p9xPoddMw+CVFCvGLK45l/ACTNZV8aDwMgbSbDJ527T6rN/BA n9QPjMcFKy8S23u/8koP6dJdEHg7g2XOwllUOhf13E3cBfwNWKf20gsx2kfw7aZZEOAa Xh7y2t9Bl8W7beMEabroDAKEXNPSoKYH+3A4XEF7ZXrr5Mkuuwhar4lMDX+RPlA+/uS0 q/Ug== X-Gm-Message-State: AOJu0YyaC5PBOyVnKZeHxS/WH/zOG4QIkrW9EhggumOfKzxAPbGfzDhc GBULbqliIyMTrTIuGdcooJdN2p7DfRVxxLaTCrOpw0tMQ+XVQPJlOLCexbW0+s27xnqBcD7KSZw CQlRLYLwiNtv6EKisjdydIfNKcUZA/rLVtvDBga9lUdtbOEKMM6vNN6W7DhavX2lr6J+tf9zXNW TEPPL35Js6U5mQFurRnlMD5syA/ZrqYQ== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:3151:b0:416:99bb:f71e with SMTP id h17-20020a05600c315100b0041699bbf71emr167934wmo.1.1712674902801; Tue, 09 Apr 2024 08:01:42 -0700 (PDT) Date: Tue, 9 Apr 2024 17:01:34 +0200 In-Reply-To: <20240409150132.4097042-5-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: <20240409150132.4097042-5-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=bi9e/PWpqNvBy6Iv3Xzt+OhmpfAYnnfD/65oU75l2lY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU00wk8x2uq7Idudy4Er3V7GfTVv3fhVhfl+pYyr8bq5W pza3qkdpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCJrJBj+Vzh1v1rY5nenQeF3 +LO6+ZzNoXNf3OCeUfvT7/6kuw/2sjL8DzHhWPYp7+qG/U3cfEeiLQsO33qZ+urhTdYVrx/ru1r ycgIA X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240409150132.4097042-6-ardb+git@google.com> Subject: [PATCH v2 1/3] kallsyms: Avoid weak references for kallsyms symbols From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Masahiro Yamada , Arnd Bergmann , Martin KaFai Lau , linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Nick Desaulniers , Kees Cook Content-Type: text/plain; charset="UTF-8" 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 f7749d0f2562..e8449be62058 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_ -- 2.44.0.478.gd926399ef9-goog