oridb / mc

Myrddin Compiler
MIT License
387 stars 34 forks source link

Properly align the stack pointer in _start #160

Closed snocl closed 6 years ago

snocl commented 6 years ago

This adds an instruction to the start-<os>.s files, aligning the stack pointer to the proper 16-byte boundary right after entering _start.

While a violation of the ABI, it doesn’t seems like this is causing problems in pure Myrddin yet. It has, however, caused me some trouble under macOS, where the dynamic linker (dyld) may abort the program when trying to call a dynamically linked function with a misaligned stack pointer.

It seems like Linux libc implementations also make sure to do this (see e.g. https://git.musl-libc.org/cgit/musl/tree/arch/x86_64/crt_arch.h#n10 and https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/start.S;h=9edd17b60cd54ec9eef11c76ab02322dcb5d057a;hb=HEAD#l90).

I’m very new to this kind of low-level programming, so please let me know if I’m completely misunderstanding/overlooking something!

make check passes on my x86-64 machine running macOS 10.13.1, but I haven’t tested it anywhere else; while I don’t see how, it might break something on other platforms.

oridb commented 6 years ago

Will test before merging when I get home, but it should be fine.