https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85203
Bug ID: 85203 Summary: cmse_nonsecure_caller intrinsic returns incorrect results Product: gcc Version: 7.3.1 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: target Assignee: thopre01 at gcc dot gnu.org Reporter: thopre01 at gcc dot gnu.org Target Milestone: --- Target: arm-none-eabi Hi, The cmse_nonsecure_caller intrinsic for Armv8-M Baseline and Mainline architecture returns true in almost all cases, ie. compiling and running the following test with arm-none-eabi-gcc -Os -mcmse -march=armv8-m.main will return an error: #include <arm_cmse.h> int foo (void) { return cmse_nonsecure_caller (); } int main (void) { /* Return success (0) if main is secure, ie if cmse_nonsecure_caller/foo returns false (0). */ return foo (); } Looking at the implementation of the associated __builtin_cmse_nonsecure_caller in gcc/config/arm/arm-builtins.c we can see why: * it performs an add instead of an and to get the lsb of LR * it does not negate the value to return true when the lsb is 0 This means that except for 0xffffffff it will always return true. I'm currently testing a patch as I write these lines.