-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: -T flag doesn't place text segment at specified address #58727
Comments
Looks as though what's happening here is that the linker is taking the specified -T value and then subtracting off HEADR, which is set to ELFRESERVE here: Perhaps the intent for "-T" was to ensure that the address of the first actual function in the text segment is set to the specified value? It's hard to tell. @cherrymui let me know if you know the history behind this. |
If that is the case, that seems to be the wrong behavior since the docs say that "-T" sets the address of the segment. |
I don't know the history behind this. It seems it has been like so (after the header, not before) since the beginning. Manually using flags like this is not generally supported, and the meaning of the flag is not accurately specified, especially given that (at least on some platforms) the header is part of the text segment. Since it's been so for a long time, I think we just document that -T means the start address of text symbols. |
I agree with @cherrymui. I might add that when I look at similar functionality with clang/lld, it also doesn't line up exactly on the dot at 0x40000000:
|
Like you mentioned using the flags isn't often used. However, they do exist to be used, no? If so, having "-T" specify the start of the elf header is prolly best. It means that it's possible to load the segment at the beginning of a memory page. Which is my use case. I'm not familiar with PE and Mach-O but does link have equivalent behavior of specifying the start of the text symbol on these? |
Is this not because the the text symbol is being placed after the read only data? Because if a linker script was used to place the text symbol first it would be aligne. Since lld did place rodata at the specified address. |
The current behavior of The GNU linker has a similar confusion about |
Change https://go.dev/cl/472356 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Cross-compile an empty main function to linux with -R=0x1000 and -T=0x40000000 linker flags.
What did you expect to see?
The text segment start at 0x40000000. (Example linked with +0xFFF)
$ readelf -Wl a.out Elf file type is EXEC (Executable file) Entry point 0x400535d0 There are 7 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000040 0x000000004000003f 0x000000004000003f 0x000188 0x000188 R 0x1000 NOTE 0x000f9c 0x0000000040000f9b 0x0000000040000f9b 0x000064 0x000064 R 0x4 LOAD 0x000001 0x0000000040000000 0x0000000040000000 0x054da0 0x054da0 R E 0x1000 LOAD 0x055000 0x0000000040055000 0x0000000040055000 0x065098 0x065098 R 0x1000 LOAD 0x0bb000 0x00000000400bb000 0x00000000400bb000 0x0034c0 0x03ac90 RW 0x1000 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8 LOOS+0x5041580 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 0x8 Section to Segment mapping: Segment Sections... 00 01 note.go.buildid 02 text note.go.buildid 03 rodata typelink itablink gosymtab gopclntab 04 go.buildinfo noptrdata data bss noptrbss 05 06
What did you see instead?
The text segment starts at 0x3FFFF000.
$ readelf -Wl a.out Elf file type is EXEC (Executable file) Entry point 0x400525d0 There are 7 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000040 0x000000003ffff040 0x000000003ffff040 0x000188 0x000188 R 0x1000 NOTE 0x000f9c 0x000000003fffff9c 0x000000003fffff9c 0x000064 0x000064 R 0x4 LOAD 0x000000 0x000000003ffff000 0x000000003ffff000 0x054da0 0x054da0 R E 0x1000 LOAD 0x055000 0x0000000040054000 0x0000000040054000 0x065098 0x065098 R 0x1000 LOAD 0x0bb000 0x00000000400ba000 0x00000000400ba000 0x0034c0 0x03ac90 RW 0x1000 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8 LOOS+0x5041580 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 0x8 Section to Segment mapping: Segment Sections... 00 01 .note.go.buildid 02 .text .note.go.buildid 03 .rodata .typelink .itablink .gosymtab .gopclntab 04 .go.buildinfo .noptrdata .data .bss .noptrbss 05 06
The text was updated successfully, but these errors were encountered: