Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp270820rdb; Mon, 18 Sep 2023 14:48:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAPVhw4jLA5nnpnm+DVO506XmOoibCMbqhCk6y8ltWtpvStcLaaHwRhCllx3ThJgpWRLnS X-Received: by 2002:a9d:6acd:0:b0:6b9:ba85:a5fa with SMTP id m13-20020a9d6acd000000b006b9ba85a5famr12385311otq.5.1695073697781; Mon, 18 Sep 2023 14:48:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695073697; cv=none; d=google.com; s=arc-20160816; b=kqh83dSJFlMqm64d/HAvvjbMUsR6SGlFiU9XGq1JmIwV4HNcCAnxpxOP10lBBvuyjk A+YrgYRREPSvgRCzP+rnX5gwdRsI0e5A5z6yGJR6bJx7d3c+kX+G2IhNqfsaHMdIQxki pp8oPuAvcivPHt4iVI5I9F/DZxZ0B/DkAAvUiOCedj0rlwzA4xBsTRbH+5tER+MLLNuj DyKSzlIOIIa7zipL6IFOnuv0On4VURtGoJM8b+5kD+HamIL1EgraPgIbyzPiFsetJE1e FqIqUE+hTc2JKOaQpyEbygXMpVp8knSuF6MgKa3kwhqRXjyYhY+pQAnOZxLk+/kG7W/h Sd2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ys8ZuxyrYrmqdRpBskmFPDEMMgzPmmgYAlaMVeRpyj0=; fh=gfE7L+XW2Z6y+c45SBULpR7z1KD6I+cAWmynmtRKiWw=; b=ypgrsIzcPOG0peNr2PmFLO0LEs3NsRhcsNVK22gp2hTcpIMVelrGSGOUIaYIilCKyk Z+0o7gU4JzuJT8WJSBbEBoiO26jC5Yv/AzkV7CJC7LwPE2PNT9pknq5OhmxUOpMxMNOE g37Tca5hhVk9oHvQs45gG+C/g2lH3GEhdWNkzj2XBVznWtbqUYFYIOYnta6yvfyw63YX zxP/P0xprtDIId+w2qiD0tpS9Rvskofar1yBY4M9lKjRrJ5k8vG/ydjj5DiYQrYATrEU LdA5OC9BIPim6pvOV383b8ORG8jDlrEx9bCiW5V/0Sry3Gf0xMNraTtaP9ivWSgrfp47 UFSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="OoL/LYep"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id w20-20020a63fb54000000b00557447d5721si8270567pgj.768.2023.09.18.14.48.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 14:48:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="OoL/LYep"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 30F16801DDBB; Mon, 18 Sep 2023 08:30:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229614AbjIRPaH (ORCPT + 99 others); Mon, 18 Sep 2023 11:30:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229720AbjIRP3o (ORCPT ); Mon, 18 Sep 2023 11:29:44 -0400 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A0E92724 for ; Mon, 18 Sep 2023 08:27:52 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id 6a1803df08f44-656340e666eso18686386d6.1 for ; Mon, 18 Sep 2023 08:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695050706; x=1695655506; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ys8ZuxyrYrmqdRpBskmFPDEMMgzPmmgYAlaMVeRpyj0=; b=OoL/LYepxyn7fq5yLXK7HVkQfKnaUwW9v7EAHw+0/BqIW7xpbUjv7ImzN6RRGVYvBB LSdoSk7MN1Ly/fxCL2Y+SuTUqfpymM3UYxEVBUYNPrhkZrNlO9wu7rUVv2pvoCMzyVxE lqdjza/DAyPELaLP1pYEw+LeIWLv11GbQ2egzU6IRqNBc62NBRHAdMpApbZdmVlFFCoF d8W+M6cPzASeKEaoqBllc/rxOVBmpUfz8Y77v6srfgZOmmN8fm/gEYSlNKFE1RQdM1ot eNYjvKobxwT0l37bAa4e5JlrJAiplopZCJ5kKeqzSHt8SVT4iMcKpjQp8MDiDn8CNUuy rSwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695050706; x=1695655506; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ys8ZuxyrYrmqdRpBskmFPDEMMgzPmmgYAlaMVeRpyj0=; b=pYrdVBQmUtK8yQ4paD+eNF3Ay2CBdBq9DkbQHtR3zrxseXj2CZ4hAtpx69hsoL9JkG 5IGXRK8SeTaLVObcYxOigjmuIOcwvCjX8dKvzehmeiqEESyqfvXDmqgOtqSkP0qdDhQ6 yU/oRDfdD6Z+n9MapT9kstdYrB8iaLqfdHaLcd60ZdYA0ZvWfLCzNkZrgu2us6TQGjTD /7ELy0VhO5To7xR0WVCJXM6ORaW0o0HTqVAOsJeJaOmfLIVNZWEp3Ogo/zRUe3pFgV6m jCZNpjoOOw0NGBcd35wWC6gEPbK0Z6XRE2sLdlvnk+cLMK3n6miSmdCbXXhNcOHA6jM6 wisg== X-Gm-Message-State: AOJu0YzFCO9IKBhi1tkiYnXSireaEstHRnt2940jrO04RD3+PtgG2dae DdFiNIjk0Lf3S4+MS50PXfscLTBrjfx/eeoCDlm/dA== X-Received: by 2002:ad4:4c48:0:b0:64f:3bec:9b29 with SMTP id cs8-20020ad44c48000000b0064f3bec9b29mr10357719qvb.39.1695050705847; Mon, 18 Sep 2023 08:25:05 -0700 (PDT) MIME-Version: 1.0 References: <20230915172900.3784163-1-mmaurer@google.com> <9966E047-44E7-4665-9628-169F3EBE2F06@kloenk.dev> <0561303E-2089-43FC-AA31-836C7BB844B7@kloenk.dev> In-Reply-To: From: Nick Desaulniers Date: Mon, 18 Sep 2023 08:24:51 -0700 Message-ID: Subject: Re: [PATCH] rust: Respect HOSTCC when linking for host To: Matthew Maurer Cc: Finn Behrens , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Masahiro Yamada , Boqun Feng , Gary Guo , Benno Lossin , Andreas Hindborg , Alice Ryhl , Nathan Chancellor , Nicolas Schier , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 08:30:11 -0700 (PDT) On Sat, Sep 16, 2023 at 12:54=E2=80=AFPM Matthew Maurer wrote: > > On Sat, Sep 16, 2023 at 11:07=E2=80=AFAM Finn Behrens wro= te: > > > > > > > > On 16 Sep 2023, at 19:53, Bj=C3=B6rn Roy Baron wrote: > > > > > On Saturday, September 16th, 2023 at 18:52, Finn Behrens wrote: > > > > > >> > > >> On 15 Sep 2023, at 19:28, Matthew Maurer wrote: > > >> > > >>> Currently, rustc defaults to invoking `cc`, even if `HOSTCC` is def= ined, > > >>> resulting in build failures in hermetic environments where `cc` doe= s not > > >>> exist. This includes both hostprogs and proc-macros. > > >>> > > >>> Since we are setting the linker to `HOSTCC`, we set the linker flav= or to > > >>> `gcc` explicitly. > > >> But as `HOSTCC` could also be clang, the linker flavor would then be= wrong, would that create a problem? > > > > > > Rustc uses the gcc linker flavor for clang too. There has been a prop= osal to split it up, but I'm not sure of the status of that. In any case cl= ang's cli is similar enough to gcc that it works fine to use the gcc linker= flavor. > > > > > In that case this looks very reasonable. > > > > Second thing I noticed is that `HOSTCC` could be the wrong variable, an= d `HOSTLD` would make more sense as we look for the linker and not the gene= ral C compiler. > > > Yes, thanks Bjorn - "gcc" is the linker flavor used for "Use the C > compiler as a linker". > > With regards to HOSTLD, I was trying to make the minimum possible > change. Currently, it is using the command `cc` as a linker, so this > would preserve existing behavior when HOSTCC is unset. Hey! Isn't HOSTCC always set? Top level Makefile lines 442-445? What happens if you invoke the linker directly? Generally, the kernel either invokes the compiler or the linker directly. (For assembler, it is typically preprocessed, as are linker scripts!) So invoking the linker directly is a common pattern in kbuild. It also makes me slightly sad if the rust compiler ends up invoking a c compiler, even if it's simply to drive the linker. I'm concerned that while this might invoke $HOSTCC, it probably won't do so with any of the $KBUILD_HOSTCFLAGS set. That's generally been a problem in the past. For example, Android carries a downstream patch to set `-fuse-ld=3Dlld` for $KBUILD_HOSTCFLAGS, because its build environment doesn't contain GNU binutils ("guilty, officer"). So if you set `rustc` to use `clang` as the linker, how do you guarantee that `-fuse-ld=3Dlld` or any of the upstream $KBUILD_HOSTCFLAGS get used? Android also sets `--sysroot` for the $KBUILD_HOSTCFLAGS to guarantee that the UAPI header tests can be built against bionic (Android's libc). Or is that handled elsewhere in rust/Makefile already? Also, if this is your first kernel patch, nice job! Welcome! > > If we would prefer `HOSTLD` instead we can do that, but we would need > to additionally inspect `LLVM` to set the linker flavor accordingly > (e.g. set ld vs ld.lld). > > Do folks have strong opinions between these? My primary concern is to > avoid calling programs by foo when their HOSTFOO variable is set. > > See https://doc.rust-lang.org/rustc/codegen-options/index.html#linker-fla= vor > for details on linker flavor settings. > > >>> > > >>> Signed-off-by: Matthew Maurer > > >>> --- > > >>> rust/Makefile | 1 + > > >>> scripts/Makefile.host | 1 + > > >>> 2 files changed, 2 insertions(+) > > >>> > > >>> diff --git a/rust/Makefile b/rust/Makefile > > >>> index 87958e864be0..2a2352638f11 100644 > > >>> --- a/rust/Makefile > > >>> +++ b/rust/Makefile > > >>> @@ -383,6 +383,7 @@ $(obj)/exports_kernel_generated.h: $(obj)/kerne= l.o FORCE > > >>> quiet_cmd_rustc_procmacro =3D $(RUSTC_OR_CLIPPY_QUIET) P $@ > > >>> cmd_rustc_procmacro =3D \ > > >>> $(RUSTC_OR_CLIPPY) $(rust_common_flags) \ > > >>> + -C linker-flavor=3Dgcc -C linker=3D$(HOSTCC) \ > > >>> --emit=3Ddep-info=3D$(depfile) --emit=3Dlink=3D$@ --ext= ern proc_macro \ > > >>> --crate-type proc-macro \ > > >>> --crate-name $(patsubst lib%.so,%,$(notdir $@)) $< > > >>> diff --git a/scripts/Makefile.host b/scripts/Makefile.host > > >>> index 8f7f842b54f9..0aa95a3af1c4 100644 > > >>> --- a/scripts/Makefile.host > > >>> +++ b/scripts/Makefile.host > > >>> @@ -91,6 +91,7 @@ hostcxx_flags =3D -Wp,-MMD,$(depfile) \ > > >>> # current working directory, which may be not accessible in the ou= t-of-tree > > >>> # modules case. > > >>> hostrust_flags =3D --out-dir $(dir $@) --emit=3Ddep-info=3D$(depfi= le) \ > > >>> + -C linker-flavor=3Dgcc -C linker=3D$(HOSTCC) \ > > >>> $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \ > > >>> $(HOSTRUSTFLAGS_$(target-stem)) > > >>> > > >>> -- > > >>> 2.42.0.459.ge4e396fd5e-goog --=20 Thanks, ~Nick Desaulniers