Bug 14539 - ar can't generate correct archive
Summary: ar can't generate correct archive
Status: RESOLVED NOTABUG
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.22
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-02 09:05 UTC by Jinsong Zhao
Modified: 2022-02-21 19:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
A minimal complete example, source, object, and archive (1.56 KB, application/x-gzip)
2012-09-02 09:05 UTC, Jinsong Zhao
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jinsong Zhao 2012-09-02 09:05:28 UTC
Created attachment 6622 [details]
A minimal complete example, source, object, and archive

I am in Windows system using MinGW. Here is a minimal example which can reproduce the problem.

The source code:

C:\test>type refs_a.f90
module refs_a
   integer, dimension(4) :: refa=(/1,2,3,4/)
   integer, dimension(2,2) :: refb
   equivalence (refa(1), refb(1,1))
end module refs_a

C:\test>type test_a.f90
program test_a
   use refs_a
   write(*,'(4I4)') refa
   write(*,'(4I4)') refb
end program test_a

Then compiled with following step:

gfortran -c refs_a.f90
gfortran -c test_a.f90
ar -r lib_a.a refs_a.o
gfortran test_a.o lib_a.a -o a.exe
gfortran test_a.o refs_a.o -o b.exe

The output of b.exe is correct:
C:\test>b.exe
   1   2   3   4
   1   2   3   4

However, a.exe give:
C:\test>a.exe
   0   0   0   0
   0   0   0   0

So, I think there might be a bug in ar.

The version of gfortran and ar is:

C:\test>gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=d:/mingw/bin/../libexec/gcc/mingw32/4.7.0/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,obj
c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-
with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.7.0 (GCC)

C:\test>ar --version
GNU ar (GNU Binutils) 2.22
Copyright 2011 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

I also test the above minimal example in a Linux platform. The version of gfortran and ar is '4.4.6 20110731' and ' 2.17.50.0.6-20.el5_8.3 20061020'. Both way of compilation gave correct result.
Comment 1 Mike Frysinger 2022-02-21 19:19:31 UTC
i am by no means a fortran expert, so if i misunderstand how it works, feel free to correct & reopen

the fact that this linking works:
> gfortran test_a.o lib_a.a -o a.exe

implies that the `use refs_a` is a "weak reference".  that means the linker is not required to pull in objects from an archive (.a) to satisfy such symbols alone.  this is how the linker has always behaved, and is always going to behave.

if you want to force linking in all the objects from the archive, you can use the --whole-archive option like:
  gfortran test_a.o -Wl,--whole-archive lib_a.a -Wl,--no-whole-archive -o a.exe

from ar's pov, it is working correctly: it created an archive with an object.  it has nothing else to do.

from ld's pov, it is working correctly: the symbol is already satisfied, and there's no objects it needs to pull out of the .a to satisfy missing symbols.