HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/relenv/__pycache__/relocate.cpython-310.pyc
o

;j@�@sbUdZddlmZddlZddlZddlZddlZddl	Z
ddlZe�
e�ZeZeZe
Z	eZgd�Zgd�ZdZdZdZdad	ed
<dad	ed<dEdd�ZdEdd�ZdFdd�ZdFdd�ZdGdd�ZdHdd�ZdId!d"�ZdJd#d$�ZdKd'd(�Z dLd+d,�Z!dFd-d.�Z"	/dMdNd3d4�Z#	dOdPd9d:�Z$		/	;	<dQdRd@dA�Z%edBkr�ddlZe&edC�s�e'dD��e%ej(�dSdS)SzO
A script to ensure the proper rpaths are in place for the relenv environment.
�)�annotationsN)�is_macho�is_elf�
parse_otool_l�parse_readelf_d�parse_macho�parse_rpath�handle_macho�	is_in_dir�
handle_elf�patch_rpath�main�os�shutil�
subprocess)zlinux-vdso.so.1z	libc.so.6�
librt.so.1z	libm.so.6z
libmd.so.0zlibpthread.so.0z
libdl.so.2zlibmemusage.sozlibnsl.so.1zlibnss_compat.so.2zlibnss_db.so.2zlibnss_dns.so.2zlibnss_files.so.2zlibnss_hesiod.so.2zlibpcprofile.so.2zlibresolv.so.2rzlibthread_db.so.1zlibutil.so.1zlibutil.so.2z
libgcc_s.so.2z
libgcc_s.so.1�LC_ID_DYLIB�
LC_LOAD_DYLIB�LC_RPATHz
str | None�_READELF_BINARY�_PATCHELF_BINARY�return�strcCs�tdurtStjdkrFz.ddlm}m}|�}|r0|�}|d|�d�}|��r5t|�atWSWdatSWdatStyEYdatSwdatS)a9
    Get the path to readelf binary, preferring toolchain version.

    Returns the cached value if already computed. On Linux, prefers the
    toolchain's readelf over the system version. Falls back to "readelf"
    from PATH if toolchain is unavailable.

    :return: Path to readelf binary
    :rtype: str
    N�linuxr)�
get_toolchain�get_triplet�binz-readelf�readelf)	r�sys�platform�
relenv.commonrr�existsr�	Exception)rr�	toolchain�tripletZtoolchain_readelf�r%�C/opt/saltstack/salt/lib/python3.10/site-packages/relenv/relocate.py�_get_readelf_binaryQs2
����r'cCs�tdurtStjdkr>z&ddlm}|�}|r(|dd}|��r-t|�atWSWdatSWdatSty=YdatSwdatS)a=
    Get the path to patchelf binary, preferring toolchain version.

    Returns the cached value if already computed. On Linux, prefers the
    toolchain's patchelf over the system version. Falls back to "patchelf"
    from PATH if toolchain is unavailable.

    :return: Path to patchelf binary
    :rtype: str
    Nrr)rr�patchelf)rrrr rr!rr")rr#Ztoolchain_patchelfr%r%r&�_get_patchelf_binaryus0
�
���r)�path�str | os.PathLike[str]�boolcCsBt|d��}|�d�}Wd�|dvS1swY|dvS)z�
    Determines whether the given file is a macho file.

    :param path: The path to the file to check
    :type path: str

    :return: Whether the file is a macho file
    :rtype: bool
    �rb�N)s���open�read�r*�fp�magicr%r%r&r�s

��rcCsBt|d��}|�d�}Wd�|dkS1swY|dkS)z�
    Determines whether the given file is an ELF file.

    :param path: The path to the file to check
    :type path: str

    :return: Whether the file is an ELF file
    :rtype: bool
    r-r.NsELFr/r2r%r%r&r�s

��r�stdout�dict[str, list[str]]cCsd}d}d}i}dd�|�d�D�D][}|sq|��ddkrGd}|dur6|dur6|�|g��|�d}d}|��d}|ttd	fvrG|}d
}|rm|��}|ddkr]t|�dkr]|d}|dd
krmt|�dkrm|d}q|r�|dur�|dur�|�|g��|�|S)z�
    Parse the output of ``otool -l <path>``.

    :param stdout: The output of the ``otool -l <path>`` command
    :type stdout: str

    :return: The parsed relevant output with command keys and path values
    :rtype: dict
    FNcS�g|]}|���qSr%��strip)�.0�xr%r%r&�
