https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115179
Bug ID: 115179 Summary: Capture method address with inline asm in PIC mode? Product: gcc Version: 11.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: paul_robinson at playstation dot sony.com Target Milestone: --- 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. ``` __asm__( ".pushsection .init_array" "\n" ".quad %c0" "\n" ".popsection" "\n" : : "p"(Helper::myfunc)); ``` I've run through all the combinations of %0, %a0, %c0 as the operand, and constraints i, m, o, p. %c0 with either i or p works in non-PIC mode, but nothing works in PIC mode. %0 with "p" compiles without error but prefixes the mangled name with $ which then can't be resolved at link time. Other combinations error out with "impossible constraint" or "invalid constraint", or suffix the mangled name with (%rip) which isn't much use in a .quad directive. I can't use __attribute__((constructor)) because it's not allowed on methods of local classes. (FTR, the documentation doesn't say that.) I can't allocate the pointer directly in a custom section because that doesn't always work (see bug 41091 and friends). (If that worked, I wouldn't need the .init_array hack.) Generating asm and editing it to remove the $ or (%rip) isn't practical. I'm hoping there's some combination of asm operands/constraints that isn't obvious from the docs that will work here.