* [PATCH] BaseTools X64: fold PLT relocations into simple relative references
@ 2016-08-04 8:45 Ard Biesheuvel
2016-08-04 8:54 ` Shi, Steven
2016-08-04 19:03 ` Nicolas Owens
0 siblings, 2 replies; 7+ messages in thread
From: Ard Biesheuvel @ 2016-08-04 8:45 UTC (permalink / raw)
To: steven.shi, yonghong.zhu, liming.gao, jordan.l.justen, edk2-devel
Cc: mischief, Ard Biesheuvel
For X64/GCC, we use position independent code with hidden visibility
to inform the compiler that symbols references are never resolved at
runtime, which removes the need for PLTs and GOTs. However, in some
cases GCC has been reported to still emit PLT based relocations, which
we need to handle in the ELF to PE/COFF perform by GenFw.
Unlike GOT based relocations, which are non-trivial to handle since the
indirections in the code can not be fixed up easily (although relocation
types exist for X64 that annotate relocation targets as suitable for
relaxation), PLT relocations simply point to jump targets, and we can
relax such relocations by resolving them using the symbol directly rather
than via a PLT entry that does nothing more than tail call the function
we already know it is going to call (since all symbol references are
resolved in the same module).
So handle R_X86_64_PLT32 as a R_X86_64_PC32 relocation.
Suggested-by: Steven Shi <steven.shi@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
BaseTools/Source/C/GenFw/Elf64Convert.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c
index 944c94b8f8b4..7cbff0df0996 100644
--- a/BaseTools/Source/C/GenFw/Elf64Convert.c
+++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
@@ -785,6 +785,17 @@ WriteSections64 (
*(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
break;
+
+ case R_X86_64_PLT32:
+ //
+ // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
+ // possible since we know all code symbol references resolve to
+ // definitions in the same module (UEFI has no shared libraries),
+ // and so there is never a reason to jump via a PLT entry,
+ // allowing us to resolve the reference using the symbol directly.
+ //
+ VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
+ /* fall through */
case R_X86_64_PC32:
//
// Relative relocation: Symbol - Ip + Addend
--
2.7.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 8:45 [PATCH] BaseTools X64: fold PLT relocations into simple relative references Ard Biesheuvel
@ 2016-08-04 8:54 ` Shi, Steven
2016-08-04 8:54 ` Ard Biesheuvel
2016-08-04 19:03 ` Nicolas Owens
1 sibling, 1 reply; 7+ messages in thread
From: Shi, Steven @ 2016-08-04 8:54 UTC (permalink / raw)
To: Ard Biesheuvel, Zhu, Yonghong, Gao, Liming, Justen, Jordan L,
edk2-devel@lists.01.org
Hi Ard,
I don't see you add below code for case R_X86_64_PLT32. Is it right?
*(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
+ (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
- (SecOffset - SecShdr->sh_addr));
Steven Shi
Intel\SSG\STO\UEFI Firmware
Tel: +86 021-61166522
iNet: 821-6522
> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, August 04, 2016 4:46 PM
> To: Shi, Steven <steven.shi@intel.com>; Zhu, Yonghong
> <yonghong.zhu@intel.com>; Gao, Liming <liming.gao@intel.com>; Justen,
> Jordan L <jordan.l.justen@intel.com>; edk2-devel@lists.01.org
> Cc: mischief@offblast.org; Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Subject: [PATCH] BaseTools X64: fold PLT relocations into simple relative
> references
>
> For X64/GCC, we use position independent code with hidden visibility
> to inform the compiler that symbols references are never resolved at
> runtime, which removes the need for PLTs and GOTs. However, in some
> cases GCC has been reported to still emit PLT based relocations, which
> we need to handle in the ELF to PE/COFF perform by GenFw.
>
> Unlike GOT based relocations, which are non-trivial to handle since the
> indirections in the code can not be fixed up easily (although relocation
> types exist for X64 that annotate relocation targets as suitable for
> relaxation), PLT relocations simply point to jump targets, and we can
> relax such relocations by resolving them using the symbol directly rather
> than via a PLT entry that does nothing more than tail call the function
> we already know it is going to call (since all symbol references are
> resolved in the same module).
>
> So handle R_X86_64_PLT32 as a R_X86_64_PC32 relocation.
>
> Suggested-by: Steven Shi <steven.shi@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
> BaseTools/Source/C/GenFw/Elf64Convert.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c
> b/BaseTools/Source/C/GenFw/Elf64Convert.c
> index 944c94b8f8b4..7cbff0df0996 100644
> --- a/BaseTools/Source/C/GenFw/Elf64Convert.c
> +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
> @@ -785,6 +785,17 @@ WriteSections64 (
> *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr
> + mCoffSectionsOffset[Sym->st_shndx]);
> VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
> break;
> +
> + case R_X86_64_PLT32:
> + //
> + // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
> + // possible since we know all code symbol references resolve to
> + // definitions in the same module (UEFI has no shared libraries),
> + // and so there is never a reason to jump via a PLT entry,
> + // allowing us to resolve the reference using the symbol directly.
> + //
> + VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
> + /* fall through */
> case R_X86_64_PC32:
> //
> // Relative relocation: Symbol - Ip + Addend
> --
> 2.7.4
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 8:54 ` Shi, Steven
@ 2016-08-04 8:54 ` Ard Biesheuvel
2016-08-04 8:58 ` Shi, Steven
0 siblings, 1 reply; 7+ messages in thread
From: Ard Biesheuvel @ 2016-08-04 8:54 UTC (permalink / raw)
To: Shi, Steven
Cc: Zhu, Yonghong, Gao, Liming, Justen, Jordan L,
edk2-devel@lists.01.org, mischief@offblast.org
On 4 August 2016 at 10:54, Shi, Steven <steven.shi@intel.com> wrote:
> Hi Ard,
> I don't see you add below code for case R_X86_64_PLT32. Is it right?
>
> *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
> + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
> - (SecOffset - SecShdr->sh_addr));
>
Isn't it identical to the code for R_X86_64_PC32?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 8:54 ` Ard Biesheuvel
@ 2016-08-04 8:58 ` Shi, Steven
2016-08-04 9:01 ` Ard Biesheuvel
0 siblings, 1 reply; 7+ messages in thread
From: Shi, Steven @ 2016-08-04 8:58 UTC (permalink / raw)
To: Ard Biesheuvel
Cc: Zhu, Yonghong, Gao, Liming, Justen, Jordan L,
edk2-devel@lists.01.org, mischief@offblast.org
OK, it is. But it is a bit not very clear.
Steven Shi
Intel\SSG\STO\UEFI Firmware
Tel: +86 021-61166522
iNet: 821-6522
> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, August 04, 2016 4:55 PM
> To: Shi, Steven <steven.shi@intel.com>
> Cc: Zhu, Yonghong <yonghong.zhu@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Justen, Jordan L <jordan.l.justen@intel.com>;
> edk2-devel@lists.01.org; mischief@offblast.org
> Subject: Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative
> references
>
> On 4 August 2016 at 10:54, Shi, Steven <steven.shi@intel.com<mailto:steven.shi@intel.com>> wrote:
> > Hi Ard,
> > I don't see you add below code for case R_X86_64_PLT32. Is it right?
> >
> > *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
> > + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
> > - (SecOffset - SecShdr->sh_addr));
> >
>
> Isn't it identical to the code for R_X86_64_PC32?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 8:58 ` Shi, Steven
@ 2016-08-04 9:01 ` Ard Biesheuvel
0 siblings, 0 replies; 7+ messages in thread
From: Ard Biesheuvel @ 2016-08-04 9:01 UTC (permalink / raw)
To: Shi, Steven
Cc: Zhu, Yonghong, Gao, Liming, Justen, Jordan L,
edk2-devel@lists.01.org, mischief@offblast.org
On 4 August 2016 at 10:58, Shi, Steven <steven.shi@intel.com> wrote:
> OK, it is. But it is a bit not very clear.
>
Did you read the elaborate comment block explaining that (and why) it
is appropriate to treat R_X86_64_PLT32 as a R_X86_64_PC32 relocation?
This is not generally true, but it is true for UEFI since we don't
support shared libraries.
So I think it is incorrect to simply duplicate the code for
R_X86_64_PC32 without mentioning that, and suggesting that the PLT
relocation receive some kind of treatment that is different.
Thanks,
Ard.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 8:45 [PATCH] BaseTools X64: fold PLT relocations into simple relative references Ard Biesheuvel
2016-08-04 8:54 ` Shi, Steven
@ 2016-08-04 19:03 ` Nicolas Owens
2016-08-04 19:11 ` Ard Biesheuvel
1 sibling, 1 reply; 7+ messages in thread
From: Nicolas Owens @ 2016-08-04 19:03 UTC (permalink / raw)
To: Ard Biesheuvel, steven.shi, yonghong.zhu, liming.gao,
jordan.l.justen, edk2-devel
ard,
i think you need to have R_X86_64_PLT32 case in WriteRelocations64.
without that, i still hit the invalid relocation message.
On 08/04/2016 01:45 AM, Ard Biesheuvel wrote:
> For X64/GCC, we use position independent code with hidden visibility
> to inform the compiler that symbols references are never resolved at
> runtime, which removes the need for PLTs and GOTs. However, in some
> cases GCC has been reported to still emit PLT based relocations, which
> we need to handle in the ELF to PE/COFF perform by GenFw.
>
> Unlike GOT based relocations, which are non-trivial to handle since the
> indirections in the code can not be fixed up easily (although relocation
> types exist for X64 that annotate relocation targets as suitable for
> relaxation), PLT relocations simply point to jump targets, and we can
> relax such relocations by resolving them using the symbol directly rather
> than via a PLT entry that does nothing more than tail call the function
> we already know it is going to call (since all symbol references are
> resolved in the same module).
>
> So handle R_X86_64_PLT32 as a R_X86_64_PC32 relocation.
>
> Suggested-by: Steven Shi <steven.shi@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
> BaseTools/Source/C/GenFw/Elf64Convert.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c
> index 944c94b8f8b4..7cbff0df0996 100644
> --- a/BaseTools/Source/C/GenFw/Elf64Convert.c
> +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
> @@ -785,6 +785,17 @@ WriteSections64 (
> *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
> VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
> break;
> +
> + case R_X86_64_PLT32:
> + //
> + // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
> + // possible since we know all code symbol references resolve to
> + // definitions in the same module (UEFI has no shared libraries),
> + // and so there is never a reason to jump via a PLT entry,
> + // allowing us to resolve the reference using the symbol directly.
> + //
> + VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
> + /* fall through */
> case R_X86_64_PC32:
> //
> // Relative relocation: Symbol - Ip + Addend
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] BaseTools X64: fold PLT relocations into simple relative references
2016-08-04 19:03 ` Nicolas Owens
@ 2016-08-04 19:11 ` Ard Biesheuvel
0 siblings, 0 replies; 7+ messages in thread
From: Ard Biesheuvel @ 2016-08-04 19:11 UTC (permalink / raw)
To: Nicolas Owens
Cc: steven.shi, yonghong.zhu, liming.gao, jordan.l.justen, edk2-devel
> On 4 aug. 2016, at 21:03, Nicolas Owens <mischief@offblast.org> wrote:
>
> ard,
>
> i think you need to have R_X86_64_PLT32 case in WriteRelocations64.
> without that, i still hit the invalid relocation message.
>
Good point. I will send out a v2 tomorrow
>> On 08/04/2016 01:45 AM, Ard Biesheuvel wrote:
>> For X64/GCC, we use position independent code with hidden visibility
>> to inform the compiler that symbols references are never resolved at
>> runtime, which removes the need for PLTs and GOTs. However, in some
>> cases GCC has been reported to still emit PLT based relocations, which
>> we need to handle in the ELF to PE/COFF perform by GenFw.
>>
>> Unlike GOT based relocations, which are non-trivial to handle since the
>> indirections in the code can not be fixed up easily (although relocation
>> types exist for X64 that annotate relocation targets as suitable for
>> relaxation), PLT relocations simply point to jump targets, and we can
>> relax such relocations by resolving them using the symbol directly rather
>> than via a PLT entry that does nothing more than tail call the function
>> we already know it is going to call (since all symbol references are
>> resolved in the same module).
>>
>> So handle R_X86_64_PLT32 as a R_X86_64_PC32 relocation.
>>
>> Suggested-by: Steven Shi <steven.shi@intel.com>
>> Contributed-under: TianoCore Contribution Agreement 1.0
>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>> ---
>> BaseTools/Source/C/GenFw/Elf64Convert.c | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c
>> index 944c94b8f8b4..7cbff0df0996 100644
>> --- a/BaseTools/Source/C/GenFw/Elf64Convert.c
>> +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
>> @@ -785,6 +785,17 @@ WriteSections64 (
>> *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
>> VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
>> break;
>> +
>> + case R_X86_64_PLT32:
>> + //
>> + // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
>> + // possible since we know all code symbol references resolve to
>> + // definitions in the same module (UEFI has no shared libraries),
>> + // and so there is never a reason to jump via a PLT entry,
>> + // allowing us to resolve the reference using the symbol directly.
>> + //
>> + VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
>> + /* fall through */
>> case R_X86_64_PC32:
>> //
>> // Relative relocation: Symbol - Ip + Addend
>>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2016-08-04 19:11 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-04 8:45 [PATCH] BaseTools X64: fold PLT relocations into simple relative references Ard Biesheuvel
2016-08-04 8:54 ` Shi, Steven
2016-08-04 8:54 ` Ard Biesheuvel
2016-08-04 8:58 ` Shi, Steven
2016-08-04 9:01 ` Ard Biesheuvel
2016-08-04 19:03 ` Nicolas Owens
2016-08-04 19:11 ` Ard Biesheuvel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox