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 <dki...@net-space.pl> 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 <phco...@gmail.com>
>
> 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

Reply via email to