V
V
Victor Bomberow2020-03-10 06:50:45
C++ / C#
Victor Bomberow, 2020-03-10 06:50:45

How to read function arguments on the stack / force gcc to store function arguments on the stack?

Tried __attribute__(__cdecl__) and CMAKE_C_FLAGS " -O0 " didn't help.

void foo (char arg1, int arg2, ... );

At the call point foo('a', 5, 'b', 39, 'c', 46, 'd', 55)
definition

void __attribute__((__cdecl__)) SumIntDouble(char type_code, int arg, ...) {

      printf("VIEW: %c\n", *(&type_code + 0));
      printf("VIEW: %c\n", *(&type_code + 1));
      printf("VIEW: %c\n", *(&type_code + 2));
      printf("VIEW: %c\n", *(&type_code + 3));
      printf("VIEW: %c\n", *(&type_code + 4));
      printf("VIEW: %c\n", *(&type_code + 5));
      printf("VIEW: %c\n", *(&type_code + 6));


I added and subtracted 1 to the address of the first argument in a loop, I did not see that b and c, etc., lay as expected if all the arguments were exactly next to each other on the stack.

I looked at the compiler flags, tried some, but it did not lead to anything.

Please help me understand

cmakelists:

cmake_minimum_required(VERSION 3.5)
project(playgraound_c_lang LANGUAGES C)
set( CMAKE_C_FLAGS " --no-stack-protector -O0 " )
add_executable(playgraound_c_lang main.c)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2020-03-10
@majstar_Zubr

https://godbolt.org/z/ARYhis
Looking into the function prologue, it makes a local copy of our a. And the desired stack location is 36 bytes higher than what you're looking for.
Of course, everything is under x86. The call under x64 implies registers.
UPD. But Intel does not make local copies and everything is in place.

M
martianin, 2020-03-24
@martianin

There are tools in the standard library for this. See here: https://en.cppreference.com/w/cpp/utility/variadic...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question