Squid Guy
Squid Guy

Reputation: 31

GCC no reorder stack variables

Hi i'm trying to compile a C code without reordering my variables in stack but can't do it.

I've tryed using:

__attribute__((no_reorder))

But doesn't work, also tryed to compile with the flag:

-fno-toplevel-reorder

But didn't work... so i'm stuck.

Actual code:

uint8_t __attribute__((no_reorder)) first_buf[64];
uint8_t second_buf[32];

This is my compiler version:

gcc (Debian 7.2.0-19) 7.2.0

Thank you for reading!

Upvotes: 3

Views: 3252

Answers (2)

Ping-Jui Liao
Ping-Jui Liao

Reputation: 31

-fno-stack-protector will do it. It will cancel stack canary and reordering of buffer on stack.

add that flag when you compile. i.e.

gcc myprogram.c -fno-stack-protector

Upvotes: 3

Andreas Grapentin
Andreas Grapentin

Reputation: 5796

from the gcc documentation here:

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

no_reorder

Do not reorder functions or variables marked no_reorder against each other or top level assembler statements the executable. The actual order in the program will depend on the linker command line. Static variables marked like this are also not removed. This has a similar effect as the -fno-toplevel-reorder option, but only applies to the marked symbols.

(emphasis mine)

So it would appear that you would need to apply the attribute to the variables the respective order of which you want preserved. Applying the attribute to only a single variable will only preserve the order of that variable with itsef, which has no effect.

Upvotes: 2

Related Questions