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

cmd/link: use __DATA_CONST on darwin? #38830

Open
bradfitz opened this issue May 3, 2020 · 6 comments
Open

cmd/link: use __DATA_CONST on darwin? #38830

bradfitz opened this issue May 3, 2020 · 6 comments

Comments

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented May 3, 2020

Recent iOS/macOS (starting at iOS 9?) support a new segment __DATA_CONST:

https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes?language=objc

In order to improve performance and security the static linker (ld) now moves globals that are marked as constant into a new segment: __DATA_CONST. These globals may consist of compiler generated pointers that the dynamic linker (dyld) needs to fix up during load, but are otherwise constant such as vtables and explicitly declared constant pointers. Once dyld has finished loading the image it makes __DATA_CONST readonly. This change doesn’t impact well behaved code, but may break code that depends on undefined behavior such as using a type pun to write to a pointer that’s declared as const. (50898833)

Other information online suggests that such segments don't contribute to the memory limit of processes on iOS, as they can always be paged in from disk.

Are there any Go sections that could go in that segment?

On a macOS binary now (with -ldflags=-w), I see:

sect[0] = {Name:__text Seg:__TEXT Addr:16781312 Size:1107627 Offset:4096 Align:4 Reloff:0 Nreloc:0 Flags:2147484672}
sect[1] = {Name:__rodata Seg:__TEXT Addr:17888960 Size:519427 Offset:1111744 Align:5 Reloff:0 Nreloc:0 Flags:0}
sect[2] = {Name:__symbol_stub1 Seg:__TEXT Addr:18408416 Size:510 Offset:1631200 Align:5 Reloff:0 Nreloc:0 Flags:2147484680}
sect[3] = {Name:__typelink Seg:__TEXT Addr:18408928 Size:4068 Offset:1631712 Align:5 Reloff:0 Nreloc:0 Flags:0}
sect[4] = {Name:__itablink Seg:__TEXT Addr:18413000 Size:752 Offset:1635784 Align:3 Reloff:0 Nreloc:0 Flags:0}
sect[5] = {Name:__gosymtab Seg:__TEXT Addr:18413752 Size:0 Offset:1636536 Align:0 Reloff:0 Nreloc:0 Flags:0}
sect[6] = {Name:__gopclntab Seg:__TEXT Addr:18413760 Size:666774 Offset:1636544 Align:5 Reloff:0 Nreloc:0 Flags:0}
sect[7] = {Name:__go_buildinfo Seg:__DATA Addr:19083264 Size:32 Offset:2306048 Align:4 Reloff:0 Nreloc:0 Flags:0}
sect[8] = {Name:__nl_symbol_ptr Seg:__DATA Addr:19083296 Size:696 Offset:2306080 Align:2 Reloff:0 Nreloc:0 Flags:6}
sect[9] = {Name:__noptrdata Seg:__DATA Addr:19084000 Size:74788 Offset:2306784 Align:5 Reloff:0 Nreloc:0 Flags:0}
sect[10] = {Name:__data Seg:__DATA Addr:19158816 Size:38288 Offset:2381600 Align:5 Reloff:0 Nreloc:0 Flags:0}
sect[11] = {Name:__bss Seg:__DATA Addr:19197120 Size:200864 Offset:0 Align:5 Reloff:0 Nreloc:0 Flags:1}
sect[12] = {Name:__noptrbss Seg:__DATA Addr:19397984 Size:9992 Offset:0 Align:5 Reloff:0 Nreloc:0 Flags:1}

I know relatively little about linkers but see no reference to __DATA_CONST in Go's tree, and see other projects online that have added __DATA_CONST support, so was curious if it's applicable for Go.

/cc @ianlancetaylor @cherrymui @thanm @jeremyfaller @aclements (people who seem to know linkers)

@bradfitz bradfitz added this to the Unplanned milestone May 3, 2020
@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented May 3, 2020

I think __rodata, __typelink, __itablink, and __gopclntab can go there. These are read-only data sections currently in __TEXT, or __DATA (for PIE binaries and a few configurations).

The question is, is it only supported in newer version of macOS and/or newer version of darwin linker? We probably still need to do the old way to support old platforms.

@bradfitz
Copy link
Contributor Author

@bradfitz bradfitz commented May 4, 2020

Sierra came out a year after iOS 9 so I assume we're good to use it unconditionally at this point.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 4, 2020

At first glance the new section sounds identical to ELF's relro section, which cmd/link already supports.

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented May 4, 2020

Yeah, I believe the linker already has the relevant bits. It's probably just a matter of where to put those sections.

@bradfitz
Copy link
Contributor Author

@bradfitz bradfitz commented May 5, 2020

cc @mwhudson too, as there's a TODO in the code:

        // TODO(mwhudson): It would make sense to do this more widely, but it makes                                                                                               
        // the system linker segfault on darwin.                                                                                                                                  
        addrelrosection := func(suffix string) *sym.Section {
                return addsection(ctxt.Arch, segro, suffix, 04)
        }

Is that darwin segfault referring to __DATA_CONST?

@mwhudson
Copy link
Contributor

@mwhudson mwhudson commented May 5, 2020

Honestly I have no idea any more. That comment is from May 2015 though, which is slightly before iOS 9 -- so I suspect it must have been something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.