0

Hi so a friend told me that:

calling conventions are c++ function specifiers that determine if the function that calls another function should clean the stack frame or that would be the job of the called function

is that true?

Also how would the cleaning part look in assembly with a syscall function calling another one?

Thanks in advance.

Alex
  • 840
  • 7
  • 23
  • 1
    Yes, that's basically true. Each function has a calling convention, explicit or implied, which guards whether it's the caller's or the callee's responsibility to restore the stack, if (some) arguments should be passed in registers, and so on. – 500 - Internal Server Error Jun 11 '20 at 11:34
  • 2
    Is this what you are looking for: [What are the different calling conventions in C/C++ and what do each mean?](https://stackoverflow.com/questions/949862/what-are-the-different-calling-conventions-in-c-c-and-what-do-each-mean)? Didn't flag as duplicate, because it doesn't have an answer to "_how would the cleaning part look in assembly_". – Algirdas Preidžius Jun 11 '20 at 11:34
  • 1
    Calling conventions aren't function specifiers. Calling conventions are contracts between the caller and callee of a callable object. Function specifiers may contain information for a compiler to implement this contract. – IInspectable Jun 11 '20 at 11:47
  • fyi - https://stackoverflow.com/questions/52903807/what-is-the-c-abi-specification-referred-to-in-gccs-manual – Richard Critten Jun 11 '20 at 11:57

1 Answers1

2

Historically, the Fortran and Pascal languages used a convention where the caller pushed its arguments on the stack (first parameter first in stack), and then called the callee function. The prologue code in the callee usually popped the return address and the parameters, and pushed again the return address ( this part is in fact an implementation detail). When the callee returns, the stack is clean.

The C language then came with the ability to call a function with a variable number of arguments. The convention was that the caller pushed the parameters in inverse order (last parameter first in stack) and then called the callee function. The callee then accessed the parameters in the stack without popping anything (the address of the first parameter is just near the return address). When the callee returns, the stack still contained the parameters and the caller should clean them.

This is still used in the Windows system, where most of the API function use the pascal convention (winapi) while by default C or C++ methods use by default the C (cdecl) convention.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    Note that for 64-bit processes on Windows all the calling conventions are now the same: https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019 – Alan Birtles Jun 11 '20 at 11:54