[Bug middle-end/98896] local label displaced with -O2

2021-01-30 Thread stsp at users dot sourceforge.net via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #9 from Stas Sergeev  ---
(In reply to Jakub Jelinek from comment #7)
> you need to tell the compiler
> the asm can goto to that label.

Of course the one would wonder what else
could be done to the passed label. :)
Maybe some distance was calculated by
subtracting 2 labels, or alike. Maybe
it wasn't jump.
But why does it help to assume that something
passed to volatile asm, remains unused?
Just wondering.
IMHO at least it deserves a warning.

[Bug middle-end/98896] local label displaced with -O2

2021-01-30 Thread stsp at users dot sourceforge.net via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #8 from Stas Sergeev  ---
(In reply to Jakub Jelinek from comment #7)
> It doesn't mean you can't use "r" (&),

Well, if not for Andrew telling exactly that
you can't, both here and in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=29305
then indeed, it doesn't.
Because this seems to work:
---
int main(void)
{
__label__ cont;
asm volatile goto (
"push %0\n"
"ret\n"
::"r"(&):"memory":cont);
cont:
return 0;
}
---

So... is this a correct, documented, supported etc
way of doing things, and it won't disappear in the
next gcc version?
Then perfectly fine.

Thanks for your help!

[Bug middle-end/98896] local label displaced with -O2

2021-01-30 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #7 from Jakub Jelinek  ---
It doesn't mean you can't use "r" (&), but you need to tell the compiler
the asm can goto to that label.
Or of course can just use the rip based addressing yourself in the inline asm,
the label's %X stands just for the label.

[Bug middle-end/98896] local label displaced with -O2

2021-01-30 Thread stsp at users dot sourceforge.net via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #6 from Stas Sergeev  ---
(In reply to Jakub Jelinek from comment #5)
> I think Andrew meant asm goto, which you haven't tried.

You are right.
Thanks for mentioning that.
But it doesn't work as well:
---
int main(void)
{
__label__ cont;
asm volatile goto (
"push %l[cont]\n"
"ret\n"
cont);
cont:
return 0;
}
---

$ LC_ALL=C cc -Wall -ggdb3 -O2 -o jmpret2 jmpret2.c -pie -fPIE
/usr/bin/ld: /tmp/cc1UoxnD.o: relocation R_X86_64_32S against `.text.startup'
can not be used when making a PIE object; recompile with -fPIE


And in an asm file we see:
---
#APP
# 4 "jmpret2.c" 1
push .L2#
ret
---


Please compare this to the following:
---
int main(void)
{
__label__ cont;
asm volatile (
"push %0\n"
"ret\n"
::"r"(&));
cont:
return 0;
}
---

And its asm:
---
.L2:
.loc 1 4 5 view .LVU1
leaq.L2(%rip), %rax #, tmp83
#APP
# 4 "jmpret3.c" 1
push %rax   # tmp83
ret
---


So... it seems, only the second case can work,
and indeed does with clang?

[Bug middle-end/98896] local label displaced with -O2

2021-01-30 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek  ---
I think Andrew meant asm goto, which you haven't tried.  That is the standard
feature that tells the compiler about the possible control flow from the inline
asm, just having & passed to inline asm (or even maybe propagated)
doesn't mean the inline asm may jump there, in such case there is no cfg edge
to the label and labels which are unreachable are just placed somewhere, don't
prevent optimizations etc.

[Bug middle-end/98896] local label displaced with -O2

2021-01-29 Thread stsp at users dot sourceforge.net via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #4 from Stas Sergeev  ---
I can achieve similar results with this:
---
void cont(void) asm("_cont");
asm volatile (
"push %0\n"
"ret\n"
"_cont:\n"
::"r"(cont));
---

But this doesn't work if the optimizer inlines
the function, as you then get multiple definitions
of "_cont".

[Bug middle-end/98896] local label displaced with -O2

2021-01-29 Thread stsp at users dot sourceforge.net via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #3 from Stas Sergeev  ---
I can't use inline-asm gotos because
I can't manipulate such a label in a portable way.
For example:
---
asm volatile (
"push $1f\n"
"ret\n"
"1:\n"
);
---

This won't work with -pie.
But if I do "r"(&) then the rip-relative
reference is generated so pie works.

> See PR 29305 and others too on why this is undefined.

>From that PR I can only see that its undefined
because the documentation didn't define such use,
at best. I am not sure it immediately means "undefined".
---
You may not use this mechanism to jump to code in a different function.
If you do that, totally unpredictable things will happen. The best way
to avoid this is to store the label address only in automatic variables
and never pass it as an argument. 
---
Not sure if I violated that.
I pass it as an argument to an inline asm only -
does this count as passing as an argument or what?
I suppose they meant "as an argument to a function".
Inline asm is not a function.

Anyway:
- What is the point to misplace the label that is obviously (for gcc) used?
- Why clang have no problem with that code?

[Bug middle-end/98896] local label displaced with -O2

2021-01-29 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #2 from Andrew Pinski  ---
See PR 29305 and others too on why this is undefined.

[Bug middle-end/98896] local label displaced with -O2

2021-01-29 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

Andrew Pinski  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID
  Component|c   |middle-end

--- Comment #1 from Andrew Pinski  ---
Use inline-asm goto's instead.