weakref of symbol defined in static library

王鑫 yw987194828@163.com
Fri Apr 18 01:47:46 GMT 2025


I write a gfortran program omp.f90 as following


use omp_lib
!$omp parallel
  write(*,*) "threads", omp_get_num_threads()
  !$omp end parallel
end


compile omp.f90 with "gfortran -fopenmp -o omp omp.f90 -static"
./omp abort
The reson is static executable invoke destroy_unit_mutex which is defined in libgfortran.a. 
destroy_unit_mutex invoke pthread_mutex_destroy, but this is a weakref. So linker does not find the definition of pthread_mutex_destroy in libpthread.a. Finally, static executable omp does not contain
the definition of pthread_mutex_destroy, so omp error when it invoke pthread_mutex_destroy.


The code about "weakref will not find it definition in static library" as following.
      if (h->type != bfd_link_hash_undefweak)
/* Symbol must be defined.  Don't check it again.  */
included[i] = true;


      if (!is_elf_hash_table (info->hash))
continue;
      struct elf_link_hash_entry *eh
= (struct elf_link_hash_entry *) h;
      /* Ignore the archive if the symbol isn't referenced by a
regular object or isn't defined in a shared object.  */
      if (!eh->ref_regular || !eh->def_dynamic)
continue;


Why can't weakref look for definitions in the static library, is there a way to statically compile omp.f90? I defined SUPPORTS_WEAK as 0 in the libgfortran directory of the gcc repository, and the generated libgfortran.a enables omp.f90 to be compiled statically, but the generated libgfortran.so gets an error when adding it to the link process because of the pthread_mutex_destroy and other functions are no longer weakref, the linking process does not find the definitions of these functions.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/binutils/attachments/20250418/84143ea8/attachment.htm>


More information about the Binutils mailing list