Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insufficient stack size in PE header for make #2731

Open
ZigMeowNyan opened this issue Dec 10, 2021 · 2 comments
Open

Insufficient stack size in PE header for make #2731

ZigMeowNyan opened this issue Dec 10, 2021 · 2 comments

Comments

@ZigMeowNyan
Copy link

The make (mingw-w64-x86_64-make 4.3-1) binary has a really small stack size defined in the PE header, which can cause build failures in projects with many/large make files or lots of recursion - all common practices. It's currently set to 2MB, which is the default.

I went through msys2 x64 installation and update today only to run into this while building from qmk. (qmk/qmk_firmware#13416)

I don't think it's exclusive to QMK. I could see this problem cropping up in various other firmware projects, where people would just jump to a VM instead of dealing with the issue. I don't know what other tools might be affected by this, but it definitely makes sense for make.

It can be fixed with a simple edit with some windows SDK tools, and QMK has an msys2 build that applies the fix this way, but it'd be ideal if the upstream package had a bigger stack size. A quick hunt for PE editing tools on linux didn't come up with any results.

There is a flag for setting the stack reserve size in GCC. You can pass -Wl,--stack,134217728 into GCC, and it will pass that to the linker ld as --stack 134217728. It's an option that's only relevant towards PE targets.

I'm not really sure how to apply this to the make build process, since I don't work with make much. Adding -Wl,--stack,134217728 to line 307 of build_w32.bat (and thus build_w32-PKGBUILD.bat) on my msys2 install results in the correct build. Presumably, adding /STACK:134217728 to line 300 would take care of the stack size when linking through MSVC.

I tried adding LFLAGS=--stack 134217728 to the PKGBUILD but I really don't have the environment tools for testing this. Can someone more experienced with make and packaging assist?

@lazka
Copy link
Member

lazka commented Dec 10, 2021

Just to be sure, is this about make.exe or mingw32-make.exe?

@ZigMeowNyan
Copy link
Author

Thank you very much for your quick response.

From the user perspective, it should be make.exe. We're hoping to change the final binary that runs in their environment when they run make.

Rebuilding the PKGBUILD file with makepkg, all I ended up finding after the compile was the other. I'm not sure if it's the wrong binary or compilation target, if there's some rename step in a target, if it's right, or if I'm missing ten obvious points to Gryffindor.

But I did use what I found by digging through man pages and forum posts to test that changing the flags as specified successfully altered the PE header of the compiled file output.

To check the binary header on windows in a developer command prompt, that's dumpbin /headers c:\path\to\make.exe (e.g. dumpbin /headers c:\msys64\usr\bin\make.exe for the original file in the original msys64 package, assuming msys64 is installed to c:\msys64). You scroll down to OPTIONAL HEADER VALUES and look for size of stack reserve. As packaged and with default compiler flags, this value is 200000, but after the change to compiler flags setting the stack reserve, the binary I compiled shows 8000000.

Armed with that I had a smart and went for help. It's been years since I was daily driving Arch or cross-compiling/compiling for multiple platforms with make and I'm clearly more rusty than I remember. Someone else will know better than I how to get this in there properly.

@lazka lazka transferred this issue from msys2/MINGW-packages Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants