Is there a gcc macro to determine if the frame pointers are not resolved?

When the -fomit-frame-pointer is used (automatic for various -O settings), backtracking is problematic. I'm wondering if there is a way at compile time to determine that the code has been compiled using this switch? In this case, I could set #ifndef to protect against backtracking when not recommended.

Is any macro set when this -fomit-frame-pointer switch is on?

Thank,

SetJmp

+2


source to share


3 answers


I just tried this:

gcc -E -fomit-frame-pointer -Wp, -dM foo.c> fpo.out
gcc -E -Wp, -dM foo.c> no-fpo.out
diff no-fpo.out fpo.out


where foo.c is a simple Hello World program and got no results. This means that all preprocessor macros were identical, whether used -fomit-frame-pointer

or not. So I think the answer to your question is no.

Probably the best thing you can do is change your Makefile (or whatever your build process is) to define your own macro (for example, -DNO_FRAME_POINTERS

or whatever) when used -fomit-frame-pointer

.

+2


source


You cannot do this at compile time, but at run time you can check if your program has been optimized or not.

Write code that will definitely be modified by the optimizer, like mixing a non-volatile variable with setjmp

/ longjmp

, and by the value of that variable you will know if your program is optimized or not.

#include <setjmp.h>
#include <stdio.h>

int is_optimised(void) {
    int i = 1;
    jmp_buf jmp_loc;

    if (setjmp(jmp_loc)) {
        return i;  // optimiser changes it to "return 1"
    }

    i = 0;
    longjmp(jmp_loc, 1);

    return 0;
}

int main(int argc, char *argv[]) {
    printf("%soptimised\n", is_optimised() ? "" : "non-");

    return 0;
}

      



If compiled with GCC without -O

, it prints " non-optimised

" to switch -O1

- -O4

it prints " optimised

."

Of course, your mileage (with other compilers) may differ.

+1


source


In terms of validation, check at runtime that the ebp register (setting for your architecture) points a few bytes below the top of the stack and then makes sense if you follow the pointer stored in [ebp].

+1


source







All Articles