Closed Zxilly closed 1 month ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
This is an advisory issue rather than a bug, as it doesn't seem to affect the operation of the Go binary.
CC @cherrymui @golang/runtime
I think the moduledata references pcheader with a dynamic relocation (or the equivalent on Mach-O, a "bind" or "rebase" entry). The data you read directly from moduledata is without the relocation applied. You'll need to decode and apply the relocation.
It is possible that the Go linker and C linker (or even different versions of the C linker) use different relocation mechanism, and the pre-relocated data may or may not be meaningful. They are semantically equivalent so at program run time it always point to the right data.
I'm not sure if there is anything we can do.
If that's the reason, I'm wondering why buildmode being exe or pie doesn't change this behaviour. I looked up some other issues, is it because Apple doesn't support generating non-pie binaries anymore either? Similarly, this behaviour is not observed on amd64.
is it because Apple doesn't support generating non-pie binaries anymore either?
That is correct.
Again, the un-relocated data varies depending on architecture, link mode, linker version, etc.. It may happen to be useful, or not.
I investigated the issue further and it doesn't seem to be a relocation issue, please check the attached image, the corresponding sections for moduledata
and pclntab
all have Nreloc of 0
This can also be verified by llvm-otool
Mach-O doesn't use relocations in that sense. They have "rebase" and "bind" tables instead. Try objdump --macho --bind
and objdump --macho --rebase
with the macOS system objdump (from Xcode).
For a binary I built, I have
$ nm x | grep firstmoduledata
00000001000f7b00 s _runtime.firstmoduledata
$ nm x | grep runtime.pclntab
000000010009f820 s _runtime.pclntab
$ objdump -m --rebase ./x | grep 0x1000F7B00
...
__DATA __noptrdata 0x1000F7B00 rebase ptr 0x10009F820
The rebase entry does point to the right address.
As you mentioned above, this is not a bug. And I don't think there is much we can do. Thanks.
Thanks for the suggestion. But I tried llvm-objdump --macho --bind
and llvm-objdump --macho --rebase
on my samples and it seems that the rebase table does not exist in these files, but the address discrepancy is still there.
Specifically, I am referring to the file
https://github.com/Zxilly/go-testdata/releases/download/latest/bin-darwin-1.23-arm64-strip-pie-cgo
Can you please suggest me some other directions?
I got
PS T:\> llvm-objdump --macho --rebase .\bin-darwin-1.23-arm64-strip-pie-cgo
.\bin-darwin-1.23-arm64-strip-pie-cgo:
Rebase table:
segment section address type
PS T:\> llvm-objdump --macho --bind .\bin-darwin-1.23-arm64-strip-pie-cgo
.\bin-darwin-1.23-arm64-strip-pie-cgo:
Bind table:
segment section address type addend dylib symbol
on this file.
I guess it was related to Chained Fixups
You're probably right that chained fixups are related. That is yet another way of expressing dynamic relocations in Mach-O.
Go version
go version devel go1.24-2a10a5351b Wed Aug 14 12:32:08 2024 +0800 windows/amd64
Output of
go env
in your module/workspace:What did you do?
I'm trying to extract the
moduledata.pcHeader
value from the binaries, and the integration test shows this error since go1.21 release.Prior to go 1.21, the runtime.text symbol would still be present in the binary even if the -s flag was passed, masking the problem.
What did you see happen?
the value stored in pcHeader in
runtime.firstmoduledata
is different fromruntime.pclntab
when using external links onmacOS arm64 cgo
Can be reproduced with the following code:
Will results to
Some scripts indicates similiar problem on most recent major Go releases:
This only happens on
macOS arm64
with external link.Here's some binary as input:
https://github.com/Zxilly/go-testdata/releases/download/latest/bin-darwin-1.22-arm64-strip-cgo https://github.com/Zxilly/go-testdata/releases/download/latest/bin-darwin-1.22-arm64-strip-pie-cgo https://github.com/Zxilly/go-testdata/releases/download/latest/bin-darwin-1.23-arm64-strip-cgo https://github.com/Zxilly/go-testdata/releases/download/latest/bin-darwin-1.23-arm64-strip-pie-cgo
What did you expect to see?
These value should keep same as other arch.