@@ -93,6 +93,12 @@ def find_library(name):
9393 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
9494 import re , tempfile
9595
96+ def _is_elf (filename ):
97+ "Return True if the given file is an ELF file"
98+ elf_header = b'\x7f ELF'
99+ with open (filename , 'br' ) as thefile :
100+ return thefile .read (4 ) == elf_header
101+
96102 def _findLib_gcc (name ):
97103 # Run GCC's linker with the -t (aka --trace) option and examine the
98104 # library name it prints out. The GCC command will fail because we
@@ -130,10 +136,17 @@ def _findLib_gcc(name):
130136 # Raised if the file was already removed, which is the normal
131137 # behaviour of GCC if linking fails
132138 pass
133- res = re .search (expr , trace )
139+ res = re .findall (expr , trace )
134140 if not res :
135141 return None
136- return os .fsdecode (res .group (0 ))
142+
143+ for file in res :
144+ # Check if the given file is an elf file: gcc can report
145+ # some files that are linker scripts and not actual
146+ # shared objects. See bpo-41976 for more details
147+ if not _is_elf (file ):
148+ continue
149+ return os .fsdecode (file )
137150
138151
139152 if sys .platform == "sunos5" :
@@ -299,17 +312,22 @@ def _findLib_ld(name):
299312 stderr = subprocess .PIPE ,
300313 universal_newlines = True )
301314 out , _ = p .communicate ()
302- res = re .search (expr , os .fsdecode (out ))
303- if res :
304- result = res .group (0 )
305- except Exception as e :
315+ res = re .findall (expr , os .fsdecode (out ))
316+ for file in res :
317+ # Check if the given file is an elf file: gcc can report
318+ # some files that are linker scripts and not actual
319+ # shared objects. See bpo-41976 for more details
320+ if not _is_elf (file ):
321+ continue
322+ return os .fsdecode (file )
323+ except Exception :
306324 pass # result will be None
307325 return result
308326
309327 def find_library (name ):
310328 # See issue #9998
311329 return _findSoname_ldconfig (name ) or \
312- _get_soname (_findLib_gcc (name ) or _findLib_ld (name ))
330+ _get_soname (_findLib_gcc (name )) or _get_soname ( _findLib_ld (name ))
313331
314332################################################################
315333# test code
0 commit comments