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

    <tr>
        <th>Summary</th>
        <td>
            flang: failure of IO on Windows/MinGW-w64 with separate shared library
        </td>
    </tr>

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

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

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

<pre>
    This example fails to read/write a file on MinGW-w64.
input.f90
```Fortran
module input
  implicit none
contains
  subroutine read_input(in_unit, i)
    integer, intent(in) :: in_unit
    integer, intent(out) :: i
    read( in_unit, * ) i
  end subroutine read_input
end module
```
main.F90
```Fortran
program test
  use input
 implicit none
  integer :: io_unit, i
#ifdef USE_NEWUNIT
  open( newunit = io_unit, file = 'input.txt', status = 'unknown', action = 'readwrite' )
#else
  io_unit = 20
  open( unit = io_unit, file = 'input.txt', status = 'unknown', action = 'readwrite' )
#endif
 write(io_unit, *), 10
  rewind(io_unit)
  call read_input(io_unit, i)
  print*, i
end program
```
If both files compiled into single file, It works
```
$ flang -c main.F90
$ flang -c input.f90
$ flang main.o input.o -o test.exe
$ ./test.exe
  10
```
but if IO is part of a library then:
```
$ flang -shared input.o -o libtest.dll -Wl,--out-implib=libtest.dll.a
$ flang main.o -o test -ltest -L.
$ ./test.exe

fatal Fortran runtime error(D:/dev/Bugs/fortran/input.f90:7): End of file during input
$ flang -c -DUSE_NEWUNIT main.F90
$ flang main.o -o test -ltest -L.
$ ./test.exe

fatal Fortran runtime error(D:/dev/Bugs/fortran/input.f90:7): Negative unit number is not allowed
```

Note that this example works as expected on Linux/Ubuntu-22.04
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVc1u4zgTfBr60pAgU_49-OCJow8BZjKHb4IcA0psWb1DkwJ_4szbLyjJsmIk2eMukEgwu5rsqq4WhXN01Ig7tvzG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