public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name
@ 2022-05-12  5:30 i at maskray dot me
  2022-05-12  6:00 ` [Bug target/105576] " pinskia at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: i at maskray dot me @ 2022-05-12  5:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105576
           Summary: x86: Support a machine constraint to get raw symbol
                    name
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i at maskray dot me
  Target Milestone: ---

An raw symbolic operand is useful in inline asm (e.g. in C++ to get the mangled
name, or in C to let the compiler do some asm checking). In aarch64 and riscv,
there is a documented (supported) approach.
documented on
https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints

extern int var;
void *addr(void) { return &var; }
void addr_via_asm(void) {
  asm (".pushsection .xxx,\"aw\"; .dc.a %0; .popsection" :: "S"(addr)); //
supported on aarch64 and riscv
  asm (".pushsection .xxx,\"aw\"; .dc.a %0; .popsection" :: "S"(&var)); //
supported on aarch64
}

On x86, it is unclear which should be preferred. It seems that the operand
modifier 'p' can be used for a function,
but not a variable.

asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "m"(addr));

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
@ 2022-05-12  6:00 ` pinskia at gcc dot gnu.org
  2022-05-12  6:00 ` pinskia at gcc dot gnu.org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-12  6:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Isn't the i constraint the one you want to be portable:

‘i’
An immediate integer operand (one with constant value) is allowed. This
includes symbolic constants whose values will be known only at assembly time or
later.

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
  2022-05-12  6:00 ` [Bug target/105576] " pinskia at gcc dot gnu.org
@ 2022-05-12  6:00 ` pinskia at gcc dot gnu.org
  2022-05-12  6:03 ` pinskia at gcc dot gnu.org
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-12  6:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Isn't the i constraint the one you want to be portable:
> 
> ‘i’
> An immediate integer operand (one with constant value) is allowed. This
> includes symbolic constants whose values will be known only at assembly time
> or later.

This is from
https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
  2022-05-12  6:00 ` [Bug target/105576] " pinskia at gcc dot gnu.org
  2022-05-12  6:00 ` pinskia at gcc dot gnu.org
@ 2022-05-12  6:03 ` pinskia at gcc dot gnu.org
  2022-05-12  6:36 ` crazylht at gmail dot com
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-12  6:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note integer here does allow for pointers as that is still an integer value
internally.

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (2 preceding siblings ...)
  2022-05-12  6:03 ` pinskia at gcc dot gnu.org
@ 2022-05-12  6:36 ` crazylht at gmail dot com
  2022-05-13 17:08 ` i at maskray dot me
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: crazylht at gmail dot com @ 2022-05-12  6:36 UTC (permalink / raw)
  To: gcc-bugs

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

Hongtao.liu <crazylht at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |crazylht at gmail dot com

--- Comment #4 from Hongtao.liu <crazylht at gmail dot com> ---
constraint "i" + "%p0"?

  asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(addr)); //
supported on aarch64 and riscv
  asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(&var)); //
supported on aarch64

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (3 preceding siblings ...)
  2022-05-12  6:36 ` crazylht at gmail dot com
@ 2022-05-13 17:08 ` i at maskray dot me
  2023-05-05  4:53 ` i at maskray dot me
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: i at maskray dot me @ 2022-05-13 17:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Fangrui Song <i at maskray dot me> ---
(In reply to Hongtao.liu from comment #4)
> constraint "i" + "%p0"?
> 
>   asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(addr)); //
> supported on aarch64 and riscv
>   asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(&var)); //
> supported on aarch64

constraint "i" + "%p0" does not work with -mcmodel=large:

a.c:11:3: warning: ‘asm’ operand 0 probably does not match constraints
   11 |   asm volatile(".quad %p0" :: "i"(foo));
      |   ^~~
a.c:11:3: error: impossible constraint in ‘asm’

On aarch64, `asm volatile(".quad %0" :: "S"(foo));` works with -mcmodel=large
-fno-pic (note: PIC large code model has not been implemented).

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (4 preceding siblings ...)
  2022-05-13 17:08 ` i at maskray dot me
@ 2023-05-05  4:53 ` i at maskray dot me
  2023-12-07  2:49 ` tanksherman27 at gmail dot com
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: i at maskray dot me @ 2023-05-05  4:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Fangrui Song <i at maskray dot me> ---
(In reply to Hongtao.liu from comment #4)
> constraint "i" + "%p0"?
> 
>   asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(addr)); //
> supported on aarch64 and riscv
>   asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "i"(&var)); //
> supported on aarch64

It looks like %p0 + "i" doesn't work with -fpie or -fpic...

void k();
void foo() {
  asm("call %p0" :: "i"(k));
}

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (5 preceding siblings ...)
  2023-05-05  4:53 ` i at maskray dot me
