Hi there,
For a bit of fun, I'm trying to implement some low-level functionality in FreeBSD and Linux. I had a question regarding some seemingly not-deterministic behavior I came across regarding the initial stack alignment in FreeBSD and, therefore, the location of argc and argv relative to %rsp. I don't have a systems programming background, so maybe this issue has an obvious explanation. I looked around the forum, but I couldn't find anything on this topic.
It doesn't seem to be a recent bug, as it's essentially the same problem indicated in this 10-year old post on stackoverflow: https://stackoverflow.com/questions/8177734/freebsd-amd64-assembly-how-to-read-argc
In short, upon the executable starting, %rsp does not always point to argc. Instead, it appears that argc is located at [rsp+0] roughly half the time and [rsp+8] the other half. In addition, the stack pointer does not seem to be aligned to 16-bytes when the program starts, which I was under the impression was specified by the System-V ABI.
Chapter 11 of the FreeBSD developers handbook, though seemingly very out-of-date, does suggest that argc should always be at [rsp+0].
The answer on stackoverflow seems to be correct in that %rdi correctly contains the location of argc on FreeBSD. In other words, if you subtract %rsp from %rdi, you will get either 0 or 8, randomly. I was wondering if you guys had any further clarification for this seemingly non-deterministic behavior. I'm guessing it has something to do with the transition from a 32bit to 64bit OS. Or maybe it's something wrong with my particular machine/install.
Either way, thanks for taking the time to read my question.
FYI: This issue arises for executables assembled and linked with as/nasm & ld (as shown in the post) and even when I directly create minimal binaries without a linker. I'd be happy to attach any screenshots or terminal outputs.
For a bit of fun, I'm trying to implement some low-level functionality in FreeBSD and Linux. I had a question regarding some seemingly not-deterministic behavior I came across regarding the initial stack alignment in FreeBSD and, therefore, the location of argc and argv relative to %rsp. I don't have a systems programming background, so maybe this issue has an obvious explanation. I looked around the forum, but I couldn't find anything on this topic.
It doesn't seem to be a recent bug, as it's essentially the same problem indicated in this 10-year old post on stackoverflow: https://stackoverflow.com/questions/8177734/freebsd-amd64-assembly-how-to-read-argc
In short, upon the executable starting, %rsp does not always point to argc. Instead, it appears that argc is located at [rsp+0] roughly half the time and [rsp+8] the other half. In addition, the stack pointer does not seem to be aligned to 16-bytes when the program starts, which I was under the impression was specified by the System-V ABI.
Chapter 11 of the FreeBSD developers handbook, though seemingly very out-of-date, does suggest that argc should always be at [rsp+0].
The answer on stackoverflow seems to be correct in that %rdi correctly contains the location of argc on FreeBSD. In other words, if you subtract %rsp from %rdi, you will get either 0 or 8, randomly. I was wondering if you guys had any further clarification for this seemingly non-deterministic behavior. I'm guessing it has something to do with the transition from a 32bit to 64bit OS. Or maybe it's something wrong with my particular machine/install.
Either way, thanks for taking the time to read my question.
FYI: This issue arises for executables assembled and linked with as/nasm & ld (as shown in the post) and even when I directly create minimal binaries without a linker. I'd be happy to attach any screenshots or terminal outputs.