[edk2-devel] [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation

Ard Biesheuvel posted 4 patches 3 years, 10 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/edk2 tags/patchew/20200608173413.1100679-1-ard.biesheuvel@arm.com
There is a newer version of this series
ArmVirtPkg/ArmVirtQemuKernel.dsc                    | 10 ++--
ArmVirtPkg/ArmVirtXen.dsc                           | 10 ++--
ArmVirtPkg/ArmVirtQemuKernel.fdf                    |  2 +-
ArmVirtPkg/ArmVirtXen.fdf                           |  2 +-
ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf |  4 +-
ArmVirtPkg/Include/Platform/Hidden.h                | 22 ---------
ArmVirtPkg/PrePi/PrePi.c                            | 21 +++++++++
ArmVirtPkg/ArmVirtRules.fdf.inc                     |  5 ++
ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S         | 49 +++++---------------
ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S             | 47 +++++--------------
ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds              | 41 ----------------
BaseTools/Scripts/GccBase.lds                       |  2 +
12 files changed, 63 insertions(+), 152 deletions(-)
delete mode 100644 ArmVirtPkg/Include/Platform/Hidden.h
delete mode 100644 ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds
[edk2-devel] [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation
Posted by Ard Biesheuvel 3 years, 10 months ago
As suggested by Jiewen in response to Ilias RFC [0], it is better to use
the PE/COFF metadata for self-relocating executables than to rely on ELF
metadata, given how the latter is only available when using ELF based
toolchains. Also, we have had some maintenance issues with this code in
the past, as PIE linking of non-position independent objects is not a well
tested code path in toolchains in general.

So implement this for the self-relocating PrePi in ArmVirtPkg first.

First, we need to ensure that the module in question is emitted with its
PE/COFF relocation metadata preserved, by creating a special FDF rule.

We also need to provide a way for the code to refer to the start of the
image directly, by adding it to the linker script.

Then, it is simply a matter of swapping out the two assembly routines,
and adding the C code that serves the same purpose but based on PE/COFF
base relocations.

Note that PE/COFF relocations are considerably more compact than ELF RELA
relocations, so this does not impact the memory footprint of the resulting
image adversely.

[0] https://edk2.groups.io/g/devel/message/60835

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Cc: Julien Grall <julien@xen.org>
Cc: Jiewen Yao <jiewen.yao@intel.com>

Ard Biesheuvel (4):
  ArmVirtPkg: add FDF rule for self-relocating PrePi
  BaseTools/Scripts/GccBase.lds: export image base symbol
  ArmVirtPkg/PrePi: use standard PeCoff routines for self-relocation
  ArmVirtPkg: remove unused files

 ArmVirtPkg/ArmVirtQemuKernel.dsc                    | 10 ++--
 ArmVirtPkg/ArmVirtXen.dsc                           | 10 ++--
 ArmVirtPkg/ArmVirtQemuKernel.fdf                    |  2 +-
 ArmVirtPkg/ArmVirtXen.fdf                           |  2 +-
 ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf |  4 +-
 ArmVirtPkg/Include/Platform/Hidden.h                | 22 ---------
 ArmVirtPkg/PrePi/PrePi.c                            | 21 +++++++++
 ArmVirtPkg/ArmVirtRules.fdf.inc                     |  5 ++
 ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S         | 49 +++++---------------
 ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S             | 47 +++++--------------
 ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds              | 41 ----------------
 BaseTools/Scripts/GccBase.lds                       |  2 +
 12 files changed, 63 insertions(+), 152 deletions(-)
 delete mode 100644 ArmVirtPkg/Include/Platform/Hidden.h
 delete mode 100644 ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds

-- 
2.26.2


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#60891): https://edk2.groups.io/g/devel/message/60891
Mute This Topic: https://groups.io/mt/74757206/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-

Re: [edk2-devel] [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation
Posted by Yao, Jiewen 3 years, 10 months ago
Thank you Ard.
Good catch on jump-table stuff.

Acked-by: Jiewen Yao <Jiewen.yao@intel.com>

> -----Original Message-----
> From: Ard Biesheuvel <ard.biesheuvel@arm.com>
> Sent: Tuesday, June 9, 2020 1:34 AM
> To: devel@edk2.groups.io
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>; Feng, Bob C
> <bob.c.feng@intel.com>; Gao, Liming <liming.gao@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Leif Lindholm <leif@nuviainc.com>; Ilias Apalodimas
> <ilias.apalodimas@linaro.org>; Julien Grall <julien@xen.org>; Yao, Jiewen
> <jiewen.yao@intel.com>
> Subject: [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation
> 
> As suggested by Jiewen in response to Ilias RFC [0], it is better to use
> the PE/COFF metadata for self-relocating executables than to rely on ELF
> metadata, given how the latter is only available when using ELF based
> toolchains. Also, we have had some maintenance issues with this code in
> the past, as PIE linking of non-position independent objects is not a well
> tested code path in toolchains in general.
> 
> So implement this for the self-relocating PrePi in ArmVirtPkg first.
> 
> First, we need to ensure that the module in question is emitted with its
> PE/COFF relocation metadata preserved, by creating a special FDF rule.
> 
> We also need to provide a way for the code to refer to the start of the
> image directly, by adding it to the linker script.
> 
> Then, it is simply a matter of swapping out the two assembly routines,
> and adding the C code that serves the same purpose but based on PE/COFF
> base relocations.
> 
> Note that PE/COFF relocations are considerably more compact than ELF RELA
> relocations, so this does not impact the memory footprint of the resulting
> image adversely.
> 
> [0] https://edk2.groups.io/g/devel/message/60835
> 
> Cc: Bob Feng <bob.c.feng@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Leif Lindholm <leif@nuviainc.com>
> Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> Cc: Julien Grall <julien@xen.org>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> 
> Ard Biesheuvel (4):
>   ArmVirtPkg: add FDF rule for self-relocating PrePi
>   BaseTools/Scripts/GccBase.lds: export image base symbol
>   ArmVirtPkg/PrePi: use standard PeCoff routines for self-relocation
>   ArmVirtPkg: remove unused files
> 
>  ArmVirtPkg/ArmVirtQemuKernel.dsc                    | 10 ++--
>  ArmVirtPkg/ArmVirtXen.dsc                           | 10 ++--
>  ArmVirtPkg/ArmVirtQemuKernel.fdf                    |  2 +-
>  ArmVirtPkg/ArmVirtXen.fdf                           |  2 +-
>  ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf |  4 +-
>  ArmVirtPkg/Include/Platform/Hidden.h                | 22 ---------
>  ArmVirtPkg/PrePi/PrePi.c                            | 21 +++++++++
>  ArmVirtPkg/ArmVirtRules.fdf.inc                     |  5 ++
>  ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S         | 49 +++++---------------
>  ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S             | 47 +++++--------------
>  ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds              | 41 ----------------
>  BaseTools/Scripts/GccBase.lds                       |  2 +
>  12 files changed, 63 insertions(+), 152 deletions(-)
>  delete mode 100644 ArmVirtPkg/Include/Platform/Hidden.h
>  delete mode 100644 ArmVirtPkg/PrePi/Scripts/PrePi-PIE.lds
> 
> --
> 2.26.2


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#60918): https://edk2.groups.io/g/devel/message/60918
Mute This Topic: https://groups.io/mt/74757206/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-

Re: [edk2-devel] [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation
Posted by Julien Grall 3 years, 10 months ago
Hi Ard,

On 08/06/2020 18:34, Ard Biesheuvel wrote:
> As suggested by Jiewen in response to Ilias RFC [0], it is better to use
> the PE/COFF metadata for self-relocating executables than to rely on ELF
> metadata, given how the latter is only available when using ELF based
> toolchains. Also, we have had some maintenance issues with this code in
> the past, as PIE linking of non-position independent objects is not a well
> tested code path in toolchains in general.
> 
> So implement this for the self-relocating PrePi in ArmVirtPkg first.
> 
> First, we need to ensure that the module in question is emitted with its
> PE/COFF relocation metadata preserved, by creating a special FDF rule.
> 
> We also need to provide a way for the code to refer to the start of the
> image directly, by adding it to the linker script.
> 
> Then, it is simply a matter of swapping out the two assembly routines,
> and adding the C code that serves the same purpose but based on PE/COFF
> base relocations.
> 
> Note that PE/COFF relocations are considerably more compact than ELF RELA
> relocations, so this does not impact the memory footprint of the resulting
> image adversely.

I have tested the change in a Xen guest. No issues reported.

Tested-by: Julien Grall <julien@xen.org>

Cheers,

-- 
Julien Grall

-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#60950): https://edk2.groups.io/g/devel/message/60950
Mute This Topic: https://groups.io/mt/74757206/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-

Re: [edk2-devel] [PATCH 0/4] ArmVirtPkg: use PE/COFF metadata for self relocation
Posted by Sami Mujawar 3 years, 10 months ago
Hi Ard,

I have made corresponding changes to the Kvmtool port patch series (that I am working on) and can confirm this patchset works.
Not sure if Tested-by would be correct in the context that the Kvmtool port is not yet merged. In either case.

Tested-by: Sami Mujawar <Sami.Mujawar@arm.com>
Acked-by: Sami Mujawar <Sami.Mujawar@arm.com>

Regards,

Sami Mujawar

-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#61094): https://edk2.groups.io/g/devel/message/61094
Mute This Topic: https://groups.io/mt/74757206/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-