public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Laszlo Ersek <lersek@redhat.com>,
	"Shi, Steven" <steven.shi@intel.com>,
	 edk2-devel-01 <edk2-devel@lists.01.org>,
	Alex Williamson <alex.williamson@redhat.com>,
	 "Justen, Jordan L" <jordan.l.justen@intel.com>,
	"Gao, Liming" <liming.gao@intel.com>,
	 "Kinney, Michael D" <michael.d.kinney@intel.com>
Subject: Re: [PATCH 1/1] BaseTools/tools_def.template: revert to large code model for X64/GCC5/LTO
Date: Tue, 22 Aug 2017 15:03:12 +0100	[thread overview]
Message-ID: <CAKv+Gu-5VaDc_YeuoLhUPyMDUQn1d8K4Kt9g5vSFjkUgg6VOSA@mail.gmail.com> (raw)
In-Reply-To: <dba4e8da-6b2d-0cf6-67ee-4912fc2109a1@redhat.com>

On 22 August 2017 at 14:27, Paolo Bonzini <pbonzini@redhat.com> wrote:
> On 22/08/2017 13:59, Laszlo Ersek wrote:
>> This seems to suggest that "-pie" is the *master* switch (used only when
>> linking), and "-fpie" is a *prerequisite* for it (to be used both when
>> linking and compiling). Is this right?
>>
>> If so, then I think this is a gcc usability bug. We don't generally
>> start our thinking from the linker side. The above implies that the
>> simple (hosted) command line:
>>
>> $ gcc -o example -fpie source1.c source2.c
>>
>> could also result in miscompilation, because "-pie" is not given, only
>> "-fpie".
>
> No, GCC should add -pie on its own.
>

I disagree. PIE linking and PIE code generation are two completely
different things.

PIE linking (in the absence of LTO) only involves emitting the
sections containing the metadata required by the loader at runtime.
Because dynamic ELF relocations are more restricted than static ones,
and only operate on native pointer sized quantities, this imposes
constraints on the code generation, which is why we need the -fpic or
-fpie compiler switch. On ARM, this means you should not emit absolute
symbol references where the address is encoded in the immediate field
of a sequence of movw/movt/movz/movk instructions. I'm sure there are
similar restrictions on other architectures. Note that the arm64 KASLR
kernel does use PIE linking but omits -fpic/-fpie simply because the
default small code model never uses such instructions, but always uses
relative references (and statically initialized function pointers etc
are guaranteed to be dynamically relocatable)

IIRC, PIE linking predates the availability of the -fpie GCC flag, and
so -fpic objects were used to create PIE binaries, because they
happened to fulfil these requirements, given that they apply to shared
libraries as well. However, -fpic is geared towards ELF symbol
preemption and other restrictions that do apply to shared libraries
but not to PIE executables, and so the -fpie switch was introduced as
an alternative, which generates code that may not be suitable for a
shared library but can be used in a PIE or ordinary executable.

None of this really applies to bare metal binaries, which is why we
need the visibility tweaks when using -fpic/-fpie, in which case the
compiler will relative references over absolute ones. Such objects
could be combined in different ways, and PIE linking is only one of
them.


  reply	other threads:[~2017-08-22 14:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-11  0:34 [PATCH 0/1] BaseTools/tools_def.template: revert to large code model for X64/GCC5/LTO Laszlo Ersek
2017-08-11  0:34 ` [PATCH 1/1] " Laszlo Ersek
2017-08-11  5:28   ` Shi, Steven
2017-08-11 11:18     ` Laszlo Ersek
2017-08-12  3:05       ` Shi, Steven
2017-08-15 15:45         ` Laszlo Ersek
2017-08-22  8:00           ` Shi, Steven
2017-08-22 11:59             ` Laszlo Ersek
2017-08-22 12:23               ` Gao, Liming
2017-08-22 13:27               ` Paolo Bonzini
2017-08-22 14:03                 ` Ard Biesheuvel [this message]
2017-08-22 14:15                   ` Paolo Bonzini
2017-08-22 16:04                     ` Laszlo Ersek
2017-08-22 16:06                       ` Paolo Bonzini
2017-08-11 10:03   ` Ard Biesheuvel
2017-08-11 10:30     ` Laszlo Ersek
2017-08-11 22:21   ` Alex Williamson

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=CAKv+Gu-5VaDc_YeuoLhUPyMDUQn1d8K4Kt9g5vSFjkUgg6VOSA@mail.gmail.com \
    --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