public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/98896] New: local label displaced with -O2
@ 2021-01-29 22:08 stsp at users dot sourceforge.net
  2021-01-29 22:33 ` [Bug middle-end/98896] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-29 22:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

            Bug ID: 98896
           Summary: local label displaced with -O2
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stsp at users dot sourceforge.net
  Target Milestone: ---

The following code works as expected with
clang, but hangs with gcc with -O2 (works with -O1):

---
int main()
{
    __label__ ret;
    asm volatile ("jmp *%0\n" :: "r"(&&ret) : "memory");
ret:
    return 0;
}
---

As can be seen from a disasm, the label is put before asm block.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
@ 2021-01-29 22:33 ` pinskia at gcc dot gnu.org
  2021-01-29 22:35 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-01-29 22:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID
          Component|c                           |middle-end

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Use inline-asm goto's instead.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
  2021-01-29 22:33 ` [Bug middle-end/98896] " pinskia at gcc dot gnu.org
@ 2021-01-29 22:35 ` pinskia at gcc dot gnu.org
  2021-01-29 23:08 ` stsp at users dot sourceforge.net
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-01-29 22:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
See PR 29305 and others too on why this is undefined.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
  2021-01-29 22:33 ` [Bug middle-end/98896] " pinskia at gcc dot gnu.org
  2021-01-29 22:35 ` pinskia at gcc dot gnu.org
@ 2021-01-29 23:08 ` stsp at users dot sourceforge.net
  2021-01-29 23:32 ` stsp at users dot sourceforge.net
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-29 23:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #3 from Stas Sergeev <stsp at users dot sourceforge.net> ---
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"(&&ret) 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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (2 preceding siblings ...)
  2021-01-29 23:08 ` stsp at users dot sourceforge.net
@ 2021-01-29 23:32 ` stsp at users dot sourceforge.net
  2021-01-30 10:02 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-29 23:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #4 from Stas Sergeev <stsp at users dot sourceforge.net> ---
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (3 preceding siblings ...)
  2021-01-29 23:32 ` stsp at users dot sourceforge.net
@ 2021-01-30 10:02 ` jakub at gcc dot gnu.org
  2021-01-30 11:25 ` stsp at users dot sourceforge.net
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-30 10:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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 &&label 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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (4 preceding siblings ...)
  2021-01-30 10:02 ` jakub at gcc dot gnu.org
@ 2021-01-30 11:25 ` stsp at users dot sourceforge.net
  2021-01-30 11:54 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-30 11:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #6 from Stas Sergeev <stsp at users dot sourceforge.net> ---
(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));
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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (5 preceding siblings ...)
  2021-01-30 11:25 ` stsp at users dot sourceforge.net
@ 2021-01-30 11:54 ` jakub at gcc dot gnu.org
  2021-01-30 12:11 ` stsp at users dot sourceforge.net
  2021-01-30 15:03 ` stsp at users dot sourceforge.net
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-30 11:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
It doesn't mean you can't use "r" (&&lab), 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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (6 preceding siblings ...)
  2021-01-30 11:54 ` jakub at gcc dot gnu.org
@ 2021-01-30 12:11 ` stsp at users dot sourceforge.net
  2021-01-30 15:03 ` stsp at users dot sourceforge.net
  8 siblings, 0 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-30 12:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #8 from Stas Sergeev <stsp at users dot sourceforge.net> ---
(In reply to Jakub Jelinek from comment #7)
> It doesn't mean you can't use "r" (&&lab),

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"(&&cont):"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!

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/98896] local label displaced with -O2
  2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
                   ` (7 preceding siblings ...)
  2021-01-30 12:11 ` stsp at users dot sourceforge.net
@ 2021-01-30 15:03 ` stsp at users dot sourceforge.net
  8 siblings, 0 replies; 10+ messages in thread
From: stsp at users dot sourceforge.net @ 2021-01-30 15:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98896

--- Comment #9 from Stas Sergeev <stsp at users dot sourceforge.net> ---
(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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-01-30 15:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 22:08 [Bug c/98896] New: local label displaced with -O2 stsp at users dot sourceforge.net
2021-01-29 22:33 ` [Bug middle-end/98896] " pinskia at gcc dot gnu.org
2021-01-29 22:35 ` pinskia at gcc dot gnu.org
2021-01-29 23:08 ` stsp at users dot sourceforge.net
2021-01-29 23:32 ` stsp at users dot sourceforge.net
2021-01-30 10:02 ` jakub at gcc dot gnu.org
2021-01-30 11:25 ` stsp at users dot sourceforge.net
2021-01-30 11:54 ` jakub at gcc dot gnu.org
2021-01-30 12:11 ` stsp at users dot sourceforge.net
2021-01-30 15:03 ` stsp at users dot sourceforge.net

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).