<listcomp>��z!parse_otool_l.<locals>.<listcomp>�
r�cmd���rT�name�r*)�split�
setdefault�appendrr�len)r5Zin_cmdr?rA�data�lineZcommand�partsr%r%r&r�s6
�r�	list[str]cCsJ|��D]}|�d�dkrq|�dd�d���d��d��d�SgS)z�
    Parse the output of ``readelf -d <path>``.

    :param stdout: The output of the ``readelf -d <path>`` command
    :type stdout: str

    :return: The RPATH values
    :rtype: list
    �PATHr@�:rB�[�])�
splitlines�findrCr9)r5rHr%r%r&r�s

*r�dict[str, list[str]] | NonecCs>tjdd|gtjtjd�}|j��}|�d�dkrdSt|�S)z�
    Run ``otool -l <path>`` and return its parsed output.

    :param path: The path to the file
    :type path: str

    :return: The parsed relevant RPATH content, or None if it isn't an object file
    :rtype: dict or None
    �otoolz-l��stderrr5zis not an object filer@N)r�run�PIPEr5�decoderPr)r*�procr5r%r%r&r�s


rcCs.t�}tj|d|gtjtjd�}t|j���S)z�
    Run ``readelf -d <path>`` and return its parsed output.

    :param path: The path to the file
    :type path: str

    :return: The RPATH's found.
    :rtype: list
    z-d)r5rT)r'rrUrVrr5rW)r*rrXr%r%r&rs
r�root_dir�
rpath_onlycCs*t�|�}t|�}t|�}t�d||�|sdSt|vr�|tD]r}|�d�r.t�d|�q tj	�
|�r�t�|���}|tj	�|�}tj	�
|�sf|rSt�
d||�q t�||�t�||�t�d||�t�d||�t�d�tj	�||��j�}	d	d
|t|	�|g}
t�|
�t�d||	|�q |S)a�
    Ensure the given macho file has the correct rpath and is in th correct location.

    :param path: The path to a macho file
    :type path: str
    :param root_dir: The directory the file needs to reside under
    :type root_dir: str
    :param rpath_only: If true, only ensure the correct rpaths are present and don't copy the file
    :type rpath_only: bool

    :return: The information from ``parse_macho`` on the macho file.
    zProcessing file %s %rN�@zSkipping dynamic load: %s�(In `rpath_only mode` but %s is not in %szCopied %s to %szUse %s to %sz@loader_pathZinstall_name_toolz-changezChanged %s to %s in %s)�pathlib�Pathrr�log�infor�
startswithrr*r!�resolve�basename�warningr�copy�copymode�relpath�parentrrU)r*rYrZZpath_obj�objZpath_strr;�
target_dir�y�zr?r%r%r&r	s8


�r	�filepath�	directorycCs tj�|��tj�|�tj�S)a5
    Determines whether a file is contained within a directory.

    :param filepath: The path to the file to check
    :type filepath: str
    :param directory: The directory to check within
    :type directory: str

    :return: Whether the file is contained within the given directory
    :rtype: bool
    )rr*�realpathra�sep)rmrnr%r%r&r
Bs r
cCs`t|�}|sdSt�d||�t�}tj|d|gtjtjd�}|jr.t�d||j	�
��dSdS)z�
    Remove the rpath from a given ELF file.

    :param path: The path to an ELF file
    :type path: str

    :return: True if successful, False otherwise
    :rtype: bool
    TzRemove RPATH from %s (was: %s)z--remove-rpathrSz"Failed to remove RPATH from %s: %sF)rr_r`r)rrUrV�
returncode�errorrTrW)r*�	old_rpathr(rXr%r%r&�remove_rpathQs
�rtT�	new_rpath�
only_relative�
str | boolcCs|t|�}|r
dd�|D�}||vr9d�|g|�}t�d||�t�}tj|dd||gtjtjd�}|jr7dS|Sd�|�S)	a�
    Patch the rpath of a given ELF file.

    :param path: The path to an ELF file
    :type path: str
    :param new_rpath: The new rpath to add
    :type new_rpath: str
    :param only_relative: Whether or not to remove non-relative rpaths, defaults to True
    :type only_relative: bool, optional

    :return: The new rpath, or False if patching failed
    :rtype: str or bool
    cSsg|]	}|�d�r|�qS)�$ORIGIN)ra)r:Zrpathr%r%r&r<�szpatch_rpath.<locals>.<listcomp>rLzSet RPATH=%s %sz
