Re: [PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-04-05 Thread Daniel Kiper
On Fri, Apr 05, 2024 at 12:39:41AM +0300, Vladimir 'phcoder' Serbinenko wrote:
> I didn't know about using weakref for this but I'm fine with the approach. 
> Just
> one thing: can we condition it on HAVE_ASM_USCORE test instead of platform?

Interestingly it is set to 0 for x86_64 MinGW and other Linux builds
and set to 1 for i686 MinGW. And it looks only x86_64 MinGW builds are
broken. So, the HAVE_ASM_USCORE itself would not work for us. Instead
I would do s/_WIN32/_WIN64/ in the ifdefery below to improve detection.
AFAICT the "#if defined(_WIN32) && !defined(__CYGWIN__)" is common
mechanism to detect MinGW in general.

> Le jeu. 4 avr. 2024, 23:47, Daniel Kiper  a écrit :
>  Adding Ard, Glenn and Dave...
>
>  First of all, sorry for late reply but I was busy with other stuff...
>
>  On Fri, Mar 15, 2024 at 09:43:22PM +0300, Vladimir 'phcoder'
>  Serbinenko wrote:
>  > stack protector needs symbols with just one underscore in C
>  > name unlike unix variant that needs double underscore.
>  > Supply both symbols for simplicity
>  >
>  > Signed-off-by: Vladimir Serbinenko 
>
>  I do not like your solution because it adds unneeded code/symbols to
>  some EFI builds. I come up with two other solutions.
>
>  First, let's define an alias when using MinGW build environment:
>
>  diff --git a/include/grub/stack_protector.h b/include/grub/
>  stack_protector.h
>  index c88dc00b5..8d99fd50e 100644
>  --- a/include/grub/stack_protector.h
>  +++ b/include/grub/stack_protector.h
>  @@ -25,6 +25,10 @@
>   #ifdef GRUB_STACK_PROTECTOR
>   extern grub_addr_t EXPORT_VAR (__stack_chk_guard);
>   extern void __attribute__ ((noreturn)) EXPORT_FUNC (__stack_chk_fail) 
> (void);
>  +#if defined(_WIN32) && !defined(__CYGWIN__)
>  +static grub_addr_t __attribute__ ((weakref("__stack_chk_guard"))) 
> EXPORT_VAR (_stack_chk_guard);
>  +static void __attribute__ ((noreturn, weakref("__stack_chk_fail"))) 
> EXPORT_FUNC (_stack_chk_fail) (void);
>  +#endif
>   #endif
>
>   #endif /* GRUB_STACK_PROTECTOR_H */

Daniel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-04-04 Thread Vladimir 'phcoder' Serbinenko
I didn't know about using weakref for this but I'm fine with the approach.
Just one thing: can we condition it on HAVE_ASM_USCORE test instead of
platform?

Le jeu. 4 avr. 2024, 23:47, Daniel Kiper  a écrit :

> Adding Ard, Glenn and Dave...
>
> First of all, sorry for late reply but I was busy with other stuff...
>
> On Fri, Mar 15, 2024 at 09:43:22PM +0300, Vladimir 'phcoder' Serbinenko
> wrote:
> > stack protector needs symbols with just one underscore in C
> > name unlike unix variant that needs double underscore.
> > Supply both symbols for simplicity
> >
> > Signed-off-by: Vladimir Serbinenko 
>
> I do not like your solution because it adds unneeded code/symbols to
> some EFI builds. I come up with two other solutions.
>
> First, let's define an alias when using MinGW build environment:
>
> diff --git a/include/grub/stack_protector.h
> b/include/grub/stack_protector.h
> index c88dc00b5..8d99fd50e 100644
> --- a/include/grub/stack_protector.h
> +++ b/include/grub/stack_protector.h
> @@ -25,6 +25,10 @@
>  #ifdef GRUB_STACK_PROTECTOR
>  extern grub_addr_t EXPORT_VAR (__stack_chk_guard);
>  extern void __attribute__ ((noreturn)) EXPORT_FUNC (__stack_chk_fail)
> (void);
> +#if defined(_WIN32) && !defined(__CYGWIN__)
> +static grub_addr_t __attribute__ ((weakref("__stack_chk_guard")))
> EXPORT_VAR (_stack_chk_guard);
> +static void __attribute__ ((noreturn, weakref("__stack_chk_fail")))
> EXPORT_FUNC (_stack_chk_fail) (void);
> +#endif
>  #endif
>
>  #endif /* GRUB_STACK_PROTECTOR_H */
>
> Tested and it works. We have to use weakref() instead of alias() due to
> definition in separate translation unit.
>
> Second, do not drop leading underscore:
>
> diff --git a/conf/Makefile.common b/conf/Makefile.common
> index b8f216f6c..fd20f6b4e 100644
> --- a/conf/Makefile.common
> +++ b/conf/Makefile.common
> @@ -38,7 +38,7 @@ CCASFLAGS_DEFAULT = $(CPPFLAGS_DEFAULT) -DASM_FILE=1
>  BUILD_CPPFLAGS += $(CPPFLAGS_DEFAULT)
>
>  CFLAGS_KERNEL = $(CFLAGS_PLATFORM) -ffreestanding
> -LDFLAGS_KERNEL = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> +LDFLAGS_KERNEL = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> -Wl,--leading-underscore
>  CPPFLAGS_KERNEL = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM) -DGRUB_KERNEL=1
>  CCASFLAGS_KERNEL = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
>  STRIPFLAGS_KERNEL = -R .rel.dyn -R .reginfo -R .note -R .comment -R
> .drectve -R .note.gnu.gold-version -R .MIPS.abiflags -R .ARM.exidx
> @@ -51,12 +51,12 @@ endif
>  endif
>
>  CFLAGS_MODULE = $(CFLAGS_PLATFORM) -ffreestanding
> -LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> -Wl,-r
> +LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> -Wl,-r -Wl,--leading-underscore
>  CPPFLAGS_MODULE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
>  CCASFLAGS_MODULE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
>
>  CFLAGS_IMAGE = $(CFLAGS_PLATFORM) -fno-builtin
> -LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> -Wl,-S
> +LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
> -Wl,-S -Wl,--leading-underscore
>  CPPFLAGS_IMAGE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
>  CCASFLAGS_IMAGE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
>
> diff --git a/grub-core/genmod.sh.in b/grub-core/genmod.sh.in
> index e57c4d920..04b680ad0 100644
> --- a/grub-core/genmod.sh.in
> +++ b/grub-core/genmod.sh.in
> @@ -83,9 +83,9 @@ else
>  for dep in $deps; do echo "char moddep_$dep[] __attribute__
> ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
>
>  if test -n "$deps"; then
> -   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o
> $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
> +   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib
> -Wl,--leading-underscore -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
>  else
> -   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o
> $tmpfile2 $t1 $tmpfile -Wl,-r
> +   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib
> -Wl,--leading-underscore -o $tmpfile2 $t1 $tmpfile -Wl,-r
>  fi
>  rm -f $t1 $t2 $tmpfile
>  mv $tmpfile2 $tmpfile
> diff --git a/util/grub-pe2elf.c b/util/grub-pe2elf.c
> index 11331294f..233118252 100644
> --- a/util/grub-pe2elf.c
> +++ b/util/grub-pe2elf.c
> @@ -71,7 +71,7 @@ insert_string (const char *name)
>  {
>int len, result;
>
> -  if (*name == '_')
> +  if (*name == '_' && !strcmp("__stack_chk_fail", name) &&
> !strcmp("__stack_chk_guard", name))
>  name++;
>
>len = strlen (name);
>
> This one works too. However, it breaks normal builds. So, it requires some
> tweaking to make it production ready.
>
> Anyway, I prefer first solution because it is simpler. I do not mention
> that the second one may break other symbols with leading underscore.
>
> What do you think?
>
> Daniel
>
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-04-04 Thread Daniel Kiper
On Wed, Apr 03, 2024 at 04:27:39PM -0500, Glenn Washburn wrote:
> On Fri, 15 Mar 2024 21:43:22 +0300
> "Vladimir 'phcoder' Serbinenko"  wrote:
>
> > stack protector needs symbols with just one underscore in C
> > name unlike unix variant that needs double underscore.
> > Supply both symbols for simplicity
> >
> > Signed-off-by: Vladimir Serbinenko 
> >
>
> Instead of essentially duplicating symbols and cluttering the code. Why
> can't we use the same technique in commit b0da8d3d35 which uses the
> linker to define an alias symbol?

Yeah, we can use almost the same thing. Please take a look at my other email.

Daniel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-04-04 Thread Daniel Kiper
Adding Ard, Glenn and Dave...

First of all, sorry for late reply but I was busy with other stuff...

On Fri, Mar 15, 2024 at 09:43:22PM +0300, Vladimir 'phcoder' Serbinenko wrote:
> stack protector needs symbols with just one underscore in C
> name unlike unix variant that needs double underscore.
> Supply both symbols for simplicity
>
> Signed-off-by: Vladimir Serbinenko 

I do not like your solution because it adds unneeded code/symbols to
some EFI builds. I come up with two other solutions.

First, let's define an alias when using MinGW build environment:

diff --git a/include/grub/stack_protector.h b/include/grub/stack_protector.h
index c88dc00b5..8d99fd50e 100644
--- a/include/grub/stack_protector.h
+++ b/include/grub/stack_protector.h
@@ -25,6 +25,10 @@
 #ifdef GRUB_STACK_PROTECTOR
 extern grub_addr_t EXPORT_VAR (__stack_chk_guard);
 extern void __attribute__ ((noreturn)) EXPORT_FUNC (__stack_chk_fail) (void);
+#if defined(_WIN32) && !defined(__CYGWIN__)
+static grub_addr_t __attribute__ ((weakref("__stack_chk_guard"))) EXPORT_VAR 
(_stack_chk_guard);
+static void __attribute__ ((noreturn, weakref("__stack_chk_fail"))) 
EXPORT_FUNC (_stack_chk_fail) (void);
+#endif
 #endif

 #endif /* GRUB_STACK_PROTECTOR_H */

Tested and it works. We have to use weakref() instead of alias() due to
definition in separate translation unit.

Second, do not drop leading underscore:

diff --git a/conf/Makefile.common b/conf/Makefile.common
index b8f216f6c..fd20f6b4e 100644
--- a/conf/Makefile.common
+++ b/conf/Makefile.common
@@ -38,7 +38,7 @@ CCASFLAGS_DEFAULT = $(CPPFLAGS_DEFAULT) -DASM_FILE=1
 BUILD_CPPFLAGS += $(CPPFLAGS_DEFAULT)

 CFLAGS_KERNEL = $(CFLAGS_PLATFORM) -ffreestanding
-LDFLAGS_KERNEL = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
+LDFLAGS_KERNEL = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) 
-Wl,--leading-underscore
 CPPFLAGS_KERNEL = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM) -DGRUB_KERNEL=1
 CCASFLAGS_KERNEL = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
 STRIPFLAGS_KERNEL = -R .rel.dyn -R .reginfo -R .note -R .comment -R .drectve 