@ 2023-12-07  2:49 ` tanksherman27 at gmail dot com
  2024-01-11  4:00 ` i at maskray dot me
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: tanksherman27 at gmail dot com @ 2023-12-07  2:49 UTC (permalink / raw)
  To: gcc-bugs

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

Julian Waters <tanksherman27 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tanksherman27 at gmail dot com

--- Comment #7 from Julian Waters <tanksherman27 at gmail dot com> ---
Strangely, the following works if compiled with optimizations enabled, -O1 and
above, but not with -O0, when optimizations are disabled

[[gnu::extended(; [dispatcher] "i" (static_cast<void (*) () noexcept>([] ()
noexcept -> void {})); ; start)]]
asm (R"(
        .endif
        .rva %l[start], 1f, %c[dispatcher], 2f
        .seh_code
)");

With -O0 the error is

exceptions.cpp: In function 'void exceptions()':
exceptions.cpp:59:5: warning: 'asm' operand 0 probably does not match
constraints
   59 |     asm (R"(
      |     ^~~
exceptions.cpp:59:5: error: impossible constraint in 'asm'

With -O1 and above, %c[dispatcher] yields:

.rva .L2, 1f, _ZZL10exceptionsvENUlPVK19_EXCEPTION_POINTERSPVKvE0_4_FUNES1_S3_,
2f

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (6 preceding siblings ...)
  2023-12-07  2:49 ` tanksherman27 at gmail dot com
@ 2024-01-11  4:00 ` i at maskray dot me
  2024-01-30 23:05 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: i at maskray dot me @ 2024-01-11  4:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Fangrui Song <i at maskray dot me> ---
I've encountered another use case related to metadata sections (establish an
artificial reference for linker garbage collection purposes)

namespace ns { extern int var; }  // defined in another translation unit

void reference() {     // if this is retained, ensure var is pulled in
  asm (".reloc ., BFD_RELOC_NONE, %0" :: "S"(&ns::var));
}

Created
https://gcc.gnu.org/pipermail/gcc-patches/2024-January/642580.html
([PATCH] i386: Add "z" constraint for symbolic address/label reference
[PR105576])

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (7 preceding siblings ...)
  2024-01-11  4:00 ` i at maskray dot me
@ 2024-01-30 23:05 ` pinskia at gcc dot gnu.org
  2024-01-30 23:21 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-30 23:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hjl.tools at gmail dot com

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 113675 has been marked as a duplicate of this bug. ***

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (8 preceding siblings ...)
  2024-01-30 23:05 ` pinskia at gcc dot gnu.org
@ 2024-01-30 23:21 ` cvs-commit at gcc dot gnu.org
  2024-01-30 23:56 ` i at maskray dot me
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-30 23:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by H.J. Lu <hjl@gcc.gnu.org>:

https://gcc.gnu.org/g:d7250100381b817114447d91fff4748526d4fb21

commit r14-8637-gd7250100381b817114447d91fff4748526d4fb21
Author: Fangrui Song <maskray@google.com>
Date:   Thu Jan 11 10:24:25 2024 -0800

    i386: Add "Ws" constraint for symbolic address/label reference [PR105576]

    Printing the raw symbol is useful in inline asm (e.g. in C++ to get the
    mangled name).  Similar constraints are available in other targets (e.g.
    "S" for aarch64/riscv, "Cs" for m68k).

    There isn't a good way for x86 yet, e.g. "i" doesn't work for
    PIC/-mcmodel=large.  This patch adds "Ws".  Here are possible use cases:

    ```
    namespace ns { extern int var; }
    asm (".pushsection .xxx,\"aw\"; .dc.a %0; .popsection" :: "Ws"(&var));
    asm (".reloc ., BFD_RELOC_NONE, %0" :: "Ws"(&var));
    ```

    gcc/ChangeLog:

            PR target/105576
            * config/i386/constraints.md: Define constraint "Ws".
            * doc/md.texi: Document it.

    gcc/testsuite/ChangeLog:

            PR target/105576
            * gcc.target/i386/asm-raw-symbol.c: New testcase.

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (9 preceding siblings ...)
  2024-01-30 23:21 ` cvs-commit at gcc dot gnu.org
