Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mfMrv-0001Cn-0n for mharc-grub-devel@gnu.org; Tue, 26 Oct 2021 09:51:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41214) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfMro-0001CN-EU for grub-devel@gnu.org; Tue, 26 Oct 2021 09:51:52 -0400 Received: from shcp25.hosting.zen.net.uk ([82.71.204.30]:49258) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfMrl-0002Z2-4W for grub-devel@gnu.org; Tue, 26 Oct 2021 09:51:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=monkeyircd.org; s=default; h=To:In-Reply-To:Cc:References:Message-Id:Date: Subject:Mime-Version:From:Content-Transfer-Encoding:Content-Type:Sender: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QrzuHjPAIYMDLM02oIi2hDck2ZKsIavJH2MLTdBH6SY=; b=RhYWt21E2eH1hQaSlAHDpBJ4WA gevtJdpAA9KyshgGRpyA4m8gFEceX7hMkPg6VTLc8lHprSHpmH0Pg5hS2QdVAyD4MGydw2AzHYNKc LY3H462CJh0kgEtdGtYvt98TQ4X6Tkfv1RVSe++wbuNBGjBg/N8dXzm5wHlQax8f5QMxnzKj35/Fv IfmGOV0m7aINDA8q/y91ii7yhwcW8wxuRDypg7LrVyNvSzntTgSTr8ZMKAM+6+FeNoi+oE2oSNOHB Ew5tQOsNCaEAK9INFlRQIDSv1qqkWXRFjm26vdif2vuhrExNmLziBtFBf4lIuY61hSP+opf9DPZl5 bNTahK3Q==; Received: from [213.205.241.226] (port=28399 helo=smtpclient.apple) by shcp25.hosting.zen.net.uk with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mfMrM-000X3E-RG; Tue, 26 Oct 2021 13:51:23 +0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Chris Plant Mime-Version: 1.0 (1.0) Subject: Re: Multiboot ELF Loader - Alignment Date: Tue, 26 Oct 2021 14:51:20 +0100 Message-Id: <6D313D4F-96C8-4852-BA46-89FFD989E761@monkeyircd.org> References: <20211026123413.7ykln34h66dvvd3i@tomti.i.net-space.pl> Cc: grub-devel@gnu.org In-Reply-To: <20211026123413.7ykln34h66dvvd3i@tomti.i.net-space.pl> To: Daniel Kiper X-Mailer: iPhone Mail (19A404) X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - shcp25.hosting.zen.net.uk X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - monkeyircd.org X-Get-Message-Sender-Via: shcp25.hosting.zen.net.uk: authenticated_id: chris@monkeyircd.org X-Authenticated-Sender: shcp25.hosting.zen.net.uk: chris@monkeyircd.org Received-SPF: pass client-ip=82.71.204.30; envelope-from=chris@monkeyircd.org; helo=shcp25.hosting.zen.net.uk X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Oct 2021 13:51:52 -0000 Thanks for the detailed reply, I think I=E2=80=99ve got to the bottom of thi= s now, I realised what I missed just after I sent this email. > On 26 Oct 2021, at 13:35, Daniel Kiper wrote: >=20 > =EF=BB=BFHey, >=20 >> On Thu, Oct 21, 2021 at 08:46:06PM +0100, Chris Plant wrote: >> Hello, >>=20 >> I'm continuing to play with Multiboot2 on ARM64 on a Raspberry Pi, and >> I've run into something which I'm trying to understand. >=20 > Why do you use Multiboot2 on ARM64? I guess the question is why wouldn=E2=80=99t I? Is the assumption that I sh= ould use ARM32 and switch after booting? >=20 >> I have an ELF file for my kernel which has two segments (I have no idea >> why rust is giving me two segments, but it is). If I boot directly >> into the ELF file from the Pi firmware it boots fine, but if I boot via >> GRUB I have issues with data corruption in .rodata which is in the >> second segment. The first segment (.text) appears to load correctly. >=20 > Could you share the output from "readelf -Wa "? Additionally, > how is your Multiboot2 header defined in the kernel? I don=E2=80=99t have this to hand but I will share when I get back. The mul= tiboot2 header is defined in ASM at the very head of my code. >=20 >> Digging into this, the ELF headers specify a load base address of >> 0x801060 for the second segment, but GRUB allocates and loads this to >> 0x802000. If I change my linker to align the second segment onto >> 0x1000 everything works fine. >=20 > The Multiboot2 protocol does not tolerate "holes" in the image. You can > find good explanation what I mean by that here [1]. I=E2=80=99ll look into this. >=20 >> Is this alignment to 0x1000 a defined behaviour of the GRUB allocator >> or Multiboot2? I'm assuming it's not a problem with the ELF file as >> it's generated by usual means and runs fine otherwise. >=20 > I think you should take closer look at grub-core/loader/multiboot_elfxx.c > and especially lines starting from 258. It seems to me something around > here overwrites part of the image in the memory. >=20 I did this and I now understand what is causing the issue. The memory alloc= ator in grub allocates pages and hence aligns the memory region to 0x1000. W= hen the allocator returns it gives the start of the region allocated and the= segment is loaded to that address, not the address specified by the elf fil= e. The workaround I=E2=80=99ve used is to align your second segment to 0x1000 o= r to only use one segment. Should the defined behaviour be to require segments to be aligned on pages, o= r should grub load as per the elf file? Chris > Daniel >=20 > [1] https://lists.xenproject.org/archives/html/xen-devel/2021-05/msg01003.= html