-R .note.gnu.gold-version -R .MIPS.abiflags -R .ARM.exidx
@@ -51,12 +51,12 @@ endif
 endif

 CFLAGS_MODULE = $(CFLAGS_PLATFORM) -ffreestanding
-LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) 
-Wl,-r
+LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) 
-Wl,-r -Wl,--leading-underscore
 CPPFLAGS_MODULE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
 CCASFLAGS_MODULE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)

 CFLAGS_IMAGE = $(CFLAGS_PLATFORM) -fno-builtin
-LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-S
+LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) 
-Wl,-S -Wl,--leading-underscore
 CPPFLAGS_IMAGE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
 CCASFLAGS_IMAGE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)

diff --git a/grub-core/genmod.sh.in b/grub-core/genmod.sh.in
index e57c4d920..04b680ad0 100644
--- a/grub-core/genmod.sh.in
+++ b/grub-core/genmod.sh.in
@@ -83,9 +83,9 @@ else
 for dep in $deps; do echo "char moddep_$dep[] __attribute__ 
((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done

 if test -n "$deps"; then
-   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 
$t2 $tmpfile -Wl,-r
+   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib 
-Wl,--leading-underscore -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
 else
-   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 
$tmpfile -Wl,-r
+   @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib 
-Wl,--leading-underscore -o $tmpfile2 $t1 $tmpfile -Wl,-r
 fi
 rm -f $t1 $t2 $tmpfile
 mv $tmpfile2 $tmpfile
diff --git a/util/grub-pe2elf.c b/util/grub-pe2elf.c
index 11331294f..233118252 100644
--- a/util/grub-pe2elf.c
+++ b/util/grub-pe2elf.c
@@ -71,7 +71,7 @@ insert_string (const char *name)
 {
   int len, result;

-  if (*name == '_')
+  if (*name == '_' && !strcmp("__stack_chk_fail", name) && 
!strcmp("__stack_chk_guard", name))
 name++;

   len = strlen (name);

This one works too. However, it breaks normal builds. So, it requires some
tweaking to make it production ready.

Anyway, I prefer first solution because it is simpler. I do not mention
that the second one may break other symbols with leading underscore.

What do you think?

Daniel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-04-03 Thread Glenn Washburn
On Fri, 15 Mar 2024 21:43:22 +0300
"Vladimir 'phcoder' Serbinenko"  wrote:

> stack protector needs symbols with just one underscore in C
> name unlike unix variant that needs double underscore.
> Supply both symbols for simplicity
> 
> Signed-off-by: Vladimir Serbinenko 
> 

Instead of essentially duplicating symbols and cluttering the code. Why
can't we use the same technique in commit b0da8d3d35 which uses the
linker to define an alias symbol?

Glenn

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH] STACK_PROTECTOR: Support symbols emitted by windows compiler

2024-03-15 Thread Vladimir 'phcoder' Serbinenko
stack protector needs symbols with just one underscore in C
name unlike unix variant that needs double underscore.
Supply both symbols for simplicity

Signed-off-by: Vladimir Serbinenko 

-- 
Regards
Vladimir 'phcoder' Serbinenko


0001-STACK_PROTECTOR-Support-symbols-emitted-by-windows-c.patch
Description: Binary data
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel