llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

ld.lld: Remove --fortran-common

Open MaskRay opened this issue 5 months ago • 6 comments

Remove --fortran-common / --no-fortran-common from ld.lld (ELF), so COMMON symbols follow the same archive-member-extraction rule as ordinary definitions. To the best of my knowledge there is no public user of this feature.

CC @mandlebug, who introduced this option in D86142 (2020) for IBM ESSL on PowerPC(?). I'd like to propose removing it, and would welcome your thoughts.

--fortran-common makes ld.lld replicate a legacy GNU ld rule (itself inherited from SUN's linker). From https://maskray.me/blog/2022-02-06-all-about-common-symbols :

When a symbol is COMMON and ld sees an archive, ld checks whether the archive index provides a STB_GLOBAL definition of the symbol. If yes, ld extracts the archive as well. This is in contrary to the usual rule that only an undefined symbol leads to archive me mber extraction.

This is surprising when COMMON and STB_GLOBAL definitions are mixed across archives:

Say b0.a and b1.a are mostly identical archives, but b0.a objects are compiled with -fcommon while b1.a objects are compiled with -fno-common. If a.o references b0.a, this archive lookup behavior may cause a duplicate definition error for ld a.o b0.a b1.a while b1.a can be shadowed by b0.a without the rule.

It's an awkward position-dependent behavior.

% ld.lld a.o b1.a b0.a && ld.lld a.o b0.a b1.a
ld.lld: warning: cannot find entry symbol _start; not setting start address
ld.lld: warning: cannot find entry symbol _start; not setting start address

% ld.lld a.o b1.a b0.a --fortran-common; ld.lld a.o b0.a b1.a --fortran-common
ld.lld: warning: cannot find entry symbol _start; not setting start address
ld.lld: error: duplicate symbol: foo
>>> defined at b.c
>>>            b0.o:(foo) in archive b0.a
>>> defined at b.c
>>>            b1.o:(.text+0x0) in archive b1.a

And it is awkward for parallel symbol resolution which I am experimenting with. (Linker performance is critical for lld to stay competitive and not be obsoleted within a few years. Carrying a niche legacy rule that obstructs it is not a good trade....)

MaskRay avatar Jun 21 '26 22:06 MaskRay

@llvm/issue-subscribers-lld-elf

Author: Fangrui Song (MaskRay)

Remove `--fortran-common` / `--no-fortran-common` from ld.lld (ELF), so COMMON symbols follow the same archive-member-extraction rule as ordinary definitions. To the best of my knowledge there is no public user of this feature.

CC @mandlebug, who introduced this option in D86142 (2020) for IBM ESSL on PowerPC(?). I'd like to propose removing it, and would welcome your thoughts.

--fortran-common makes ld.lld replicate a legacy GNU ld rule (itself inherited from SUN's linker). From https://maskray.me/blog/2022-02-06-all-about-common-symbols :

> When a symbol is COMMON and ld sees an archive, ld checks whether the archive index provides a STB_GLOBAL definition of the symbol. If yes, ld extracts the archive as well. This is in contrary to the usual rule that only an undefined symbol leads to archive me mber extraction.

This is surprising when COMMON and STB_GLOBAL definitions are mixed across archives:

> Say b0.a and b1.a are mostly identical archives, but b0.a objects are compiled with -fcommon while b1.a objects are compiled with -fno-common. If a.o references b0.a, this archive lookup behavior may cause a duplicate definition error for ld a.o b0.a b1.a while b1.a can be shadowed by b0.a without the rule.

It's an awkward position-dependent behavior.

% ld.lld a.o b1.a b0.a && ld.lld a.o b0.a b1.a
ld.lld: warning: cannot find entry symbol _start; not setting start address
ld.lld: warning: cannot find entry symbol _start; not setting start address

% ld.lld a.o b1.a b0.a --fortran-common; ld.lld a.o b0.a b1.a --fortran-common
ld.lld: warning: cannot find entry symbol _start; not setting start address
ld.lld: error: duplicate symbol: foo
>>> defined at b.c
>>>            b0.o:(foo) in archive b0.a
>>> defined at b.c
>>>            b1.o:(.text+0x0) in archive b1.a

And it is awkward for parallel symbol resolution which I am experimenting with. (Linker performance is critical for lld to stay competitive and not be obsoleted within a few years. Carrying a niche legacy rule that obstructs it is not a good trade....)

@mandlebug :)

MaskRay avatar Jul 12 '26 23:07 MaskRay

Hi @MaskRay, sorry for taking so long to respond. You are right I added this mainly for supporting linking ESSL with LLD and we no longer ship that library so that need is gone. IIRC though it was more a common pattern in older Fortran that when linked into archives needed the bfd specific behavior to work correctly. Essentially ESSL was structured the way it was to match other popular FORTRAN archives that it would be used with or substituted for. If that is the case it could still be an issue with code built by flang. Fortran is not really my forte so I'll chat with @kkwli to see what the implications are.

mandlebug avatar Jul 20 '26 15:07 mandlebug

Hi @MaskRay, sorry for taking so long to respond. You are right I added this mainly for supporting linking ESSL with LLD and we no longer ship that library so that need is gone. IIRC though it was more a common pattern in older Fortran that when linked into archives needed the bfd specific behavior to work correctly. Essentially ESSL was structured the way it was to match other popular FORTRAN archives that it would be used with or substituted for. If that is the case it could still be an issue with code built by flang. Fortran is not really my forte so I'll chat with @kkwli to see what the implications are.

@kkwli :)

MaskRay avatar Aug 01 '26 16:08 MaskRay

I discussed it with Kelvin. The problem still exists with Flang compiled code, but we think it isn't super common since as you pointed out in the original issue the pattern is limited to fortran 77 with newer language levels having better constructs to avoid the common blocks. He created a discussion on discourse to see if anyone else in the community was concerned at the removal, but it doesn't look like its an issue.

And it is awkward for parallel symbol resolution which I am experimenting with.

Have you considered treating a common symbol as undefined throughout the search and only upgrading it to a definition once resolution has finished? That shouldn't have much impact on the parallel symbol resolution implementation, keeps the bfd style handling and should only have a link time cost for code bases that actually have common symbols.

mandlebug avatar Aug 17 '26 17:08 mandlebug

Woud love to take this :)

foxhasabox avatar Sep 07 '26 20:09 foxhasabox