diff mbox series

[v5,3/6] firmware: Explicitly pass -pie to the linker, not just the driver

Message ID 20210711022824.29915-4-jrtc27@jrtc27.com
State Accepted
Headers show
Series Fully support standalone Clang/LLVM toolchains | expand

Commit Message

Jessica Clarke July 11, 2021, 2:28 a.m. UTC
When using Clang with a bare-metal triple, -pie does not get passed to
the linker as it's not normally a thing that makes sense, unlike GCC
which will unconditionally forward it on and potentially result in a
linker error. However, LLD does support it, and manually forwarding it
on works as desired, so do so to fully support FW_PIC with Clang and
LLD.

Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
---
 firmware/objects.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Bin Meng July 11, 2021, 12:15 p.m. UTC | #1
On Sun, Jul 11, 2021 at 10:29 AM Jessica Clarke <jrtc27@jrtc27.com> wrote:
>
> When using Clang with a bare-metal triple, -pie does not get passed to
> the linker as it's not normally a thing that makes sense, unlike GCC
> which will unconditionally forward it on and potentially result in a
> linker error. However, LLD does support it, and manually forwarding it
> on works as desired, so do so to fully support FW_PIC with Clang and
> LLD.
>
> Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
> ---
>  firmware/objects.mk | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Bin Meng July 11, 2021, 1:05 p.m. UTC | #2
On Sun, Jul 11, 2021 at 10:29 AM Jessica Clarke <jrtc27@jrtc27.com> wrote:
>
> When using Clang with a bare-metal triple, -pie does not get passed to
> the linker as it's not normally a thing that makes sense, unlike GCC
> which will unconditionally forward it on and potentially result in a

It seems GCC driver does not unconditionally forward it to the linker, see:

$ riscv64-unknown-elf-gcc -fPIE -nostdlib -x c /dev/null -o /dev/null
2>&1 && echo y
/opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
warning: cannot find entry symbol _start; defaulting to
0000000000010040
y

But

$ riscv64-unknown-elf-gcc -fPIE -nostdlib -Wl,-pie -x c /dev/null -o
/dev/null 2>&1 && echo y
/opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
-pie not supported
collect2: error: ld returned 1 exit status

So regarding bare-metal triple, GCC driver and  clang driver behave
the same way. It's just GNU bare-metal ld does not support PIE, while
LLD supports.

So we need to revise the above commit message.

> linker error. However, LLD does support it, and manually forwarding it
> on works as desired, so do so to fully support FW_PIC with Clang and
> LLD.
>
> Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
> ---
>  firmware/objects.mk | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>

Regards,
Bin
Jessica Clarke July 11, 2021, 1:06 p.m. UTC | #3
On 11 Jul 2021, at 14:05, Bin Meng <bmeng.cn@gmail.com> wrote:
> 
> On Sun, Jul 11, 2021 at 10:29 AM Jessica Clarke <jrtc27@jrtc27.com> wrote:
>> 
>> When using Clang with a bare-metal triple, -pie does not get passed to
>> the linker as it's not normally a thing that makes sense, unlike GCC
>> which will unconditionally forward it on and potentially result in a
> 
> It seems GCC driver does not unconditionally forward it to the linker, see:
> 
> $ riscv64-unknown-elf-gcc -fPIE -nostdlib -x c /dev/null -o /dev/null
> 2>&1 && echo y
> /opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
> warning: cannot find entry symbol _start; defaulting to
> 0000000000010040
> y
> 
> But
> 
> $ riscv64-unknown-elf-gcc -fPIE -nostdlib -Wl,-pie -x c /dev/null -o
> /dev/null 2>&1 && echo y
> /opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
> -pie not supported
> collect2: error: ld returned 1 exit status
> 
> So regarding bare-metal triple, GCC driver and  clang driver behave
> the same way. It's just GNU bare-metal ld does not support PIE, while
> LLD supports.
> 
> So we need to revise the above commit message.

You didn’t pass -pie, only -fPIE.

Jess

>> linker error. However, LLD does support it, and manually forwarding it
>> on works as desired, so do so to fully support FW_PIC with Clang and
>> LLD.
>> 
>> Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
>> ---
>> firmware/objects.mk | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
> 
> Regards,
> Bin
Bin Meng July 11, 2021, 1:08 p.m. UTC | #4
On Sun, Jul 11, 2021 at 9:06 PM Jessica Clarke <jrtc27@jrtc27.com> wrote:
>
> On 11 Jul 2021, at 14:05, Bin Meng <bmeng.cn@gmail.com> wrote:
> >
> > On Sun, Jul 11, 2021 at 10:29 AM Jessica Clarke <jrtc27@jrtc27.com> wrote:
> >>
> >> When using Clang with a bare-metal triple, -pie does not get passed to
> >> the linker as it's not normally a thing that makes sense, unlike GCC
> >> which will unconditionally forward it on and potentially result in a
> >
> > It seems GCC driver does not unconditionally forward it to the linker, see:
> >
> > $ riscv64-unknown-elf-gcc -fPIE -nostdlib -x c /dev/null -o /dev/null
> > 2>&1 && echo y
> > /opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
> > warning: cannot find entry symbol _start; defaulting to
> > 0000000000010040
> > y
> >
> > But
> >
> > $ riscv64-unknown-elf-gcc -fPIE -nostdlib -Wl,-pie -x c /dev/null -o
> > /dev/null 2>&1 && echo y
> > /opt/riscv-unknown-elf/lib/gcc/riscv64-unknown-elf/10.1.0/../../../../riscv64-unknown-elf/bin/ld:
> > -pie not supported
> > collect2: error: ld returned 1 exit status
> >
> > So regarding bare-metal triple, GCC driver and  clang driver behave
> > the same way. It's just GNU bare-metal ld does not support PIE, while
> > LLD supports.
> >
> > So we need to revise the above commit message.
>
> You didn’t pass -pie, only -fPIE.

Ah, yes. Indeed -pie is the linker option. Then we are good.

Regards,
Bin
Anup Patel July 11, 2021, 2:37 p.m. UTC | #5
On Sun, Jul 11, 2021 at 5:45 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> On Sun, Jul 11, 2021 at 10:29 AM Jessica Clarke <jrtc27@jrtc27.com> wrote:
> >
> > When using Clang with a bare-metal triple, -pie does not get passed to
> > the linker as it's not normally a thing that makes sense, unlike GCC
> > which will unconditionally forward it on and potentially result in a
> > linker error. However, LLD does support it, and manually forwarding it
> > on works as desired, so do so to fully support FW_PIC with Clang and
> > LLD.
> >
> > Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
> > ---
> >  firmware/objects.mk | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
>
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
> Tested-by: Bin Meng <bmeng.cn@gmail.com>

Reviewed-by: Anup Patel <anup.patel@wdc.com>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
diff mbox series

Patch

diff --git a/firmware/objects.mk b/firmware/objects.mk
index ce91c2f..3bc83cd 100644
--- a/firmware/objects.mk
+++ b/firmware/objects.mk
@@ -21,7 +21,7 @@  ifeq ($(FW_PIC),y)
 firmware-genflags-y +=	-DFW_PIC
 firmware-asflags-y  +=	-fpic
 firmware-cflags-y   +=	-fPIE -pie
-firmware-ldflags-y  +=  -Wl,--no-dynamic-linker
+firmware-ldflags-y  +=  -Wl,--no-dynamic-linker -Wl,-pie
 endif
 
 ifdef FW_TEXT_START