1Backport https://github.com/python/cpython/commit/82df3b3071bb003247c33eac4670775e9883c994
2and https://github.com/python/cpython/commit/27ac19cca2c639caaf6fedf3632fe6beb265f24f
3
4Fixes the check phase of python2Packages.cffi.
5
6--- a/Lib/ctypes/util.py
7+++ b/Lib/ctypes/util.py
8@@ -87,6 +87,12 @@ elif os.name == "posix":
9 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
10 import re, tempfile, errno
11
12+ def _is_elf(filename):
13+ "Return True if the given file is an ELF file"
14+ elf_header = b'\x7fELF'
15+ with open(filename, 'rb') as thefile:
16+ return thefile.read(4) == elf_header
17+
18 def _findLib_gcc(name):
19 # Run GCC's linker with the -t (aka --trace) option and examine the
20 # library name it prints out. The GCC command will fail because we
21@@ -110,10 +116,17 @@ elif os.name == "posix":
22 # the normal behaviour of GCC if linking fails
23 if e.errno != errno.ENOENT:
24 raise
25- res = re.search(expr, trace)
26+ res = re.findall(expr, trace)
27 if not res:
28 return None
29- return res.group(0)
30+
31+ for file in res:
32+ # Check if the given file is an elf file: gcc can report
33+ # some files that are linker scripts and not actual
34+ # shared objects. See bpo-41976 for more details
35+ if not _is_elf(file):
36+ continue
37+ return file
38
39
40 if sys.platform == "sunos5":
41@@ -237,8 +250,37 @@ elif os.name == "posix":
42 def _findSoname_ldconfig(name):
43 return None
44
45+ def _findLib_ld(name):
46+ # See issue #9998 for why this is needed
47+ expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
48+ cmd = ['ld', '-t']
49+ libpath = os.environ.get('LD_LIBRARY_PATH')
50+ if libpath:
51+ for d in libpath.split(':'):
52+ cmd.extend(['-L', d])
53+ cmd.extend(['-o', os.devnull, '-l%s' % name])
54+ result = None
55+ try:
56+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
57+ stderr=subprocess.PIPE,
58+ universal_newlines=True)
59+ out, _ = p.communicate()
60+ res = re.findall(expr, out)
61+ for file in res:
62+ # Check if the given file is an elf file: gcc can report
63+ # some files that are linker scripts and not actual
64+ # shared objects. See bpo-41976 for more details
65+ if not _is_elf(file):
66+ continue
67+ return file
68+ except Exception:
69+ pass # result will be None
70+ return result
71+
72 def find_library(name):
73- return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
74+ # See issue #9998
75+ return _findSoname_ldconfig(name) or \
76+ _get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))
77
78 ################################################################
79 # test code