Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp8380362rwi; Tue, 25 Oct 2022 06:07:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6dMbX0eIsYYi6BbnZRpQ62CeMQ//BQa1lXYFCI5AB4OsUj2NEL8wdEXKu5oogsMjWi+2UE X-Received: by 2002:a17:90b:1a88:b0:20d:8df0:ac64 with SMTP id ng8-20020a17090b1a8800b0020d8df0ac64mr43753997pjb.140.1666703267781; Tue, 25 Oct 2022 06:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666703267; cv=none; d=google.com; s=arc-20160816; b=aTm5CefmjBX6KpOlkl8aUhpae3xzk7XGSpXD8Ls4Ba9o2fIKJoHLBR99Exe4g6Hk5w gcIB4gunZqsqHvvC8DiXr7baa1Up9k6e7Mg8HOdBHVMWwu0sTXEfAp7XWcl++ZrJKnfd yM8jg+dyJeZv2gNeo66DNpoVzMcZgFQBBKtBIJ7I4c38BVi4LYV5nhrQ69jij/DjZ4J1 29utsQh8rXiMaYGPwPjp6ZIxvawnPHHHT7bhXn/pGgdgxQicteFp+Hehj/3dR7U87LkT yWruOPYA431HNzOJEyR4uPPmHSYIBDz2KYo8kXrtv/tTfieAZNHyN8joaa6bm2UtL6vx Nx9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature:dkim-signature; bh=RPt7m2ZnSnU+qoKAuwn24AcvwL7oef2WOn+bYLufeh4=; b=Hy3wk/Y1TA8nrUS7fXBjKo0yUq7SkWMHqpuY84XtcpBbFCfh/iV0XvhcfnB96pF2Zr mppqv0qE8o/tyBQDwDaJVXs9xwHhIoRrH5e6IYZbpTgqr6RHrooVDinqC86n2uArF+85 kr9/DxMH7y8NrW1oEOuwHd5jwuhse/pYS0gLVQLwcRR2w3Z4iFQYcn9SLuwM30b6YjAQ 5y22YRDmO3A9928R1KHYqaOFdoBhog5Dv+3nlpHbQQ03hJnJ73wC/1BAT0pmKUapf2Tn TYyfhS04E+dLUSeNt5yUMaIY4V/v9IpKvyjpMKXvrqymqX4T6pOmQyfxEW0WNrc3esA3 lyHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=N6LXlW9l; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w14-20020a63160e000000b0043a18ce4e2asi2944133pgl.393.2022.10.25.06.07.34; Tue, 25 Oct 2022 06:07:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=N6LXlW9l; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231629AbiJYM0l (ORCPT + 99 others); Tue, 25 Oct 2022 08:26:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231217AbiJYM0i (ORCPT ); Tue, 25 Oct 2022 08:26:38 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EE9311A95C; Tue, 25 Oct 2022 05:26:37 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id E060522042; Tue, 25 Oct 2022 12:26:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1666700795; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RPt7m2ZnSnU+qoKAuwn24AcvwL7oef2WOn+bYLufeh4=; b=N6LXlW9lyqmmCxP5okb4I3fCGFCnJku835czaiNTRFI5Edafm7dl3FCBGeNKKBW73gBFiK rJqOulq+TDAY3QMMnFjTayIibggOoY8ix8MONCBI5fY+lWmofOISdRpqLjFVdGzf8ccHGy HSLoNuUR+4UM3wCbf6YKfcrJmJeEG7M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1666700795; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RPt7m2ZnSnU+qoKAuwn24AcvwL7oef2WOn+bYLufeh4=; b=+BYq5nR74fQfcIAWf3bflqx3o7EU6Mp+se1/XJS8qRT/n3Wgw3WHgPM1zo1IE4fby9vxIK FJR1HWflfS0KrsDA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id D46042C142; Tue, 25 Oct 2022 12:26:35 +0000 (UTC) Received: by wotan.suse.de (Postfix, from userid 10510) id C6EED6405; Tue, 25 Oct 2022 12:26:35 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by wotan.suse.de (Postfix) with ESMTP id C581063BB; Tue, 25 Oct 2022 12:26:35 +0000 (UTC) Date: Tue, 25 Oct 2022 12:26:35 +0000 (UTC) From: Michael Matz To: Jiri Slaby cc: Masahiro Yamada , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Nick Desaulniers , Nathan Chancellor , =?ISO-8859-15?Q?Martin_Li=A8ka?= , Borislav Petkov , Peter Zijlstra , Ard Biesheuvel Subject: Re: [PATCH v3 6/7] kbuild: use obj-y instead extra-y for objects placed at the head In-Reply-To: Message-ID: References: <20220924181915.3251186-1-masahiroy@kernel.org> <20220924181915.3251186-7-masahiroy@kernel.org> User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED 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 Hello, On Mon, 24 Oct 2022, Jiri Slaby wrote: > > Create vmlinux.a to collect all the objects that are unconditionally > > linked to vmlinux. The objects listed in head-y are moved to the head > > of vmlinux.a by using 'ar m'. ... > > --- a/scripts/Makefile.vmlinux_o > > +++ b/scripts/Makefile.vmlinux_o > > @@ -18,7 +18,7 @@ quiet_cmd_gen_initcalls_lds = GEN $@ > > $(PERL) $(real-prereqs) > $@ > > .tmp_initcalls.lds: $(srctree)/scripts/generate_initcall_order.pl \ > > - $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE > > + vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE > > There is a slight problem with this. The kernel built with gcc-LTO does not > boot. But as I understand it, it's not limited to gcc-LTO only. > > On x86, startup_64() is supposed to be at offset >zero< of the image (see > .Lrelocated()). It was ensured by putting head64.o to the beginning of vmlinux > (by KBUILD_VMLINUX_OBJS on the LD command-line above). The patch above instead > packs head64.o into vmlinux.a and then moves it using "ar -m" to the beginning > (it's in 7/7 of the series IIRC). > > The problem is that .o files listed on the LD command line explicitly are > taken as spelled. But unpacking .a inside LD gives no guarantees on the order > of packed objects. To quote: "that it happens to work sometimes is pure luck." > (Correct me guys, if I misunderstood you.) To be precise: I know of no linker (outside LTO-like modes) that processes archives in a different order than first-to-last-member (under whole-archive), but that's not guaranteed anywhere. So relying on member-order within archives is always brittle. It will completely break down with LTO modes: the granularity for that is functions, and they are placed in some unknown (from the outside, but usually related to call-graph locality) order into several partitions, with non-LTO-able parts (like asm code) being placed randomly between them. The order of these blobs can not be defined in relation to the input order of object files: with cross-file dependencies such order might not even exist. Those whole sequence of blobs then takes the place of the input archive (which, as there was only one, has no particular order from the linker command lines perspective). There are only two ways of guaranteeing an ordering: put non-LTO-.o files at certain places of the link command, or, better, use a linker script to specify an order. > For x86, the most ideal fix seems to be to fix it in the linker script. By > putting startup_64() to a different section and handle it in the ld script > specially -- see the attachment. It should always have been put this way, the > command line order is only a workaround. But this might need more fixes on > other archs too -- I haven't take a look. > > Ideas, comments? I'll send the attachment as a PATCH later (if there are > no better suggestions). This will work. An alternative way would be to explicitely name the input file in the section commands, without renaming the section: @@ -126,6 +126,7 @@ SECTIONS _text = .; _stext = .; /* bootstrapping code */ + KEEP(vmlinux.a:head64.o(.head.text)) HEAD_TEXT TEXT_TEXT But I guess not all arch's name their must-be-first file head64.o (or even have such requirement), so that's probably still arch-dependend and hence not inherently better than your way. (syntax for the section selector in linkerscripts is: {archive-glob:}filename-glob (sectionname-glob) Ciao, Michael.