Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2952849lqt; Tue, 23 Apr 2024 06:45:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVnOuHluepHUyuQBfdvx//97T16B2zG2mnUGq4PqrvpNqO4Sa+6AjeRd8hCSfARlratnrnCAd3wP1zvaOKdyY7PPD3Gm5gEPEYAcvEZ9g== X-Google-Smtp-Source: AGHT+IGFV1Pv34L3B4HUjC+L4rKshtCdk8C/CH3sbVp34YwABpr/BNabrDE0SqYlKjDjYn4nKOSo X-Received: by 2002:ae9:e00a:0:b0:78d:6649:4c79 with SMTP id m10-20020ae9e00a000000b0078d66494c79mr13940258qkk.70.1713879913982; Tue, 23 Apr 2024 06:45:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713879913; cv=pass; d=google.com; s=arc-20160816; b=uxRoCwrl+UQu3TiDUkHsSxWaIyf+L/NSC26TJmDS4qdYyuHsFcFhizsem6wzfpVtGy XfPy1iMA5p6LpOO1gJTuPuBztmrN6/ML8ZtD0uOvbKriFPeH8xZBggTATubuP5EVnM0M M7y4NJNtaoEHOaVAO6NAYygXpnFAxz/54cyXknM3RC9D2H3a4ArBTlAliwTs9qZ0BZWa 9Q8m209oKa3xNwg9HfwMnLUNGWhKliDvlrSURyZlgEkTPUM5IYlCLcABuYoneG+cZFn8 f2KoUzwAstqF4ZlvB1vEHVt+hIZuAMxp8hfnkn1jB8IM0jSlX9rdy9UMsgKFv58UFdDe wgtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=U+K87JqPpId1N8n7tqsCNp/yJ9ChuARWPCP5Z/yxDT4=; fh=7koMbQOx7WQ5AT+4/YtPz9/CIYqfIV0tfiT25h/VHkw=; b=kekxz88xo1Cc3DqsMoWwvINVVvrh3snwF3E410xge4YYZVYaR6aOAps8Q/FMpiHNzU abjTseZ0En/z9hu7Tcr5/6ps+HyWOuEdxx+UGlc4Wa22EmjGNijUWO0sVhoZamTjavrh fEyGm0BV99l0Cj7QR/u05hloHUiFsDcL/sBOoJq8qb/1F56Pz9zmxaegrH/2Ojf87MQ5 sYWKY/tRNRzWp0M3hy7/ODZULSprwMlYDfX59QPgj8AzVIaziMF/W5ugDoTz6Tw82InA RTmMwuTOgNum4vPHElEO4MCpGvl2xLicQmaK3nsObaUVpqm/M8EG4Zzb5tvd2aqfDOFk RnSA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V4kl05cm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-155297-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155297-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id dw11-20020a05620a600b00b00790677f600asi7859238qkb.131.2024.04.23.06.45.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 06:45:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-155297-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V4kl05cm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-155297-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155297-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 B0A9E1C21DFD for ; Tue, 23 Apr 2024 13:45:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B30311369AC; Tue, 23 Apr 2024 13:45:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="V4kl05cm" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A55C6290F; Tue, 23 Apr 2024 13:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713879904; cv=none; b=NcAKCQF9l10dQnqP7Cmv2gK4c9QeE4yDHyhGsJpphwkczjtAkYrfCZvCZWQlOCN8NK2gZHV9q/fzTWfr71MxzTCc8vRhlUh94CJIuTVn0IhCiEe6dA75/MaTAh6RTOmgmc3fZLIDnS/Ddx2C0l/WHlxnizOzFYHnFd/Q7y6K1qg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713879904; c=relaxed/simple; bh=zWK2ReDFbkliJtaNVR7CmsOANV76XcgqSo5F0cqc4rs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=oo8Zx2cPxXFrEyXYud75gY5vA7hJPZ/wiGcPTJfvCkFZcc6t4DlEK0LkR2HyOCy9WeB+iUfl4JLbgQOA2sylE2wbu4sVEZm3aWF6w46PR2a4OpUnzdWDmMMkFJ0gnZor9am3tuMfqUrAU5cPB5dCTQBtthEC3EwOhqdLwgbx2PY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V4kl05cm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 220ECC32783; Tue, 23 Apr 2024 13:45:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713879904; bh=zWK2ReDFbkliJtaNVR7CmsOANV76XcgqSo5F0cqc4rs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=V4kl05cml6OpRcGFHeQw9n3dATJ/Ob+Tc/PDPgR76CXhiaG8ll72J+9JZ8il2UPUP y7QgwvZQOVjw2Bnm+UYZ4XJY6XvauiC83A7eYqWphs9M25ZTugbLtEO8o7sful4gWg fx82dtiy/pGO2gfDRbRF+aJAsmB3NtBxqTskQzoXcxCuNgoKMdTxYtIHy9vfkBn5iQ GxZe066I4Nk+/k5ZKq4tnY2pKhoEcJCmcLpH/bN8tSBxWyYTjr62NtRHkuEo5w/zyt Nqa3/GOhS8cKz2xj3fcy6FDdOlHsWfWiZ+i/a3J1ASsbKTsSOQ+7MXY+ukV5tpfHaA EMQC3bu6GPNVw== Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-516d2600569so7150292e87.0; Tue, 23 Apr 2024 06:45:04 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCWb0kuXropkWuunCugs5zOPoe7oQ/cwZ/QVFju6sSepS4uAFn1N9ArHVP9B/PHy2HM69cXbcMtphi/TnSgsubfRXh4p7DJhbk+V8AhIPY370NGaBtin4UeyE2GxW92EXr1a0RYJRsUrSbuXuYvxjF0HwHDmlOnEvleb90+LKQ== X-Gm-Message-State: AOJu0YzVDO/Ssv1exOOz4quoR6Fpo6kZlxU0cyIya8O4M3on76BEZJ74 MytUh8wGuBhetvKQ93y/k2CQ+XtOuM0MkdfHvRfma65/hvkir4wvjPL5k5NwfY1dpcBbJqcV7If rqT7AqGmSMxQSF0DW13Z2gYDnim8= X-Received: by 2002:a2e:9a95:0:b0:2da:9f24:44a8 with SMTP id p21-20020a2e9a95000000b002da9f2444a8mr9158584lji.11.1713879902758; Tue, 23 Apr 2024 06:45:02 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240415162041.2491523-5-ardb+git@google.com> <20240415162041.2491523-6-ardb+git@google.com> In-Reply-To: <20240415162041.2491523-6-ardb+git@google.com> From: Masahiro Yamada Date: Tue, 23 Apr 2024 22:44:26 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 1/3] kallsyms: Avoid weak references for kallsyms symbols To: Ard Biesheuvel Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Arnd Bergmann , Martin KaFai Lau , linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Nick Desaulniers , Kees Cook Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 16, 2024 at 1:20=E2=80=AFAM Ard Biesheuvel wrote: > > 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 > Acked-by: Arnd Bergmann > Link: https://lkml.kernel.org/r/20230504174320.3930345-1-ardb%40kernel.or= g > Signed-off-by: Ard Biesheuvel > --- I dropped v5, and picked up this one. Thanks. > 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/vmli= nux.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 t= he > + * link, but are guaranteed to exist in the final link. Provide prelimin= ary > + * 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 posi= tion > + * 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 ad= dr, > unsigned long symbol_start =3D 0, symbol_end =3D 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 =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 > > /* > - * 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 l= ink > + * stage. Preliminary values must be provided in the linker script using= the > + * PROVIDE() directive so that the first link stage can complete success= fully. > */ > -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 t= he 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.683.g7961c838ac-goog > > --=20 Best Regards Masahiro Yamada