--force-rpathz--set-rpathrSF)	r�joinr_r`r)rrUrVrq)r*rurvrsZ
patched_rpathr(rXr%r%r&rns �
r�libs�root�str | os.PathLike[str] | None�NonecCs�|dur|}tjd|gtjtjd�}d}|j����D]�}|�d�dkr+t�d|�qdd	�|�	dd
�D�\}}|dkrDt�
d||�q|�d
d
�d��}	t
j�|	�}
|tvr_t�d|�qt|	|�rmd}t�d|	�qt
j�||
�}t
j�|�r�t�d|�q|r�t�
d|	|�qt�d|	|�t�|	|�t�|	|�d}q|r�t
j�|t
j�|��}|dkr�d}
n	tt�d�|�}
t�d||
�t||
�dSt|�dS)a�
    Handle the parsing and pathcing of an ELF file.

    :param path: The path of the ELF file
    :type path: str
    :param libs: The libs directory
    :type libs: str
    :param rpath_only: If true, only ensure the correct rpaths are present and don't copy the file
    :type rpath_only: bool
    :param root: The directory to ensure the file is under, defaults to None
    :type root: str, optional
    NZlddrSFz=>r@zSkip ldd output line: %scSr7r%r8)r:�_r%r%r&r<�r=zhandle_elf.<locals>.<listcomp>rBz	not foundz(Unable to find library %s linked from %s� rzSkipping glibc lib %sTz&File already within root directory: %szRelocated library exists: %sr\z
Copy %s to %s�.rxzAdjust rpath of %s to %s)rrUrVr5rWrOrPr_�debugrCrd�rsplitr9rr*rc�LIBCLIBSr
r`ryr!rrerfrg�dirnamerr]r^rrt)r*rzrZr{rXZneeds_rpathrHZlib_nameZ
location_infoZ
linked_libZlib_basenameZrelocated_pathZrelpartrgr%r%r&r�sL
r�DEBUG�<stdout>�libs_dir�	log_level�
log_file_namecCs(t�|���}|dkrtj|d|dd�ntj|dd�tt�|����}|dur/t�|d�}tt�|����}i}d}|r�d	}t�	|�D]H\}}	}
|
D]@}tj
�||�}||vrZqLt�
d
|�t|�r{t�d|�t|||�}
|
durz|
||<d}qLt|�r�t�d|�t||||�qLqE|s>dSdS)
a
    The entrypoint into the relocate script.

    :param root: The root directory to operate traverse for files to be patched
    :type root: str
    :param libs_dir: The directory to place the libraries in, defaults to None
    :type libs_dir: str, optional
    :param rpath_only: If true, only ensure the correct rpaths are present and don't copy the file, defaults to True
    :type rpath_only: bool, optional
    :param log_level: The level to log at, defaults to "INFO"
    :type log_level: str, optional
    r�z%(asctime)s %(message)s�w)�level�format�filename�filemode)r�r�N�libTFzChecking %szFound Mach-O %szFound ELF %s)�logging�getLevelName�upper�basicConfigrr]r^rbr�walkr*ryr_r�rr`r	rr)r{r�rZr�r�r�rYZ	processed�found�dirs�files�filer*r~r%r%r&r
�sN������r
�__main__�RELENVzNot in a relenv environment)rr)r*r+rr,)r5rrr6)r5rrrJ)r*r+rrQ)r*r+rrJ)r*r+rYr+rZr,rrQ)rmr+rnr+rr,)T)r*r+rurrvr,rrw)N)
r*r+rzr+rZr,r{r|rr})NTr�r�)r{r+r�r|rZr,r�rr�rrr}))�__doc__�
__future__rr�r�_osr]r�_shutilr�_subprocessr�_sys�	getLogger�__name__r_�__all__r�rrrr�__annotations__rr'r)rrrrrrr	r
rtrrr
�hasattr�RuntimeErrorr�r%r%r%r&�<module>s\


$
#


*



/
 �,�L�:
