# Lib/ctype support for cdll interface to dlopen() for AIX # Author: M Felt, aixtools.net, May 2016 # # dlopen() is an interface to AIX initAndLoad() - these are documented at: # https://www.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/dlopen.htm?lang=en # https://www.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/load.htm?lang=en # # Some basics # when given an argument such as -lFOO to the AIX loader and a symbol must be resolved # the AIX ld searches LIBPATH for an archive named libFOO.a. When found, it then searches archive members # for a member that resolves the symbol. # When an archive (.a) is not found, the search starts again, but looking for a file named libFOO.so # # when a full or partial path information is given dlopen() does not search LIBPATH - only the path specified # # ctypes.aix.find_library() knows how to search archives for members, just as GNU ldconfig -p provides a list # of paths to installed shared libraries # # It is not intended that find_library("libFOO.so") be given as an argument. # That may be suitable for CDLL(), but not for find_library() # Much code is written as: CDLL(find_library("libFOO")) # or : CDLL((find_library("FOO")) # # examples: # aix.find_library("libiconv") => libiconv.a(shr4_64.o) # aix.find_library("libintl") => libintl.a(libintl.so.1) or libintl.a(libintl.so.8) depending on the # version of GNU gettext installed # when packaging python - a packager (for AIX) may want to include "export LDFLAGS=-L${prefix}/lib" # so that dlopen() will look in ${prefix}/lib as well /usr/lib, etc.. # # 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 # # find_library() returns the archive(member) string needed by dlopen() to open a member whenever possible # only returning a file - when available - if the archive search fails # directory path is returned only when directory path information was supplied # import re as _re, os as _os, sys as _sys # tell machine size to find the correct member in an archive def _aixABI(): if (_sys.maxsize < 2**32): return 32 else: return 64 # execute and pre-scan output of /usr/bin/dump ... -H ... def _get_dumpH(_exe_so_or_a, p=None, lines=None): import subprocess as _subprocess p = _subprocess.Popen(["/usr/bin/dump", "-X%s"%_aixABI(), "-H", _exe_so_or_a], universal_newlines=True, stdout=_subprocess.PIPE) lines=[line.strip() for line in p.stdout.readlines() if line.strip()] p.stdout.close() p.wait() return lines # process dumpH output and return only members with loader sections def _get_shared(input, list=None, cpy=None, idx=0): cpy=input list=[] for line in input: idx = idx + 1 if line.startswith("/"): next = cpy.pop(idx) if not "not" in next: list.append(line[line.find("["):line.rfind(":")]) return(list) # find the singular match, if more - undefined, and None is None def _get_match(expr, lines, line=None, member=None): matches = [m.group(0) for line in lines for m in [_re.search(expr, line)] if m] if len(matches) == 1: match=matches[0] member = match[match.find("[")+1:match.find("]")] return member else: return None # sometimes a generic name gets 64 appended to it, and AIX 64-bit # archive members have different legacy names than 32-bit ones def _get_member64(stem, members): # Recall that member names are in square brackets [] # an old convention - insert _64 between libFOO and .so for expr in [ '\[lib%s_*64\.so\]' % stem, '\[lib%s.so.[0-9]+.*\]' % stem, '\[lib%s_*64\.o\]' % stem, '\[shr4_64.o\]', '\[shr4*_*64.o\]']: member = _get_match(expr, members) if member: return member return None # Get the most recent/highest numbered version - if it exists def _get_version(stem, members): def _num_version(libname): # "libxyz.so.CURRENT.REVISION.AGE" => [ CURRENT, REVISION, AGE ] parts = libname.split(".") nums = [] try: while parts: nums.insert(0, int(parts.pop())) except ValueError: pass return nums or [ _sys.maxsize ] expr = 'lib%s[_64]*.so.[0-9]+[0-9\.]*' % stem versions = [m.group(0) for line in members for m in [_re.search(expr, line)] if m] if versions: versions.sort(key=_num_version) return versions[-1] return None # return an archive member matching name # (versioned) .so members have priority over legacy AIX member name # # member names in in the dumpH output are in square brackets # These get stripped by _get_match() and regular parenthesis are added by the caller def _get_member(stem, name, members): # look first for an exact match, then a generic match for expr in [ '\[%s\]' % name, '\[lib%s\.so\]' % stem]: member = _get_match(expr, members) if member: return member # if name ends in a digit, a specific version is wanted, # if not found above as an exact match do not look for # a different version if _re.findall("\d$", name): return None else: member = _get_version(stem, members) if member: return member elif _aixABI() == 64: return(_get_member64(stem, members)) # 32-bit legacy names - shr4.o and shr.o else: for expr in [ '\[shr4.o\]', '\[shr.o\]']: member = _get_match(expr, members) if member: return member return None def _getExecLibPath_aix(libpaths=None, x=None, lines=None, line=None, skipping=None): # if LIBPATH is defined, add it to the paths to search # FIXME: maybe LD_LIBRARY_PATH should be checked as well 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 # A) exact match (relative/full-path name) # B) normal dlopen() processing, member in a archive # C) OLD behavior, "gnu"/"posix" like - find a file in a directory 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 path.filename, return ASIS # This is for people who are trying to match a file as "./libFOO.so" # or using a fixed (fullpath) # FIXME: include a check for a "/" in the name before looking at the file/path # FIXME: this will only work -as expected - for a .so argument # FIXME: may want a additional test for .a ending, and if true AND path.exists # FIXME: comoute stem and look for suitable member name 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 # FIXME: if name endswith(".so") or contains(".so.") then: # print message to syslog DEBUG _stem = "%s." % name if _stem.find("lib") < 0: _stem = _stem[0:_stem.find(".")] else: _stem = _stem[3:_stem.find(".")] paths = _getExecLibPath_aix() for dir in paths.split(":"): # /lib is a symbolic link to /usr/lib, skip it 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): members = _get_shared(_get_dumpH(_arfile)) member = _get_member(_stem, name, members) if member != None: shlib = "lib%s.a(%s)" % (_stem, member) return shlib ##### C) look for a .so FILE based on the request for dir in paths.split(":"): # /lib is a symbolic link to /usr/lib, skip it if dir == "/lib": continue # check for an exact match shlib = _os.path.join(dir, "%s" % name) if _os.path.exists(shlib): return shlib # check for a generic match, note - no libtool versioning search 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