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

    <tr>
        <th>Summary</th>
        <td>
            [flang-rt] Static link fails with "multiple definition of `srand_`" when a program defines its own srand and references the RAND extension intrinsic
        </td>
    </tr>

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

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

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

<pre>
    Since #166780, `flang-rt/lib/runtime/extensions.cpp` defines both the RAND/IRAND runtime entry points (`_FortranARand`, `_FortranAIrand`) and the GNU-compatibility wrapper `FORTRAN_PROCEDURE_NAME(srand)` (= `srand_` on Linux) in the same translation unit. A legacy program that defines its **own** `srand` and references `rand()` (a very common pre-F90 pattern, where `integer rand` was the only declaration) fails to link against the static runtime:

```fortran
subroutine srand(seed)
  integer :: seed
end subroutine
program p
  integer :: n, rand
  call srand(1)
  n = rand()
  print *, n
end program
```

```
$ flang -static-libflangrt repro.f90
ld: libflang_rt.runtime.a(extensions.cpp.o): in function `srand_':
multiple definition of `srand_'; repro.o: first defined here
```

The reference to `_FortranARand` (the `rand()` lowering) pulls in `extensions.cpp.o`, whose unrelated `srand_` definition then collides with the program's own. The same object also defines `getarg_`, `iargc_`, `qsort_`, `flush_`, `hostnm_`, `mclock_`, `secnds_`, `dsecnds_`, `fdate_`, `getlog_`, `access_`, `fnum_`, `ierrno_` — a user program defining **any** of these names while using **any** intrinsic implemented in this file hits the same failure.

F2023 4.2 p4 permits resolving `rand()` to the extension intrinsic ("the processor is permitted to use the intrinsic procedure unless the name has the EXTERNAL attribute where it is used"), but the runtime's packaging should not break the link for the rest of a conforming program. gfortran is structurally immune to this class of collision because its intrinsics reference only `_gfortran_*`-prefixed runtime symbols.

Suggested fixes (either):
- (a) mark the `FORTRAN_PROCEDURE_NAME` compatibility wrappers weak on platforms that support weak symbols — verified locally that weakening `srand_` in `extensions.cpp.o` (via `objcopy --weaken-symbol=srand_`) makes the reproducer link and run, with the program's strong definition winning as expected; or
- (b) move each Fortran-mangled compatibility wrapper into its own translation unit so the linker only pulls in what is actually referenced.

Related documentation gap: RAND/IRAND are missing from the "Nonstandard intrinsic functions" list in `flang/docs/Extensions.md` (it currently names only ISNAN and SIZEOF).

Observed with flang 24.0.0git (9b1aee96e8897ca7c8bfa363ec87c7acf5d6b656) on x86_64-unknown-linux-gnu.

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEV11v47oR_TXMy8CGTDuy_OAH3U1cLHCbLZK9QNGXgKJGEm8oUuWHHf_7YijZVjZZFDBgiyLn48zhmbHwXrUGcc_u_2D3D3cihs66PcYWDeLgu4Bnc1fZ-rx_UUYiML5e5fm2yBj_BizPGi1Mu3CB8YNWFeMHF01QPTJ-wPeAxitr_FIOA8szqLFRBj1UNnQQOoTn8umB8cN3-obpJKAJ7gyDVSZ4YLxgefZ6sC44YcpnYWqWX5xfl7-7y_oOhKmT7X88_bWQth9EUJXSKpzh5MQwoKOThx_PP5_Lp9d_Pf_49vjw1_Pj61P5z0fGC58s8R2FS77XD7Q9rb7SmjXwpzLxnTwpkxx50SNQGF6LoKyBaFRYQgkaWyHPMDjbOtFD6ES4QqBSbiXjpT2Z8cfVEfmhLBw26NBI9PRqDKy4xSbgiO4M0va9NTA4XBx2GQwiBHSGEDp16JCOKhOwRQcX4yfhU-TW6DPUKLVwKXJKqhFKewgWtDJvIFqhjA9jnkEEJS9lYuuSZemTZ-OnGYvBstLHytkYlEGY8Cw8YoI1KwEu4ZCJdQnpVVaiqeF2kGXlBbfhq0MpwWSbXkqh9dXV6uLHAJVvhhstDk6ZMEL_Dczkd3I1z-aX5Og330CiOyxGKBZaVWnBBXA4OLtsdrRR1xTh5eWrC8sJs6VgvPh4LZaWAluXRKYmGpkIdGMc344491EHNWgc-aPSLtv8svGPKQpL9hrl_IVuNRATPm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