@ 2024-01-30 23:56 ` i at maskray dot me
  2024-01-31  0:15 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: i at maskray dot me @ 2024-01-30 23:56 UTC (permalink / raw)
  To: gcc-bugs

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

Fangrui Song <i at maskray dot me> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #11 from Fangrui Song <i at maskray dot me> ---
Thanks to HJ for landing the GCC patch (milestone: 15?) for me.

Note that I made a typo in the commit message. "Ws" should typically be used
with the modifier 'p'

    ```
    namespace ns { extern int var; }
    asm (".pushsection .xxx,\"aw\"; .dc.a %p0; .popsection" :: "Ws"(&var));
    asm (".reloc ., BFD_RELOC_NONE, %p0" :: "Ws"(&var));
    ```

The upcoming Clang 18 release will also support "Ws".

For software utilizing this feature, for aarch64 and riscv, use the constraint
"S".

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (10 preceding siblings ...)
  2024-01-30 23:56 ` i at maskray dot me
@ 2024-01-31  0:15 ` pinskia at gcc dot gnu.org
  2024-05-21 16:48 ` pinskia at gcc dot gnu.org
  2024-05-28  5:29 ` i at maskray dot me
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-31  0:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0

--- Comment #12 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Fangrui Song from comment #11)
> Thanks to HJ for landing the GCC patch (milestone: 15?) for me.

It made into GCC 14.

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (11 preceding siblings ...)
  2024-01-31  0:15 ` pinskia at gcc dot gnu.org
@ 2024-05-21 16:48 ` pinskia at gcc dot gnu.org
  2024-05-28  5:29 ` i at maskray dot me
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-21 16:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |paul_robinson at playstation dot s
                   |                            |ony.com

--- Comment #13 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 115179 has been marked as a duplicate of this bug. ***

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

* [Bug target/105576] x86: Support a machine constraint to get raw symbol name
  2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
                   ` (12 preceding siblings ...)
  2024-05-21 16:48 ` pinskia at gcc dot gnu.org
@ 2024-05-28  5:29 ` i at maskray dot me
  13 siblings, 0 replies; 15+ messages in thread
From: i at maskray dot me @ 2024-05-28  5:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from Fangrui Song <i at maskray dot me> ---
> Is there a way to capture a method address in inline asm that works in
> -fPIC mode? Specifically I want to capture the address of a static
> method that's in a class that's local to a function. I'm able to do it
> in non-PIC mode but not PIC mode.

I have a write up how to support raw symbol names for different architectures
https://maskray.me/blog/2024-01-30-raw-symbol-names-in-inline-assembly

Summary:

AArch32: "US" (for symbol reference without an offset)
AArch64: "S"
x86: "Ws", GCC 14+, Clang 18+
MIPS/PowerPC/RISC-V: "s"

I hope that newer ports all define LEGITIMATE_PIC_OPERAND_P so that "s" can be
used.

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

end of thread, other threads:[~2024-05-28  5:29 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-12  5:30 [Bug c/105576] New: x86: Support a machine constraint to get raw symbol name i at maskray dot me
2022-05-12  6:00 ` [Bug target/105576] " pinskia at gcc dot gnu.org
2022-05-12  6:00 ` pinskia at gcc dot gnu.org
2022-05-12  6:03 ` pinskia at gcc dot gnu.org
2022-05-12  6:36 ` crazylht at gmail dot com
2022-05-13 17:08 ` i at maskray dot me
2023-05-05  4:53 ` i at maskray dot me
2023-12-07  2:49 ` tanksherman27 at gmail dot com
2024-01-11  4:00 ` i at maskray dot me
2024-01-30 23:05 ` pinskia at gcc dot gnu.org
2024-01-30 23:21 ` cvs-commit at gcc dot gnu.org
2024-01-30 23:56 ` i at maskray dot me
2024-01-31  0:15 ` pinskia at gcc dot gnu.org
2024-05-21 16:48 ` pinskia at gcc dot gnu.org
2024-05-28  5:29 ` i at maskray dot me

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).