-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[MachO] add -pagezero_size #11875
[MachO] add -pagezero_size #11875
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @motiejus. I hope you won't mind if I take it from here. As it currently stands, this PR needs a bit more work as it doesn't check the alignment on the requested page size, nor does it get rid of the segment if the requested page size is 0.
I found this PR in LLVM which I will use a guide when patching things up a little: D118724.
src/link/MachO.zig
Outdated
@@ -4374,7 +4375,7 @@ fn populateMissingMetadata(self: *MachO) !void { | |||
.segment = .{ | |||
.inner = .{ | |||
.segname = makeStaticString("__PAGEZERO"), | |||
.vmsize = pagezero_vmsize, | |||
.vmsize = self.base.options.pagezero_size orelse pagezero_vmsize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK you cannot just set the value to whatever you want without aligning to the page size.
By all means, thank you! |
@motiejus I've applied all fixes that were required, and tested that the resultant binaries (in user-space) run fine with different |
Pass `-pagezero_size` to the MachO linker. This is the final "unsupported linker arg" that I could chase that CGo uses. After this and ziglang#11874 we may be able to fail on an "unsupported linker arg" instead of emiting a warning. Test case: zig=/code/zig/build/zig CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC="$zig cc -target x86_64-macos" CXX="$zig c++ -target x86_64-macos" go build -a -ldflags "-s -w" cgo.go I compiled a trivial CGo program and executed it on an amd64 Darwin host. To be honest, I am not entirely sure what this is doing. This feels right after reading what this argument does in LLVM sources, but I am by no means qualified to make MachO pull requests. Will take feedback.
If page aligned requested pagezero size is 0, skip generating __PAGEZERO segment. Add misc improvements to the pipeline, and correctly transfer the requested __PAGEZERO size to the linker.
OK, will report here.
The cgo binary executed; that's all I can report. It was very simple though, so maybe it was not hitting a code path that required the
Ack. |
No, you literally forgot to pass the value to |
cgo.go
Compile:
Golang requests this:
https://github.com/golang/go/blob/go1.18.3/src/cmd/link/internal/ld/lib.go#L1327 ... which, if unaligned, should probably be reported to them? Also, it would be good if the warning message would report the requested pagezero_size together with the "rounded down" to make debugging easier. Also, the resulting binary executes correctly. |
Oh, I think we don't parse it correctly.
Sounds good.
|
This matches the behavior of other linkers out there including `ld64` and `lld`.
Thanks. I think we are in LGTM land now. :) |
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see ziglang#11679, ziglang#11875, ziglang#11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: ziglang#11614 and ziglang#11863.
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see ziglang#11679, ziglang#11875, ziglang#11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: ziglang#11614 and ziglang#11863.
Just checked that Go uses |
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see ziglang#11679, ziglang#11875, ziglang#11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: ziglang#11614 and ziglang#11863.
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see ziglang#11679, ziglang#11875, ziglang#11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: ziglang#11614 and ziglang#11863.
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see ziglang#11679, ziglang#11875, ziglang#11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: ziglang#11614 and ziglang#11863.
Currently `zig cc`, when confronted with a linker argument it does not understand, skips the flag and emits a warning. This has been causing headaches for people that build third-party software (including me). Zig seemingly builds and links the final executable, only to segfault when running it. If there are linker warnings when compiling software, the first thing we have to do is add support for ones linker is complaining, and only then go file issues. If zig "successfully" (i.e. status code = 0) compiles a binary, there is instead a tendency to blaim "zig doing something weird". (I am guilty of this.) In my experience, adding the unsupported arguments has been quite easy; see #11679, #11875, #11874 for recent examples. With the current ones (+ prerequisites below) I was able to build all of the CGo programs that I am encountering at $dayjob. CGo is a reasonable example, because it is exercising the unusual linker args quite a bit. Prerequisites: #11614 and #11863.
Pass
-pagezero_size
to the MachO linker. This is the final"unsupported linker arg" that I could chase that CGo uses. After this
and #11874 we may be able to fail on an "unsupported linker arg" instead
of emiting a warning.
Test case:
I compiled a trivial CGo program and executed it on an amd64 Darwin
host.
To be honest, I am not entirely sure what this is doing. This feels
right after reading what this argument does in LLVM sources, but I am by
no means qualified to make MachO pull requests. Will take feedback.