https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114641
Bug ID: 114641 Summary: sh: fdpic optimization of function address breaks pointer equality Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: bugdal at aerifal dot cx Target Milestone: --- Created attachment 57904 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57904&action=edit rough fix For FDPIC targets where the canonical value of function address for equality purposes is determined by the address of the function descriptor, the function symbol being locally defined is not a sufficient condition for using a GOT-relative descriptor address. The address cannot be determined at link-time, only at ldso-time, and thus must be loaded through the GOT. sh.c's legitimize_pic_address wrongly optimizes references with SYMBOL_REF_LOCAL_P to @GOTOFFFUNCDESC form unless they are weak (for undef-weak) reasons, but also needs to refrain from doing this optimization if the symbol is external and not hidden. The test case I was working with is: #include <dlfcn.h> #include <stdio.h> int main() { printf("%p %p\n", (void *)main, dlsym(RTLD_DEFAULT, "main")); } but you can see the problem without executing anything, just looking at the emitted assembly. The attached patch fixes it but is probably not idiomatic. Note that there is a related binutils bug that prevents the fix from having an effect on the test program when linked: https://sourceware.org/bugzilla/show_bug.cgi?id=31619 With both applied, the linked output is correct too.