flat assembler
Message board for the users of flat assembler.
Index
> Linux > Linux ABI stack alignment Goto page 1, 2, 3 Next |
Author |
|
revolution 12 Apr 2017, 12:48
I don't think this is something that should be measured, because what you see could easily be an artefact of coincidence.
I hate to point out the obvious but either the source code or the documentation, or both, should tell you what the alignment is expected to be. |
|||
12 Apr 2017, 12:48 |
|
fasmnewbie 12 Apr 2017, 12:54
revolution wrote: I don't think this is something that should be measured, because what you see could easily be an artefact of coincidence. There is significant differences for both formats when dealing with alignment for the stack. What I am actually interested in knowing is the kernel's stack allocation policy. I don't think there're documented anywhere (or perhaps I missed the documentation entirely). |
|||
12 Apr 2017, 12:54 |
|
fasmnewbie 12 Apr 2017, 13:16
Code: format ELF64 executable 3 entry $ ;The stack is consistently aligned to 16 by default here ;Now you can safely access any syscalls with guaranteed alignment ;no external alignment needed. Any attempt to align will break the whole alignment ecosystem. ;exit Code: format ELF64 public something something: ;The stack is NOT GUARANTEED to be aligned to 16 by default here ;Now we're not sure anymore. Should we aligned the stack externally b4 using syscalls / C library? ;exit At least on my machine, this behaviour is consistent. But I don't know how they'd behave on other distros / PCs. This is where I need the confirmation, because this behavior is not documented anywhere I hope this pseudo makes my question clearer. Thanks |
|||
12 Apr 2017, 13:16 |
|
revolution 12 Apr 2017, 13:17
Linux doesn't require 16-byte alignment. Perhaps you are confusing things with Windows fastcall convention.
|
|||
12 Apr 2017, 13:17 |
|
fasmnewbie 12 Apr 2017, 13:22
revolution wrote: Linux doesn't require 16-byte alignment. Perhaps you are confusing things with Windows fastcall convention. It doesn't? Are you sure my friend? |
|||
12 Apr 2017, 13:22 |
|
revolution 12 Apr 2017, 13:25
|
|||
12 Apr 2017, 13:25 |
|
fasmnewbie 12 Apr 2017, 13:27
How about this, then? It's under "Stack Frame" sub-heading.
http://chamilo2.grenet.fr/inp/courses/ENSIMAG3MM1LDB/document/doc_abi_ia64.pdf |
|||
12 Apr 2017, 13:27 |
|
revolution 12 Apr 2017, 13:44
Okay, maybe you are right. There often seems to be conflicting information about Linux.
In that case I would hesitate to always rely upon the test results to "confirm" that the stack is, or isn't, aligned correctly at the application entry point. Instead, just add the appropriate and rsp,-16 once as the first instruction and then you don't have to care about it again. |
|||
12 Apr 2017, 13:44 |
|
fasmnewbie 12 Apr 2017, 13:50
revolution wrote: Okay, maybe you are right. There often seems to be conflicting information about Linux. You're not entirely wrong on this either. I've experimented with some syscalls and they do work on unaligned stack, giving us such wrong impressions that Linux ABI don't need strict stack alignments. |
|||
12 Apr 2017, 13:50 |
|
Furs 13 Apr 2017, 11:17
revolution is half-right. The Linux Kernel does not require stack alignment. i.e. syscalls don't.
However, shared libraries do, since they follow the stupid AMD ABI. If you only use the Linux Kernel (which is technically "Linux", libraries aren't), then you don't need alignment of stack. If you call into GNU libraries or whatever you'll need alignment though. |
|||
13 Apr 2017, 11:17 |
|
fasmnewbie 13 Apr 2017, 14:41
Furs wrote: revolution is half-right. The Linux Kernel does not require stack alignment. i.e. syscalls don't. I think the issue is not yet fully resolved. Follow the discussions here: https://patchwork.kernel.org/patch/9507697/ I can't follow the discussions very well because those guys (including Linus) talk in C. Bleeds my eyes. But my take on this is to just follow the ABI alignment requirement to avoid future problems. I have this hunch that the alignment requirement is actually meant for syscalls that make use of SSE instructions. I don't see any other practical reasons for enforcing aligned stack other than SSE. The alignment is not actually for the function's code, but for use by the SSE save and restore inside the red zone of one particular syscall. 128 bytes is enough for XMM0 and XMM7. They are the most likely candidate for such alignment or redzone population. So the fact that many 64-bit code can safely disregard the alignment requirements is because, for backward compatibility, the syscall have rarely used SSE instructions / registers so far or at least they don't bother saving the SSE states. But there's no guarantee in the future. If UNIX or Linux don't come clean on this as soon as possible, we could be witnessing massive restructuring of userland codes that have complete disregard for such alignment requirement, including my own, once the movdqa kicks in from any of the syscall. |
|||
13 Apr 2017, 14:41 |
|
Furs 13 Apr 2017, 15:17
But Linux kernel is fully backwards compatible and they never introduce breaking changes. I don't understand your worry though? If the specification says it doesn't require 16-byte alignment, then it will always work without 16-byte alignment, unless they break the spec (which they won't, like I said, Linus values backwards compatibility; his words "we never break userland")
As a matter of fact they had a bug suggestion on GCC to allow 8-byte aligned stack for the kernel, to not waste space: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383 Yes, the kernel barely uses SSE, and when it does it aligns the stack properly by itself. If in the future it will use more SSE, it will align the stack itself, so you don't have to worry about it when using syscall. Then again, who's to say it won't use AVX or AVX512 in the future instead of SSE? (to be honest, they probably already do). The kernel is known to use the latest instruction sets, anyway, quite early might I add. Are you going to align the stack to 64-bytes? What if they add 128-byte vectors? See where I'm going? I don't think you have to worry when doing syscalls, unless I misunderstand your situation? If the kernel will use vectors, it will align the stack itself, like a sane ABI. |
|||
13 Apr 2017, 15:17 |
|
fasmnewbie 13 Apr 2017, 16:02
Furs wrote: But Linux kernel is fully backwards compatible and they never introduce breaking changes. I don't understand your worry though? If the specification says it doesn't require 16-byte alignment, then it will always work without 16-byte alignment, unless they break the spec (which they won't, like I said, Linus values backwards compatibility; his words "we never break userland") I am more concern with Linux kernel inconsistency regarding the stack allocation, hence my first post. That first stack allocation will determine my stack alignment policy for the next libraries / routines. IF, by default Linux allocates the stack aligned to 16, then the rest would just follow suit to maintain such alignment ecosystem. IF, Linux allocates it using random alignment (8 or 16), then I have to determine the correct adjustment required for the next routines. That's the central issue here, and I need help confirming both cases to determine its consistency. So far, both format apply different alignments, at least on my distro. Thanks. |
|||
13 Apr 2017, 16:02 |
|
revolution 13 Apr 2017, 16:11
If you need some particular alignment then do it yourself. and rsp,-16: done. Just assume the alignment from the kernel is random, and make it what you need. It is only one single instruction, no big deal.
|
|||
13 Apr 2017, 16:11 |
|
fasmnewbie 13 Apr 2017, 16:21
revolution wrote: If you need some particular alignment then do it yourself. and rsp,-16: done. Just assume the alignment from the kernel is random, and make it what you need. It is only one single instruction, no big deal. and rsp,-16 = no ABI required. That literally means you can disregard any calling convention out there. Is that what you're suggesting? My BASELIB is full of that https://board.flatassembler.net/topic.php?p=184548 |
|||
13 Apr 2017, 16:21 |
|
revolution 13 Apr 2017, 16:24
Calling conventions are more than just the stack alignment. And in fact you would be complying with the convention (not disregarding it) if you align accordingly.
|
|||
13 Apr 2017, 16:24 |
|
Furs 13 Apr 2017, 16:27
Does the kernel even have callbacks?
If not, then you only need one and rsp, -16 at the beginning of your entire program, just to be safe, what's the big deal? Anyway, I think it does align the stack to 16-byte before passing it to userland though. But then again, one instruction per program isn't going to kill it EDIT: I looked at the sources of libc (which is what usually runs before 'main' in C programs), and it seems it DOES align the stack (sorry I didn't read correctly before). First it gets information from above the stack on entry to program (which is where Linux stores argc/argv/env vars), and then aligns the stack with and rsp, -16, so you should do it just in case. The file is in sysdeps/x86_64/start.S of libc. (AT&T asm though, so beware) |
|||
13 Apr 2017, 16:27 |
|
fasmnewbie 13 Apr 2017, 16:38
revolution wrote: Calling conventions are more than just the stack alignment. And in fact you would be complying with the convention (not disregarding it) if you align accordingly. With and rsp,-16 you can completely disregard any conventions. Trust me, I did that to Win64 and Linux64 using the same identical source via BASELIB. I further extended the support to binaries (SO, DLL, LIB, OBJ, O etc) and not one ever emits any segfault. I managed to dodge the stack requirements for both ABIs via "and rsp,-16" alone. "and rsp,-16" literally means no64- ABI required. It's the old 32-bit standard call calling convention |
|||
13 Apr 2017, 16:38 |
|
revolution 13 Apr 2017, 16:42
If you need to call a system API and the system requires a particular stack alignment then you still have to comply to the convention. So using and rsp,-16 helps you to comply. If you are talking about your own internal code then you can do whatever you want, align to 1 if it pleases you, but don't try to call a system API like that, you'll need to place an and rsp,-16 to fix everything up and comply.
|
|||
13 Apr 2017, 16:42 |
|
Goto page 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.