[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #14 from Fangrui Song --- > 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.
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Andrew Pinski changed: What|Removed |Added CC||paul_robinson at playstation dot s ||ony.com --- Comment #13 from Andrew Pinski --- *** Bug 115179 has been marked as a duplicate of this bug. ***
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Andrew Pinski changed: What|Removed |Added Target Milestone|--- |14.0 --- Comment #12 from Andrew Pinski --- (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.
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Fangrui Song changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED Resolution|--- |FIXED --- Comment #11 from Fangrui Song --- 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"()); asm (".reloc ., BFD_RELOC_NONE, %p0" :: "Ws"()); ``` The upcoming Clang 18 release will also support "Ws". For software utilizing this feature, for aarch64 and riscv, use the constraint "S".
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #10 from GCC Commits --- The master branch has been updated by H.J. Lu : https://gcc.gnu.org/g:d7250100381b817114447d91fff4748526d4fb21 commit r14-8637-gd7250100381b817114447d91fff4748526d4fb21 Author: Fangrui Song 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"()); asm (".reloc ., BFD_RELOC_NONE, %0" :: "Ws"()); ``` 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.
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Andrew Pinski changed: What|Removed |Added CC||hjl.tools at gmail dot com --- Comment #9 from Andrew Pinski --- *** Bug 113675 has been marked as a duplicate of this bug. ***
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #8 from Fangrui Song --- 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"(::var)); } Created https://gcc.gnu.org/pipermail/gcc-patches/2024-January/642580.html ([PATCH] i386: Add "z" constraint for symbolic address/label reference [PR105576])
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Julian Waters changed: What|Removed |Added CC||tanksherman27 at gmail dot com --- Comment #7 from Julian Waters --- 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([] () 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
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #6 from Fangrui Song --- (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"()); // > supported on aarch64 It looks like %p0 + "i" doesn't work with -fpie or -fpic... void k(); void foo() { asm("call %p0" :: "i"(k)); }
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #5 from Fangrui Song --- (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"()); // > 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).
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 Hongtao.liu changed: What|Removed |Added CC||crazylht at gmail dot com --- Comment #4 from Hongtao.liu --- 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"()); // supported on aarch64
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #3 from Andrew Pinski --- Note integer here does allow for pointers as that is still an integer value internally.
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #2 from Andrew Pinski --- (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
[Bug target/105576] x86: Support a machine constraint to get raw symbol name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105576 --- Comment #1 from Andrew Pinski --- 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.