# Author: M Felt, aixtools.net, 2016 # ctype support for cdll interface to dlopen() for AIX # find .so aka shared library members in .a files (i.e., archives) # # by default, on AIX dlopen follows the "blibpath" that can be seen # by the command "/usr/bin/dump -XNN -H exe_so_member" # As python executable probably does not include the $prefix/lib # implied in the ./configure --prefix=/foo/dir # then LIBPATH, when not already defined, is set to _sys.prefix/lib # # With this 'assist' AIX dlopen() will also search ${prefix}/lib for libraries # # NOTE: RTLD_NOW is AIX default, regardless (as of March 2016) (set above!) # NOTE: RTLD_MEMBER is needed by dlopen to open a shared object from within an archive # NITE: RTLD_NOW and RTLD_MEMBER are defined and assigned (for now) in ctypes/__init__.py # # find_library() returns the archive(member) string needed by dlopen() to open a member # as part of an archive # when versioned .so files are requested, e.g. libintl.so.8, libc.so.6, # but it is not available -> None is returned # # when a 'generic' .so file is requested, e.g., libintl.so, of libc.so # - if something is available, that is returned # # "stem" names, such as "c" or "intl" find the latest available version, # or nothing, if a shared library does not exist (e.g., "m", and "libm" will probably fail) import re as _re, os as _os, subprocess as _subprocess, sys as _sys # need machine size to find the correct member in an archive def _aixABI(): if (_sys.maxsize < 2**32): return 32 else: return 64 # return a file (stdout) of the dump command of an object # stderr still goes to 'tty', even with stderr=None specified # stderr=_os.devnull, def _get_dumpH(_exe_so_or_a, p=None, lines=None): p = _subprocess.Popen(["/usr/bin/dump", "-X%s"%_aixABI(), "-H", _exe_so_or_a], universal_newlines=True, stdout=_subprocess.PIPE) lines=p.stdout.readlines() p.stdout.close() p.wait() return lines # 64-bit archive members have different legacy names than 32-bit ones def _get_member64(stem, lines): # Recall that member names are in square brackets [] # an old convention - insert _64 between libFOO and .so expr = r'\[lib%s_*64\.so\]' % stem res = _re.findall(expr, lines) # two AIX conventions for 64-bit archive members: shr4_64.o and shr_64.o # From memory, shr4_64.o is the later convention if not res: expr = r'\[shr4_64.o\]' res = _re.findall(expr, lines) if not res: expr = r'\[shr_64.o\]' res = _re.findall(expr, lines) if res: return res[0][1:-1] else: return None # Looking for a versioned .so filename # Get the most recent/highest numbered version def _get_version(stem, lines): # sort function, borrowed from below def _num_version(libname): # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] parts = libname.split(".") nums = [] try: while parts: nums.insert(0, int(parts.pop())) except ValueError: pass return nums or [ _sys.maxsize ] expr = r'\[lib%s.so.[0-9]+.*]' % stem versions = [m.group(0) for line in lines for m in [_re.search(expr, line)] if m] if versions: versions.sort(key=_num_version) return versions[-1][1:-1] return None # return an archive member matching name # if a (versioned) .so member exists, it is returned rather than a legacy AIX member name # the assumption is that a .so mamber was added to the library - and the old members are # there to support legacy applications # # member names in in the dumpH output are in square brackets # These get stripped and regular parenthesis are added by the caller def _get_match(expr, lines, line=None, member=None): matches = [line for line in lines if expr in line] if len(matches) == 1: match=matches[0] member = match[match.find("[")+1:match.find("]")] return member else: return None def _get_member(stem, name, lines): # FIXME: no longer sure about test here # if name ends in a digit, a specific version is wanted, this or nothing # if _re.findall("\d$", name): # return None # look first for an exact match lines = [line for line in lines if line.startswith("/")] expr = r'[%s]' % name member = _get_match(expr, lines) if member: return member # look a generic match # expr = r'[lib%s.so]' % stem member = _get_match(expr, lines) if member: return member member = _get_version(stem, lines) if member: return member # to be here no member has been found # now look for legacy AIX member names # 64-bit legacy names - shr4.o and shr.o if _aixABI() == 64: return(_get_member64(stem, lines)) # 32-bit searches # legacy names - shr4.o and shr.o # Not using elif because first exact match counts # must try again after each mismatch expr = r'[shr4.o]' member = _get_match(expr, lines) if member: return member expr = r'[shr.o]' member = _get_match(expr, lines) if member: return member else: return None # make an LIBPATH like string that will be searched directory by directory # where the first match is THE match def _getExecLibPath_aix(libpaths=None, x=None, lines=None, line=None, skipping=None): libpaths = _os.environ.get('LIBPATH') lines = _get_dumpH(_sys.executable) skipping = True; for line in lines: if skipping == False: x = line.split()[1] if (x.startswith('/') or x.startswith('./') or x.startswith('../')): if libpaths is not None: libpaths = "%s:%s" % (libpaths, x) else: libpaths = x elif line.find("INDEX PATH") == 0: skipping = False; return libpaths # search all LIBPATH executable "library search paths" for an archive with member # return a AIX dlopen(able) target, or None def find_library(name, _name=None, _stem=None, envpath=None, lines=None, path=None, member=None, shlib=None): # A) if an argument resolves as a filename, return ASIS # This is for people who are trying to match a file as "./libFOO.so" # or using a fixed (fullpath) if _os.path.exists(name): return name # B) the default AIX behavior is to find a member within a .a archive # find the shared library for AIX # name needs to in it's shortest form: libFOO.a, libFOO.so.1.0.1, etc. # are all FOO, as in link arguments -lFOO _stem = "%s." % name if _stem.find("lib") < 0: _stem = _stem[0:_stem.find(".")] else: _stem = _stem[3:_stem.find(".")] # see https://www.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/dlopen.htm?lang=en # and https://www.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/load.htm?lang=en # for how LIBPATH and/or LD_LIBRARY_LD are used by dlopen() (actually loadAndInit()) # the expectation is that normal python packaging, on AIX, will not include ${prefix}/lib in # the library search path - aka -W,-blibpath value - we add it here when LIBPATH is not defined already # if the GNU autotools are configured to add ${prefix}/lib for all compilers, and also ${prefix)/lib64 # when using gcc (that may expect seperate directories rather than .a archives) envpath = _os.environ.get('LIBPATH') if envpath is None: _os.environ['LIBPATH'] = "%s/lib" % _sys.prefix paths = _getExecLibPath_aix() for dir in paths.split(":"): if dir == "/lib": continue # AIX rtl searches .a, by default, if it exists _arfile = _os.path.join(dir, "lib%s.a" % _stem) if _os.path.exists(_arfile): lines = _get_dumpH(_arfile) member = _get_member(_stem, name, lines) if member != None: shlib = "lib%s.a(%s)" % (_stem, member) return shlib # C) look for a .so FILE based on the name requested OR based on _stem if envpath is None: if _aixABI() == 64: _os.environ['LIBPATH'] = "%s/lib64" % _sys.prefix paths = _getExecLibPath_aix() # same as above! # else: # _os.environ['LIBPATH'] = "%s/lib" % _sys.prefix # paths = _getExecLibPath_aix() for dir in paths.split(":"): if dir == "/lib": continue shlib = _os.path.join(dir, "%s" % name) if _os.path.exists(shlib): return shlib # Try for a generic match shlib = _os.path.join(dir, "lib%s.so" % _stem) if _os.path.exists(shlib): return shlib # if we are here, we have not found anything plausible return None