diff mbox series

[2/2] kvm: Fix undefined reference to __stack_chk_fail()

Message ID 20220606184320.8210-3-petr.vorel@gmail.com
State Accepted
Headers show
Series LTP compilation fixes for buildroot toolchains | expand

Commit Message

Petr Vorel June 6, 2022, 6:43 p.m. UTC
Some x86_64 buildroot toolchains (bootlin-x86-64-glibc,
bootlin-x86-64-musl) try to link to __stack_chk_fail().
-nostdlib is not enough, it requires also -fstack-protector.

x86_64-buildroot-linux-gnu/bin/ld: /tmp/ccgBXEoR.o: in function `handle_page_fault':
kvm_pagefault01.c:(.text+0x5d): undefined reference to `__stack_chk_fail'
collect2: error: ld returned 1 exit status

Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
---
 testcases/kernel/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Cyril Hrubis June 8, 2022, 2:36 p.m. UTC | #1
Hi!
> Some x86_64 buildroot toolchains (bootlin-x86-64-glibc,
> bootlin-x86-64-musl) try to link to __stack_chk_fail().
> -nostdlib is not enough, it requires also -fstack-protector.
>
> x86_64-buildroot-linux-gnu/bin/ld: /tmp/ccgBXEoR.o: in function `handle_page_fault':
> kvm_pagefault01.c:(.text+0x5d): undefined reference to `__stack_chk_fail'
> collect2: error: ld returned 1 exit status

I'm still puzzled on why does it try to link it. Why is stack protection
turned on for the code. Does buildroot pass -fstack-protector to the
CFLAGS?

