public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Marvin Häuser" <mhaeuser@posteo.de>
To: devel@edk2.groups.io, ardb@kernel.org
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>,
	Thomas Abraham <thomas.abraham@arm.com>,
	"Ard Biesheuvel (TianoCore)" <ardb+tianocore@kernel.org>,
	"Lindholm, Leif" <leif@nuviainc.com>,
	Laszlo Ersek <lersek@redhat.com>,
	Sami Mujawar <sami.mujawar@arm.com>, nd <nd@arm.com>
Subject: Re: [edk2-devel] ArmVirt and Self-Updating Code
Date: Sun, 01 Aug 2021 21:40:12 +0000	[thread overview]
Message-ID: <1042ebd1-30eb-420e-be10-2d5d7ec71373@localhost> (raw)
In-Reply-To: <CAMj1kXFTOrEm70LMJdwzvKc14uAEtn0h+6koVeYxFUZjycJEsg@mail.gmail.com>

01.08.2021 18:33:47 Ard Biesheuvel <ardb@kernel.org>:

> On Sat, 31 Jul 2021 at 21:08, Marvin Häuser <mhaeuser@posteo.de> wrote:
>> 
>> On 23.07.21 16:34, Ard Biesheuvel wrote:
>>> On Fri, 23 Jul 2021 at 16:27, Marvin Häuser <mhaeuser@posteo.de> 
>>> wrote:
>>>> 
>>>> 
>>>> On 23.07.21 16:09, Ard Biesheuvel wrote:
>>>>> On Fri, 23 Jul 2021 at 12:47, Marvin Häuser <mhaeuser@posteo.de> 
>>>>> wrote:
>>>>>> …
>>> ...
>>>>>> …
>>>> Do you maybe have one final comment regarding that second question,
>>>> please? :)
>>> The RELA section is not converted into PE/COFF relocations. This 
>>> would
>>> not achieve a lot, given that no prior PE/COFF loader exists to
>>> process them. There is a snippet of asm code in the startup code that
>>> processes the R_AARCH64_RELATIVE relocation entries before calling
>>> into C code.
>> 
>> I searched for said ASM code till my fingers fell asleep and at last
>> found this:
>> https://github.com/tianocore/edk2/commit/b16fd231f6d8124fa05a0f086840934b8709faf9#diff-3d563cc4775c7720900f4895bf619eed06291044aaa277fcc57eddc7618351a1L12-R148
>> 
>> If I understand the commit message correctly, it is basically "pray 
>> the
>> C code does not use globals at all", which is fair enough, so maybe I
>> should document this in my proposed new library? I trust that this is
>> enough of a constraint for both ARM and AArch64, because I do not know
>> them at all.
>> 
> 
> The C code can use globals, but not global pointer variables. But you
> are right, this is not very robust at all.

Right... Will document for my PE library.

>> What worries me is that StandaloneMmCore has no such ASM entry point 
>> at
>> all and instead it's just executing C directly. Also, it is not passed
>> the "-fno-jump-tables" flag that is commented to be important in the
>> commit linked above.
>> 
> 
> This is because the StandaloneMmCore is built with -fpie, which
> already implies -fno-jump-tables, although I suppose this may not
> offer complete coverage for BASE libraries that are pulled into the
> link.

Ah okay, thanks. Out of curiosity of how ARM implements PIE, and how 
StMmCore self-relocation can work *after* the PE/COFF section 
permissions have been applied with .got merged into .text (i.e. 
read-only), I checked the GCC5 "DLL" with readelf and found many 
relocations into the .text section. I have no idea how any of this 
works, and no idea where to find out, but as it apparently does, I might 
just update the PE calls and call it a day. I cannot test anything 
either because there is no QEMU code for StMmCore I can find. :(

Thanks for your tireless replies!

Best regards,
Marvin

> 
> 
>> Best regards,
>> Marvin
>> 
>>> This also gives us the guarantee that no GOT indirections are
>>> dereferenced, given that our asm code simply does not do that.
>>> 
>>>> Let's drop "GOT" and make it "any instruction that requires prior
>>>> relocation to function correctly".
>>>> 
>>> The thing to keep in mind here is that R_AARCH64_RELATIVE relocations
>>> never target instructions, but only memory locations that carry
>>> absolute addresses. This could be locations in .rodata or .data
>>> (global vars carrying pointer values), or GOT entries.
>>> 
>>>>>> …
>>>>> Correct. And this works really well for shared libraries, where all
>>>>> text and data sections can be shared between processes, as they 
>>>>> will
>>>>> not be modified by the loader. All locations targeted by 
>>>>> relocations
>>>>> will be nicely lumped together in the GOT.
>>>>> 
>>>>> However, for bare metal style programs, there is no sharing, and 
>>>>> there
>>>>> is no advantage to lumping anything together. It is much better to 
>>>>> use
>>>>> relative references where possible, and simply apply relocations
>>>>> wherever needed across the text and data sections,
>>>>> 
>>>>>> …
>>>>> The GOT is a special data structure used for implicit variable
>>>>> accesses, i.e., global vars used in the code. Statically 
>>>>> initialized
>>>>> pointer variables are the other category, which are not code, and 
>>>>> for
>>>>> which the same considerations do not apply, given that the right 
>>>>> value
>>>>> simply needs to be stored in the variable before the program 
>>>>> starts.
>>>>> 
>>>>>> …
>>>>> The selection of 'code model' as it is called is controlled by 
>>>>> GCC's
>>>>> -mcmodel= argument, which defaults to 'small' on AArch64, 
>>>>> regardless
>>>>> of whether you use PIC/PIE or not.
>>>> Aha, makes sense, thanks!
>>>> 
>>>> Best regards,
>>>> Marvin
>>>> 
>>>>>> …
>> 
> 
> 
> 


  reply	other threads:[~2021-08-01 21:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <MW4PR21MB19074D9D114BBBCC21B53B6FEF199@MW4PR21MB1907.namprd21.prod.outlook.com>
     [not found] ` <DB9PR08MB67464B261E4F1815BCDD7C289D199@DB9PR08MB6746.eurprd08.prod.outlook.com>
2021-07-22 14:54   ` ArmVirt and Self-Updating Code Bret Barkelew
2021-07-22 15:14     ` Ard Biesheuvel
2021-07-23  9:54       ` Marvin Häuser
2021-07-23 10:13         ` Ard Biesheuvel
2021-07-23 10:47           ` Marvin Häuser
2021-07-23 14:09             ` Ard Biesheuvel
2021-07-23 14:27               ` Marvin Häuser
2021-07-23 14:34                 ` Ard Biesheuvel
2021-07-31 19:08                   ` Marvin Häuser
2021-08-01 16:33                     ` Ard Biesheuvel
2021-08-01 21:40                       ` Marvin Häuser [this message]
2021-08-02 18:05                         ` [edk2-devel] " Andrew Fish

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1042ebd1-30eb-420e-be10-2d5d7ec71373@localhost \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox