Re: [PATCH] init/Kconfig: fix CC_HAS_ASM_GOTO_TIED_OUTPUT test with dash

From: Masahiro Yamada
Date: Thu Aug 18 2022 - 13:06:45 EST


On Fri, Aug 5, 2022 at 4:03 AM <alexandre.belloni@xxxxxxxxxxx> wrote:
>
> From: Alexandre Belloni <alexandre.belloni@xxxxxxxxxxx>
>
> When using dash as /bin/sh, the CC_HAS_ASM_GOTO_TIED_OUTPUT test fails
> with a syntax error which is not the one we are looking for:
>
> <stdin>: In function ‘foo’:
> <stdin>:1:29: warning: missing terminating " character
> <stdin>:1:29: error: missing terminating " character
> <stdin>:2:5: error: expected ‘:’ before ‘+’ token
> <stdin>:2:7: warning: missing terminating " character
> <stdin>:2:7: error: missing terminating " character
> <stdin>:2:5: error: expected declaration or statement at end of input
>
> Move all the CC_HAS_ASM_GOTO tests to scripts/gcc-goto.sh to solve the
> escaping issues.
>
> Fixes: 1aa0e8b144b6 ("Kconfig: Add option for asm goto w/ tied outputs to workaround clang-13 bug")
> Signed-off-by: Alexandre Belloni <alexandre.belloni@xxxxxxxxxxx>
> ---
> init/Kconfig | 6 +++---
> scripts/gcc-goto.sh | 31 +++++++++++++++++++++++++++++++
> 2 files changed, 34 insertions(+), 3 deletions(-)
>
> diff --git a/init/Kconfig b/init/Kconfig
> index c984afc489de..9903a11cfe7d 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -71,16 +71,16 @@ config CC_CAN_LINK_STATIC
> default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
>
> config CC_HAS_ASM_GOTO
> - def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
> + def_bool $(success,$(srctree)/scripts/gcc-goto.sh goto $(CC))
>
> config CC_HAS_ASM_GOTO_OUTPUT
> depends on CC_HAS_ASM_GOTO
> - def_bool $(success,echo 'int foo(int x) { asm goto ("": "=r"(x) ::: bar); return x; bar: return 0; }' | $(CC) -x c - -c -o /dev/null)
> + def_bool $(success,$(srctree)/scripts/gcc-goto.sh goto_output $(CC))
>
> config CC_HAS_ASM_GOTO_TIED_OUTPUT
> depends on CC_HAS_ASM_GOTO_OUTPUT
> # Detect buggy gcc and clang, fixed in gcc-11 clang-14.
> - def_bool $(success,echo 'int foo(int *x) { asm goto (".long (%l[bar]) - .\n": "+m"(*x) ::: bar); return *x; bar: return 0; }' | $CC -x c - -c -o /dev/null)
> + def_bool $(success,$(srctree)/scripts/gcc-goto.sh goto_tied_output $(CC))
>
> config TOOLS_SUPPORT_RELR
> def_bool $(success,env "CC=$(CC)" "LD=$(LD)" "NM=$(NM)" "OBJCOPY=$(OBJCOPY)" $(srctree)/scripts/tools-support-relr.sh)
> diff --git a/scripts/gcc-goto.sh b/scripts/gcc-goto.sh
> index 8b980fb2270a..aa9498b74df8 100755
> --- a/scripts/gcc-goto.sh
> +++ b/scripts/gcc-goto.sh
> @@ -3,6 +3,11 @@
> # Test for gcc 'asm goto' support
> # Copyright (C) 2010, Jason Baron <jbaron@xxxxxxxxxx>
>
> +TEST=$1
> +shift
> +
> +case $TEST in
> + "goto")
> cat << "END" | $@ -x c - -fno-PIE -c -o /dev/null
> int main(void)
> {
> @@ -20,3 +25,29 @@ entry:
> return 0;
> }
> END
> + ;;
> +
> + "goto_output")
> +cat << "END" | $@ -x c - -c -o /dev/null
> +int foo(int x) {
> + asm goto ("": "=r"(x) ::: bar);
> + return x;
> + bar: return 0;
> +}
> +END
> + ;;
> +
> + "goto_tied_output")
> +cat << "END" | $@ -x c - -c -o /dev/null
> +int foo(int *x) {
> + asm goto (".long (%l[bar]) - .\n": "+m"(*x) ::: bar);
> + return *x;
> + bar: return 0;
> +}
> +END
> + ;;
> +
> + *)
> + exit -1
> + ;;
> +esac
> --
> 2.37.1
>







It is well known that 'echo' command is not portable
when used with escape sequences.
'printf' will do in such situations.


The POSIX spec [1] also mentions this:

"If the first operand is -n, or if any of the operands contain a
<backslash> character,
the results are implementation-defined."


"It is not possible to use echo portably across all POSIX systems
unless both -n (as the first argument) and escape sequences are omitted."


[1] : https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html






So, the issue for this case is '\n'.
Do we need it?


Even with '\n' dropped, I still can detect the bug of clang-13.



[For clang 13]


$ echo 'int foo(int *x) { asm goto (".long (%l[bar]) - .": "+m"(*x)
::: bar); return *x; bar: return 0; }' | clang-13 -x c - -c -o
/dev/null
<stdin>:1:29: error: invalid operand in inline asm: '.long (${1:l}) - .'
int foo(int *x) { asm goto (".long (%l[bar]) - .": "+m"(*x) ::: bar);
return *x; bar: return 0; }
^
<stdin>:1:29: error: unknown token in expression
<inline asm>:1:9: note: instantiated into assembly here
.long () - .
^
2 errors generated.




[For clang 14]

$ echo 'int foo(int *x) { asm goto (".long (%l[bar]) - .": "+m"(*x)
::: bar); return *x; bar: return 0; }' | clang-14 -x c - -c -o
/dev/null
$ echo $?
0





So, please drop '\n' and check if it is OK.

That will be simpler for back-porting.






--
Best Regards
Masahiro Yamada