I guess that -nostdlib causes the libssp (which implements
__stack_chk_fail() not to be linked by default, but the
__stack_chk_fail() shouldn't be called unless unless the feature is
enabled in the first place.

Also does -fno-stack-protector passed in GUEST_CFLAGS make it disappear?

> Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
> ---
>  testcases/kernel/kvm/Makefile | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
> index 8d5193d8e..bce1a4eb5 100644
> --- a/testcases/kernel/kvm/Makefile
> +++ b/testcases/kernel/kvm/Makefile
> @@ -9,7 +9,7 @@ ASFLAGS =
>  CPPFLAGS += -I$(abs_srcdir)/include
>  GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
>  GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
> -GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
> +GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector
>  GUEST_LDLIBS =
>  
>  FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86
> -- 
> 2.36.1
>
Martin Doucha June 9, 2022, 8:26 a.m. UTC | #2
On 06. 06. 22 20:43, Petr Vorel wrote:
> Some x86_64 buildroot toolchains (bootlin-x86-64-glibc,
> bootlin-x86-64-musl) try to link to __stack_chk_fail().
> -nostdlib is not enough, it requires also -fstack-protector.
> 
> x86_64-buildroot-linux-gnu/bin/ld: /tmp/ccgBXEoR.o: in function `handle_page_fault':
> kvm_pagefault01.c:(.text+0x5d): undefined reference to `__stack_chk_fail'
> collect2: error: ld returned 1 exit status
> 
> Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
> ---
>  testcases/kernel/kvm/Makefile | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
> index 8d5193d8e..bce1a4eb5 100644
> --- a/testcases/kernel/kvm/Makefile
> +++ b/testcases/kernel/kvm/Makefile
> @@ -9,7 +9,7 @@ ASFLAGS =
>  CPPFLAGS += -I$(abs_srcdir)/include
>  GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
>  GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
> -GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
> +GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector

We should use -fno-stack-protector here instead. Your patch probably
enables linking of libssp despite -nostdlib which we don't want. The GCC
stack protector may also break tests because bootstrap initializes stack
manually instead of letting GCC handle it.

>  GUEST_LDLIBS =
>  
>  FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86
Thomas Petazzoni June 9, 2022, 12:26 p.m. UTC | #3
On Thu, 9 Jun 2022 10:26:55 +0200
Martin Doucha <mdoucha@suse.cz> wrote:

> > diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
> > index 8d5193d8e..bce1a4eb5 100644
> > --- a/testcases/kernel/kvm/Makefile
> > +++ b/testcases/kernel/kvm/Makefile
> > @@ -9,7 +9,7 @@ ASFLAGS =
> >  CPPFLAGS += -I$(abs_srcdir)/include
> >  GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
> >  GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
> > -GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
> > +GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector  
> 
> We should use -fno-stack-protector here instead. Your patch probably
> enables linking of libssp despite -nostdlib which we don't want. The GCC
> stack protector may also break tests because bootstrap initializes stack
> manually instead of letting GCC handle it.

I agree. Assuming -nostdlib means you're building freestanding code,
then indeed, -fno-stack-protector is most likely what you want.

Thomas
Joerg Vehlow June 10, 2022, 7:56 a.m. UTC | #4
Hi,


Am 6/9/2022 um 10:26 AM schrieb Martin Doucha:
> On 06. 06. 22 20:43, Petr Vorel wrote:
>> Some x86_64 buildroot toolchains (bootlin-x86-64-glibc,
>> bootlin-x86-64-musl) try to link to __stack_chk_fail().
>> -nostdlib is not enough, it requires also -fstack-protector.
>>
>> x86_64-buildroot-linux-gnu/bin/ld: /tmp/ccgBXEoR.o: in function `handle_page_fault':
>> kvm_pagefault01.c:(.text+0x5d): undefined reference to `__stack_chk_fail'
>> collect2: error: ld returned 1 exit status
>>
>> Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
>> ---
>>  testcases/kernel/kvm/Makefile | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
>> index 8d5193d8e..bce1a4eb5 100644
>> --- a/testcases/kernel/kvm/Makefile
>> +++ b/testcases/kernel/kvm/Makefile
>> @@ -9,7 +9,7 @@ ASFLAGS =
>>  CPPFLAGS += -I$(abs_srcdir)/include
>>  GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
>>  GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
>> -GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
>> +GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector
> 
> We should use -fno-stack-protector here instead. Your patch probably
> enables linking of libssp despite -nostdlib which we don't want. The GCC
> stack protector may also break tests because bootstrap initializes stack
> manually instead of letting GCC handle it.
I can confirm, that adding -fno-stack-protector to GUEST_LDFLAGS fixes
the linker error.

> 
>>  GUEST_LDLIBS =
>>  
>>  FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86
> 
> 

Joerg
Petr Vorel June 13, 2022, 9:31 p.m. UTC | #5
Hi all,

> Hi,


> Am 6/9/2022 um 10:26 AM schrieb Martin Doucha:
> > On 06. 06. 22 20:43, Petr Vorel wrote:
> >> Some x86_64 buildroot toolchains (bootlin-x86-64-glibc,
> >> bootlin-x86-64-musl) try to link to __stack_chk_fail().
> >> -nostdlib is not enough, it requires also -fstack-protector.

> >> x86_64-buildroot-linux-gnu/bin/ld: /tmp/ccgBXEoR.o: in function `handle_page_fault':
> >> kvm_pagefault01.c:(.text+0x5d): undefined reference to `__stack_chk_fail'
> >> collect2: error: ld returned 1 exit status

> >> Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
> >> ---
> >>  testcases/kernel/kvm/Makefile | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)

> >> diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
> >> index 8d5193d8e..bce1a4eb5 100644
> >> --- a/testcases/kernel/kvm/Makefile
> >> +++ b/testcases/kernel/kvm/Makefile
> >> @@ -9,7 +9,7 @@ ASFLAGS =
> >>  CPPFLAGS += -I$(abs_srcdir)/include
> >>  GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
> >>  GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
> >> -GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
> >> +GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector

> > We should use -fno-stack-protector here instead. Your patch probably
> > enables linking of libssp despite -nostdlib which we don't want. The GCC
> > stack protector may also break tests because bootstrap initializes stack
> > manually instead of letting GCC handle it.
> I can confirm, that adding -fno-stack-protector to GUEST_LDFLAGS fixes
> the linker error.

Thanks a lot for your input. Martin, thanks a lot for testing.
I also verified that -fno-stack-protector fix compilation, thus patchset merged.

Kind regards,
Petr

> >>  GUEST_LDLIBS =

> >>  FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86



> Joerg
diff mbox series

Patch

diff --git a/testcases/kernel/kvm/Makefile b/testcases/kernel/kvm/Makefile
index 8d5193d8e..bce1a4eb5 100644
--- a/testcases/kernel/kvm/Makefile
+++ b/testcases/kernel/kvm/Makefile
@@ -9,7 +9,7 @@  ASFLAGS =
 CPPFLAGS += -I$(abs_srcdir)/include
 GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
 GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
-GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
+GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fstack-protector
 GUEST_LDLIBS =
 
 FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86