Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC no reorder stack variables

Tags:

c

stack

gcc

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!

like image 540
Squid Guy Avatar asked Sep 24 '26 06:09

Squid Guy


2 Answers

-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
like image 60
Ping-Jui Liao Avatar answered Sep 27 '26 03:09

Ping-Jui Liao


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.

like image 22
Andreas Grapentin Avatar answered Sep 27 '26 03:09

Andreas Grapentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!