@@ -93,6 +93,12 @@ def find_library(name):
93
93
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
94
94
import re , tempfile
95
95
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
+
96
102
def _findLib_gcc (name ):
97
103
# Run GCC's linker with the -t (aka --trace) option and examine the
98
104
# library name it prints out. The GCC command will fail because we
@@ -130,10 +136,17 @@ def _findLib_gcc(name):
130
136
# Raised if the file was already removed, which is the normal
131
137
# behaviour of GCC if linking fails
132
138
pass
133
- res = re .search (expr , trace )
139
+ res = re .findall (expr , trace )
134
140
if not res :
135
141
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 )
137
150
138
151
139
152
if sys .platform == "sunos5" :
@@ -299,17 +312,22 @@ def _findLib_ld(name):
299
312
stderr = subprocess .PIPE ,
300
313
universal_newlines = True )
301
314
out , _ = p .communicate ()
302
- res = re .search (expr , os .fsdecode (out ))
303
- if res :
304
- result = res .group (0 )
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 )
305
323
except Exception :
306
324
pass # result will be None
307
325
return result
308
326
309
327
def find_library (name ):
310
328
# See issue #9998
311
329
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 ))
313
331
314
332
################################################################
315
333
# test code
0 commit comments