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

            Bug ID: 109707
           Summary: Skip __attribute__((naked)) functions for -pg and
                    -finstrument-functions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i at maskray dot me
  Target Milestone: ---

The asm in a naked function may reasonably expect the argument registers and
the
return address register (if present) to be live.

When using -pg and -finstrument-functions, functions are instrumented by adding
a function call to mcount/__cyg_profile_func_enter, which will clobber these
registers.
If the return address register is clobbered, the function will be unable to
return to the caller, possibly causing an infinite loop.

% riscv64-linux-gnu-gcc a.c -finstrument-functions
-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1
-Wl,-rpath=/usr/riscv64-linux-gnu/lib
% ./a.out   # infinite loop
% riscv64-linux-gnu-gcc a.c -pg
-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1
-Wl,-rpath=/usr/riscv64-linux-gnu/lib
% ./a.out   # infinite loop

% arm-linux-gnueabihf-gcc a.c -finstrument-functions
-Wl,--dynamic-linker=/usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
-Wl,-rpath=/usr/arm-linux-gnueabihf/lib
% ./a.out   # infinite loop


arm -pg uses `push {lr}; bl __gnu_mcount_nc`, so a naked function works with
-pg, but this appears to be a rare exception.

% arm-linux-gnueabihf-gcc a.c -pg
-Wl,--dynamic-linker=/usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
-Wl,-rpath=/usr/arm-linux-gnueabihf/lib
% ./a.out   # good

Reply via email to