0

I have a global function func() implemented in assembly, declared as an external in a C++ source file:

extern "C" void func(char*);

func() requires the stack to be aligned at 32 bytes. Now, I could:

  1. Force all my calls to be aligned at 32 bytes passing the appropriate flag to the compiler
  2. Align the stack in the assembly source of func()
  3. Somehow force the calls to func() to be aligned at 32 bytes instead of a default of 16.

I don't want to do 1. So my question is, how to do 3? Perhaps the most efficient way is to just enforce alignment, as in 2.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Reimundo Heluani
  • 918
  • 9
  • 18
  • 1
    If you expect ABI-conforming code to call your function, you should probably build your function in a way that is ABI-conforming i.e. expecting 16-byte alignment and adjusting if necessary to get to 32-byte alignment. – Thomas Jager Jul 30 '21 at 15:22
  • You have to do 2. – user253751 Jul 30 '21 at 15:40
  • @ThomasJager yes, that is 2. above, but still would like to know the answer to my question as to how to do 3? – Reimundo Heluani Jul 30 '21 at 15:47
  • @ReimundoHeluani 3 can't be done on a per-call basis at the call site, only *inside* the function itself, as in 2. My question to you is - WHY does `func()` require the stack to be aligned at 32 bytes? Why can't it just use whatever alignment the compiler wants to use? – Remy Lebeau Jul 30 '21 at 16:03
  • 2
    You can not do (3) in C++. Your only option is (2) with convenience band-aid: you can create a call_func() in ASM, which aligns stack to 32 bytes and than calls func. – SergeyA Jul 30 '21 at 16:11
  • Thanks, this is an unexpected answer to my question, but is a full answer then if it can't be done from the C++ code. @RemyLebeau it's a vectorized algorithm, and having the stack aligned at 32 bytes makes life easier (since most of the vector operations require this alignment on memory operands). I don't mind doing 2, but since the code is based on an external library, maintaining the fork becomes a little more complicated. – Reimundo Heluani Jul 30 '21 at 16:29
  • 1
    So @SergeyA's approach seems the simplest from a maintaining perspective instead of changing the code directly in func. – Reimundo Heluani Jul 30 '21 at 16:31
  • There obviously isn't portable ISO C++ syntax for calling convention details like that (which might not even exist on some hypothetical implementations that implement function args totally differently from the asm stack in a modern mainstream systems.) But there are ways with specific compilers or specific calling conventions to make this happen; I think there might be a GCC `__attribute__` to control this. There is for telling the compiler about incoming alignment maybe being less than 16. So do you want a compiler-specific answer? If so, which compiler, and which calling convention? – Peter Cordes Aug 01 '21 at 03:43
  • But yeah, it only takes a couple instructions to align the stack inside a function, especially if you don't need VLAs in that function, and/or if you were already making a frame pointer anyway. – Peter Cordes Aug 01 '21 at 03:47

0 Answers0