From nobody Sat Feb 7 18:16:01 2026 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zzKXP78O" 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-Google-Smtp-Source: AGHT+IHvCKu1YErbunDRlQQT1mQ2V1BPeNyzIW3NN67RkKNkrJSZrXol+cAxMIkYVKNsmKpsmezyOCuk 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-Transfer-Encoding: quoted-printable 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 Acked-by: Arnd Bergmann --- 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/vmlinu= x.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 =20 +/* + * 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 positi= on + * independent code. + */ +#define PRELIMINARY_SYMBOL_DEFINITIONS \ + PROVIDE(kallsyms_addresses =3D .); \ + PROVIDE(kallsyms_offsets =3D .); \ + PROVIDE(kallsyms_names =3D .); \ + PROVIDE(kallsyms_num_syms =3D .); \ + PROVIDE(kallsyms_relative_base =3D .); \ + PROVIDE(kallsyms_token_table =3D .); \ + PROVIDE(kallsyms_token_index =3D .); \ + PROVIDE(kallsyms_markers =3D .); \ + PROVIDE(kallsyms_seqs_of_names =3D .); + /* * Read only Data */ #define RO_DATA(align) \ . =3D ALIGN((align)); \ + PRELIMINARY_SYMBOL_DEFINITIONS \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ __start_rodata =3D .; \ *(.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 =3D 0, symbol_end =3D 0; unsigned long i, low, high, mid; =20 - /* 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 =3D 0; high =3D 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 =20 /* - * 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 t= he + * PROVIDE() directive so that the first link stage can complete successfu= lly. */ -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[]; =20 -/* - * 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; =20 -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[]; =20 -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[]; =20 #endif // LINUX_KALLSYMS_INTERNAL_H_ --=20 2.44.0.478.gd926399ef9-goog From nobody Sat Feb 7 18:16:01 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 89E7C132808 for ; Tue, 9 Apr 2024 15:01:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712674908; cv=none; b=k7wClJFC6n4rZpQeebN4m1dPU515v/VKLAruEWzVX5resDm8roMTIAr2QXq4Zhj3FCvRqZQbBUff/5RCXTK/ADuRqeUtEKgGuj7FxSB3oOA3a4eKV7k5U2zf4/a8Qdi9o56vt2zDG7j41wdcfoqV1mYQ5vcI0R4XBSHb4B7BFWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712674908; c=relaxed/simple; bh=ne1nIh0Qwrvg61AZAr4OTz8Z0LGxASlvMGm2cM6SR1I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gxBRr9S5T7JebQJRlUM4RKeDCwGa78WHQfVgoQ3E/0qV4ygicgHOXOFD41xNr0+Q30X5PNYMjg06ucVP/oftvRlRKiO/7Po9vxzyukfYNpmtdbn4BBSc3RqvFKN6c+gGvn20G/inaW/5U6CFp1r1FGkeMCxNqk9gTOveAhZefYE= 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=Nmjz20d3; arc=none smtp.client-ip=209.85.219.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Nmjz20d3" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dd944567b6cso7435606276.2 for ; Tue, 09 Apr 2024 08:01:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712674905; x=1713279705; 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=24D5emD5FgWK1X1kR0N5gec4wW0lY0v4XxqAgq53xkE=; b=Nmjz20d33u9rLdxGdphKCSSWa8Z2ty3V/+CLOg5WSz0CFekw0qSm5qfxo81mFO1Pli 0FkXH2yjBZDcKnoydobfGzHkbR/F32YcPRHkphoe8fud7kYjjlMzxB6O8+HZK3d3WuHO RJR7P9T3Y0Db1GzXMWt+LSStmKLkVNcIwWXklaIn2plbesmwY31tXGm/Av+Tb/ZOHvGe 88X7fDsP35AroaSA4Lf5pX9xYwetQn2dFGmqtWRqF65pxpHR039dzHpWciflpnw3Za0Y BoLXMhPq0/c1VjtRnnMf9EjQkGC6Ss2/kZQf/66xLH00zAW1mAIZlVX+csPI6sOXvJcM xKrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712674905; x=1713279705; 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=24D5emD5FgWK1X1kR0N5gec4wW0lY0v4XxqAgq53xkE=; b=jR1idj9DCq3UdQVg6lvODrFshvetG3qfzORTP5mnHzuvqzxNJLdibD6jvg4NjLAt49 rxjq7XTVuo98i3696oFqT8lrfh9zTH8NF6sGFCavtOPIKiV/Iif6qVfDFtMu9DAhKXpS d81lVGtUPTsPmldskU9jFHmiKCVgxiAMi++J3tXFSprZcPwisi9ok38eyaLTkpkivKpe 6JMzx2L8Yaj52SlzTgeRWOkCB1eNBHUm7p405ol9UgafoqeS80jDaddQuTCZVTk7CAqW XCgvok2g3oVJku+Y8A+BddI1XDXoVJhjUOMMVFbgQdNihmv9EgXvWYpuBd1qPadlfKi0 FTVw== X-Gm-Message-State: AOJu0Yzw5KymBtKgoUQtWXvfS6tNPsPak37Tnkaya18ZuUy7jw2Vcl52 /9BeSGQgnISJpa69HHcbam/6zxUVP0reB+sHJsmq7sTy2kRHKwAbP+Y+J3RdENKeA7RauvVp/+X BJtaoySiIbW/4oaMWetvUSU9rp0Jv6m4te0iW7dZELGfJvM6fLG+LQJ5t8nNC4yEAvo2UhZNDUn XcK/UxAGvumCS8MLgoQ7yORxdPgSmCcw== X-Google-Smtp-Source: AGHT+IFApcvERFVIpHAfgZJhNNMync2IwT47zPV5gAUWhJSxWCho+x3ksshS2TS7lQty3tR6R2B5I8hv X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:1082:b0:dd9:2d94:cd8a with SMTP id v2-20020a056902108200b00dd92d94cd8amr926332ybu.9.1712674905221; Tue, 09 Apr 2024 08:01:45 -0700 (PDT) Date: Tue, 9 Apr 2024 17:01:35 +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=1618; i=ardb@kernel.org; h=from:subject; bh=ORZChgCpQukYWaId1hq+FAluQSgY7FsL4Hz5iVd1jE8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU00wp9pL9M1t7Sty6RbsvftP5R05JPTl/bSvhflPYdjj uy5XCjfUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACZic56R4eKb17tar+vu7Z2R ts6cJfreoohXvLd4uqbeXs1usvGY6R+G/+nnFziwnjNYNonhd07J3e9rEjaJan0+WCn35czN9MM cYhwA X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240409150132.4097042-7-ardb+git@google.com> Subject: [PATCH v2 2/3] vmlinux: Avoid weak reference to notes section 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Weak references are references that are permitted to remain unsatisfied in the final link. This means they cannot be implemented using place relative relocations, resulting in GOT entries when using position independent code generation. The notes section should always exist, so the weak annotations can be omitted. Signed-off-by: Ard Biesheuvel Acked-by: Arnd Bergmann --- kernel/ksysfs.c | 4 ++-- lib/buildid.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c index 495b69a71a5d..07fb5987b42b 100644 --- a/kernel/ksysfs.c +++ b/kernel/ksysfs.c @@ -228,8 +228,8 @@ KERNEL_ATTR_RW(rcu_normal); /* * Make /sys/kernel/notes give the raw contents of our kernel .notes secti= on. */ -extern const void __start_notes __weak; -extern const void __stop_notes __weak; +extern const void __start_notes; +extern const void __stop_notes; #define notes_size (&__stop_notes - &__start_notes) =20 static ssize_t notes_read(struct file *filp, struct kobject *kobj, diff --git a/lib/buildid.c b/lib/buildid.c index 898301b49eb6..7954dd92e36c 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -182,8 +182,8 @@ unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_= after_init; */ void __init init_vmlinux_build_id(void) { - extern const void __start_notes __weak; - extern const void __stop_notes __weak; + extern const void __start_notes; + extern const void __stop_notes; unsigned int size =3D &__stop_notes - &__start_notes; =20 build_id_parse_buf(&__start_notes, vmlinux_build_id, size); --=20 2.44.0.478.gd926399ef9-goog From nobody Sat Feb 7 18:16:01 2026 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 D1E8F134CFE for ; Tue, 9 Apr 2024 15:01:48 +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=1712674911; cv=none; b=F5hXfJfhztOgJvELVAT/GAWYK1I0H2Wey6CPlOUeYf0ogqxGCl8q8BQdGDPdHr8EIPSB4RrfR3EAtJautq0tL5psNg55rCH0HdcDPlFz22shYCzGyX+w+1Tc4Jilot9R2i72wkZB6UjhTA8GFE+W5jwrKcfU6y3vwpeSlX71vq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712674911; c=relaxed/simple; bh=2XRLBMqkeM5HXNl5EWou8ZB0E0TV13o8LSI5x8BSvlY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qCos7Q8O8cP6KV5brnmLaSZWPjYwn8QZUK0+21X4GaIE+DIly6kc3XGwwjst8Zp1hqtWKLAhqliFeYd11XRlXV4RBCQwG27UZr+Jr3EDyutAJfzFUa/7++MrUcoFD7F2t2rhdxymiG47KQSozZYmlZ6rtaAzX0su3oOfDTTOpww= 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=Xjvwx9k3; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Xjvwx9k3" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4166b96002dso10630765e9.0 for ; Tue, 09 Apr 2024 08:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712674907; x=1713279707; 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=+c7Nae7lWjUxFMlj/H2Ns6fhkmPqVh4ydm9u7CZiONI=; b=Xjvwx9k3qQCxCqCPdMfNuiDIs0K/ej3onc8KTfYn8pWkSS4Ix8PnySLBwYMt+aAEh1 SEM5HwkuUPwEC3ftN0Oy07gEsbmWLfeJiIgRlLOH7x0d4mtVAyzQJDLOU8i18LT7XCHY bkwFtdSFCB/IIwPMfeJ4sk8WJ9HJ5fW+PELHzkz2BCRvaOH05GVK+gLYDJTZYha9kHz1 NDCN5F64Q2PVakeKVNLpSLHssJ9xB+pegmXosI5DAgv7USVVBmaYNZAq4Q24DNrozqJi Qt5kkFfMs/CpNDW9/gUToAkQRRnTrbsPkQ2vR2kO+Br5wSUsxVruyIMZ8CVGMoK/0XKi 10NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712674907; x=1713279707; 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=+c7Nae7lWjUxFMlj/H2Ns6fhkmPqVh4ydm9u7CZiONI=; b=GsfPADCgej8qA3IMmeDEgc7KY49xW1fKHNEJWwXBC14BziPrz00shE2gKyBttHT4fC aICffvTyac0s4cZjCZmoZANYCP7yF+5TBx52ugCazQrD+eMmyzcmqNR6fMOmD6Da3mQN HdV8c3JD34/ZXu643lyPDDjFZzhzoa8FZXPLeSzlvPiWF5kiGcmYe/F5MIke7nkMMdfu g+LXAES3a2bpKUEaZ9OmaiLfMx+fsHepZnubg920TvRsU5SM6iY824ASrZNkns1q72xw W+qyfftYhZUa8fYsQ7QSvkMzsAy8muSp3wCK1CyERDNKHQeXAAqqPexgbcayT9K4EBhR ywyQ== X-Gm-Message-State: AOJu0Yw9gUa7mAUK2yCBF2SOKqvQdti0r68YDcayX5nmiIf20DGl4PuK BGmuCY/tKz0fRWsYXaChN0SpT5iupBcIDrEjox1zBMaPITfeQR5KwlSKCCqJsCnn/wzNQj7cBDL MITbic3OUgI9uJkyqAbAAYIJvan2U0o6rFSRRUrflwTOAKfKa103tkn5i1Ym77A07YDMB4WSsww tyPUqFx+zC5EGWmQJkMCZqHoIsvENG2Q== X-Google-Smtp-Source: AGHT+IFXgzgigm6gF3ssRomZsMb2tDkm1/NBAMp7/v4nwt6/VHQkX6fUKALfUDlw7BZpcWQWvAe2T1rU X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:3b29:b0:414:a0c:a82c with SMTP id m41-20020a05600c3b2900b004140a0ca82cmr31439wms.4.1712674907499; Tue, 09 Apr 2024 08:01:47 -0700 (PDT) Date: Tue, 9 Apr 2024 17:01:36 +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=3124; i=ardb@kernel.org; h=from:subject; bh=AkRYLBTm6VuFUE4hRnB+rntHpPOXzattnmr7pMUxTAQ=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU00IkD3NPOKFTu0Hs7W2XPVxMPG4eyLtet0z14zVlho3 KFyPPBNRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZgINw/D/5zHBY8Yjzz8ViLP eCfHpP8DU+J8q72OZiqLNCeGfRA3es/IMIf9kPTBh1IGWxLPFGtVLXWe99aYPWVPvPNpXZv3S38 6MgIA X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240409150132.4097042-8-ardb+git@google.com> Subject: [PATCH v2 3/3] btf: Avoid weak external references 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel If the BTF code is enabled in the build configuration, the start/stop BTF markers are guaranteed to exist in the final link but not during the first linker pass. Avoid GOT based relocations to these markers in the final executable by providing preliminary definitions that will be used by the first linker pass, and superseded by the actual definitions in the subsequent ones. Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so that inadvertent references to this section will trigger a link failure if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF. Note that Clang will notice that taking the address of__start_BTF cannot yield NULL any longer, so testing for that condition is no longer needed. Acked-by: Andrii Nakryiko Signed-off-by: Ard Biesheuvel Acked-by: Arnd Bergmann --- include/asm-generic/vmlinux.lds.h | 9 +++++++++ kernel/bpf/btf.c | 4 ++-- kernel/bpf/sysfs_btf.c | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index e8449be62058..4cb3d88449e5 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -456,6 +456,7 @@ * independent code. */ #define PRELIMINARY_SYMBOL_DEFINITIONS \ + PRELIMINARY_BTF_DEFINITIONS \ PROVIDE(kallsyms_addresses =3D .); \ PROVIDE(kallsyms_offsets =3D .); \ PROVIDE(kallsyms_names =3D .); \ @@ -466,6 +467,14 @@ PROVIDE(kallsyms_markers =3D .); \ PROVIDE(kallsyms_seqs_of_names =3D .); =20 +#ifdef CONFIG_DEBUG_INFO_BTF +#define PRELIMINARY_BTF_DEFINITIONS \ + PROVIDE(__start_BTF =3D .); \ + PROVIDE(__stop_BTF =3D .); +#else +#define PRELIMINARY_BTF_DEFINITIONS +#endif + /* * Read only Data */ diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 90c4a32d89ff..46a56bf067a8 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5642,8 +5642,8 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat return ERR_PTR(err); } =20 -extern char __weak __start_BTF[]; -extern char __weak __stop_BTF[]; +extern char __start_BTF[]; +extern char __stop_BTF[]; extern struct btf *btf_vmlinux; =20 #define BPF_MAP_TYPE(_id, _ops) diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c index ef6911aee3bb..fedb54c94cdb 100644 --- a/kernel/bpf/sysfs_btf.c +++ b/kernel/bpf/sysfs_btf.c @@ -9,8 +9,8 @@ #include =20 /* See scripts/link-vmlinux.sh, gen_btf() func for details */ -extern char __weak __start_BTF[]; -extern char __weak __stop_BTF[]; +extern char __start_BTF[]; +extern char __stop_BTF[]; =20 static ssize_t btf_vmlinux_read(struct file *file, struct kobject *kobj, @@ -32,7 +32,7 @@ static int __init btf_vmlinux_init(void) { bin_attr_btf_vmlinux.size =3D __stop_BTF - __start_BTF; =20 - if (!__start_BTF || bin_attr_btf_vmlinux.size =3D=3D 0) + if (bin_attr_btf_vmlinux.size =3D=3D 0) return 0; =20 btf_kobj =3D kobject_create_and_add("btf", kernel_kobj); --=20 2.44.0.478.gd926399ef9-goog