U ,a1@sdZdZddlZddlmZmZddlmZddlm Z m Z ddl m Z m Z mZe e dZdd lmZd d Zd d ZddZddZddZddZddZddZddZddZddZd d!ZdS)"a Lib/ctypes.util.find_library() support for AIX Similar approach as done for Darwin support by using separate files but unlike Darwin - no extension such as ctypes.macholib.* dlopen() is an interface to AIX initAndLoad() - primary documentation at: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/dlopen.htm https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/load.htm AIX supports two styles for dlopen(): svr4 (System V Release 4) which is common on posix platforms, but also a BSD style - aka SVR3. From AIX 5.3 Difference Addendum (December 2004) 2.9 SVR4 linking affinity Nowadays, there are two major object file formats used by the operating systems: XCOFF: The COFF enhanced by IBM and others. The original COFF (Common Object File Format) was the base of SVR3 and BSD 4.2 systems. ELF: Executable and Linking Format that was developed by AT&T and is a base for SVR4 UNIX. While the shared library content is identical on AIX - one is located as a filepath name (svr4 style) and the other is located as a member of an archive (and the archive is located as a filepath name). The key difference arises when supporting multiple abi formats (i.e., 32 and 64 bit). For svr4 either only one ABI is supported, or there are two directories, or there are different file names. The most common solution for multiple ABI is multiple directories. For the XCOFF (aka AIX) style - one directory (one archive file) is sufficient as multiple shared libraries can be in the archive - even sharing the same name. In documentation the archive is also referred to as the "base" and the shared library object is referred to as the "member". For dlopen() on AIX (read initAndLoad()) the calls are similar. Default activity occurs when no path information is provided. When path information is provided dlopen() does not search any other directories. For SVR4 - the shared library name is the name of the file expected: libFOO.so For AIX - the shared library is expressed as base(member). The search is for the base (e.g., libFOO.a) and once the base is found the shared library - identified by member (e.g., libFOO.so, or shr.o) is located and loaded. The mode bit RTLD_MEMBER tells initAndLoad() that it needs to use the AIX (SVR3) naming style. z%Michael Felt N)environpath) executable)c_void_psizeof)PopenPIPEDEVNULL)maxsizecsfdd}tt||dS)NcsL|}g}z|r*|dt|qWntk r@YnX|pJtgS)Nr)splitinsertintpop ValueErrorr )ZlibnamepartsZnumssep0/opt/alt/python38/lib64/python3.8/ctypes/_aix.py _num_version>s z#_last_version.._num_version)key)maxreversed)Zlibnamesrrrrr _last_version=s rcCs:d}|jD]*}|dr|}q d|kr |dSq dS)N)/z./z../ZINDEX )stdout startswithrstrip)p ld_headerlinerrr get_ld_headerJs  r#cCs0g}|jD] }td|r&||q q,q |S)Nz[0-9])rrematchappend)r infor"rrrget_ld_header_infoTs    r(cCs\g}tddtd|gdttd}t|}|rF||t|fq"qFq"|j| |S)z Parse the header of the loader section of executable and archives This function calls /usr/bin/dump -H as a subprocess and returns a list of (ld_header, ld_header_info) tuples. z /usr/bin/dumpz-Xz-HT)Zuniversal_newlinesrstderr) rAIX_ABIrr r#r&r(rclosewait)fileZ ldr_headersr r!rrrget_ld_headersas  r.cCs6g}|D](\}}d|kr|||ddq|S)z extract the shareable objects from ld_headers character "[" is used to strip off the path information. Note: the "[" and "]" characters that are part of dump -H output are not removed here. [)r&index)Z ld_headersZsharedr"_rrr get_sharedys  r3csJddttdfdd|D}t|dkrB|ddSdSdS)zy Must be only one match, otherwise result is None. When there is a match, strip leading "[" and trailing "]" z\[(z)\]Nc3s|]}t|VqdS)N)r$search).0r"exprrr sz get_one_match..r)listfilterlengroup)r7linesZmatchesrr6r get_one_matchs   r?cCsJtdkr d}t||}|rF|Sn&dD] }tt||}|r$|Sq$dS)z This routine provides historical aka legacy naming schemes started in AIX4 shared library support for library members names. e.g., in /usr/lib/libc.a the member name shr.o for 32-bit binary and shr_64.o for 64-bit binary. @z shr4?_?64\.o)zshr.ozshr4.oN)r*r?r$escape)membersr7membernamerrr get_legacys  rEcCsfd|dd|dg}|D]D}g}|D]$}t||}|r(||dq(|rt|dSqdS)a Sort list of members and return highest numbered version - if it exists. This function is called when an unversioned libFOO.a(libFOO.so) has not been found. Versioning for the member name is expected to follow GNU LIBTOOL conventions: the highest version (x, then X.y, then X.Y.z) * find [libFoo.so.X] * find [libFoo.so.X.Y] * find [libFoo.so.X.Y.Z] Before the GNU convention became the standard scheme regardless of binary size AIX packagers used GNU convention "as-is" for 32-bit archive members but used an "distinguishing" name for 64-bit members. This scheme inserted either 64 or _64 between libFOO and .so - generally libFOO_64.so, but occasionally libFOO64.so libz\.so\.[0-9]+[0-9.]*z_?64\.so\.[0-9]+[0-9.]*r.N)r$r4r&r=r)rDrBZexprsr7Zversionsr"mrrr get_versions   rIcCsbd|d}t||}|r|Stdkrr"rrrr get_libpathss     rOcCsp|D]f}|dkrqd|d}t||}t|rtt|}tt||}|dkrd||fSdSqdS)a paths is a list of directories to search for an archive. name is the abbreviated name given to find_library(). Process: search "paths" for archive, and if an archive is found return the result of get_member(). If an archive is not found then return None /librFz.aN)NN)rjoinexistsr3r.rJr$rA)pathsrDdirbasearchiverBrCrrr find_shared s     rWcCsnt}t||\}}|dkr,|d|dSd|d}|D],}|dkrJqs(.      &