<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/149222>149222</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang] Error-prone representation of large common symbols
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vzakhari
      </td>
    </tr>
</table>

<pre>
    The linker fails for a Fortran program ([repro.f90.gz](https://github.com/user-attachments/files/21267956/repro.f90.gz)) that is linked with a library:
```
$ flang -mcmodel=medium repro.f90 -lmylib
/tmp/repro-585cda.o: in function `_QQmain':
FIRModule:(.text+0xb): failed to convert GOTPCREL relocation against 'ccc_'; relink with --no-relax
FIRModule:(.text+0x1f): failed to convert GOTPCREL relocation against 'ccc_'; relink with --no-relax
FIRModule:(.text+0x50): failed to convert GOTPCREL relocation against 'ccc_'; relink with --no-relax
FIRModule:(.text+0x65): failed to convert GOTPCREL relocation against 'ccc_'; relink with --no-relax
```

I cannot provide the proprietary library, but I am attaching its stats indicating that it does not violate the `-mcmodel=medium` limitations: [library_stats.txt.gz](https://github.com/user-attachments/files/21267955/library_stats.txt.gz)

`ifx` 2025 compiler fails the same way:
```
$ ifx -mcmodel=medium repro.f90 -lmylib
ld: failed to convert GOTPCREL relocation; relink with --no-relax
```

Gfortran 8.5.0 and 15.1.0 work well.

I believe the problem is that Flang does not mark the large common symbols as `SHN_X86_64_LCOMMON`:
```
$ flang -mcmodel=medium repro.f90 -c
$ readelf -s repro.o
     8: 0000000000000008   144 OBJECT  GLOBAL DEFAULT  COM ccc_
 16: 0000000000000008 0x90331e28 OBJECT  GLOBAL DEFAULT  COM __BLNK__
```

While gfortran does:
```
$ gfortran -mcmodel=medium repro.f90 -c
$ readelf -s repro.o
     7: 0000000000000020   144 OBJECT GLOBAL DEFAULT  COM ccc_
    15: 0000000000000020 0x90331e28 OBJECT  GLOBAL DEFAULT LARGE_COM __BLNK__
$ llvm-readelf -s repro.o
     7: 0000000000000020   144 OBJECT  GLOBAL DEFAULT   COM ccc_
    15: 0000000000000020 2419269160 OBJECT GLOBAL DEFAULT PRC[0xff02] __BLNK__
```

The large common blocks are allocated in `.lbss` by the linker, so they do not affect the distance between the "small" symbols and their relocations: https://lld.llvm.org/ELF/large_sections.html

It looks like LLVM toolchain does not support `SHN_X86_64_LCOMMON` at all (see the `llvm-readelf` output above that cannot recognize the `SHN_X86_64_LCOMMON` flag, which is defined as:
```
#define SHN_LORESERVE 0xff00
#define SHN_X86_64_LCOMMON  (SHN_LORESERVE + 2)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVtGT2rYT_mvEyw4eWcYGHniAO7hffuVCcrmmfWNke22ryBIjyRzkr-9IcCTkyDVt2omHGTP2-tvdb3e_FbdW1ApxQtIZSW97vHONNpPdJ75puBG9XJeHyWODIIXaoIGKC2mh0gY4LLRxhivYGl0b3gJhI5LODG6NjqoxjepPJL0lbNQ4t7UkmRK2IGxRC9d0eVTolrBFZ9H0uXO8aFpUzhK2qIREf2cxy4bjNCNscQHJxoSNwTXcgbDHsEp4Eq4BDlLkhpuD90WnJKOnH50SNoBKclVDvy1aXaIkyW2LpehaOKNDX7YHKfJgv3Dt9tl1Px2lRckjTZIpCAVVpwontAKS0fX79y0XirDh0evizcO9LjuJIeFR5HDvCJvRfe4jT6aBQSzBaSi02qFxcLd6fHfzMF-CQakLHpB5zYWyDggbFkWxDvAzbyDU5phtv69036Dk-9e8xtVPcZvSn-I2S_9LtxcNRadvoOBKaef7fydKBNeg_781Ah03h3M3shvIOwdvgLdw7HWhahDOgnXcWRCqFD4iVZ_a2kGp0YLH3gktuTtik4y-6F6SUZCiFS5k5KcMSDo7eV4H_Mjt3b8ziylhi6vQbHykhGRUVHsfE6MshUK3WyHPquFzsLxFeOLfGFFR7b9zQGX53WX-OzW9q06aNorSiAJXJcRpFEcUnrTZwB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