
    d                          d dl mZ d dlmZmZ ddlmZmZ  G d de      Z	 G d de      Z
 G d	 d
e      Z G d de      Z G d de      Z G d de      Zy)   )CString)struct_parse
elf_assert   )SectionSymbolc                       e Zd ZdZddZd Zy)Versiona[   Version object - representing a version definition or dependency
        entry from a "Version Needed" or a "Version Dependency" table section.

        This kind of entry contains a pointer to an array of auxiliary entries
        that store the information about version names or dependencies.
        These entries are not stored in this object and should be accessed
        through the appropriate method of a section object which will return
        an iterator of VersionAuxiliary objects.

        Similarly to Section objects, allows dictionary-like access to
        verdef/verneed entry
    Nc                      || _         || _        y Nentrynameselfr   r   s      :/usr/lib/python3/dist-packages/elftools/elf/gnuversions.py__init__zVersion.__init__       
	    c                      | j                   |   S )z- Implement dict-like access to entry
        r   r   r   s     r   __getitem__zVersion.__getitem__        zz$r   r   __name__
__module____qualname____doc__r   r    r   r   r
   r
      s     r   r
   c                       e Zd ZdZd Zd Zy)VersionAuxiliaryz Version Auxiliary object - representing an auxiliary entry of a version
        definition or dependency entry

        Similarly to Section objects, allows dictionary-like access to the
        verdaux/vernaux entry
    c                      || _         || _        y r   r   r   s      r   r   zVersionAuxiliary.__init__,   r   r   c                      | j                   |   S )z/ Implement dict-like access to entries
        r   r   s     r   r   zVersionAuxiliary.__getitem__0   r   r   Nr   r    r   r   r"   r"   %   s     r   r"   c                   <     e Zd ZdZ fdZd ZddZd Zd Z xZ	S )GNUVersionSectionz{ Common ancestor class for ELF SUNW|GNU Version Needed/Dependency
        sections class which contains shareable code
    c                 h    t         t        |   |||       || _        || _        || _        || _        y r   )superr&   r   stringtablefield_prefixversion_structversion_auxiliaries_struct)	r   headerr   elffiler)   r*   r+   r,   	__class__s	           r   r   zGNUVersionSection.__init__;   s7    /gF&(,*D'r   c                     | d   S )z2 Number of version entries in the section
        sh_infor    r   s    r   num_versionszGNUVersionSection.num_versionsC   s     Ir   c                 2    |rdnd}| j                   |z   |z   S )zZ Return the real field's name of version or a version auxiliary
            entry
        a__)r*   )r   r   	auxiliarymiddles       r   _field_namezGNUVersionSection._field_nameH   s#     #  6)D00r   c              #   (  K   | j                  dd      }| j                  dd      }t        |      D ]Z  }t        | j                  | j                  |      }| j
                  j                  ||         }t        ||      }| |||   z  }\ yw)z8 Yield all auxiliary entries of a version entry
        r   T)r7   next
stream_posN)r9   ranger   r,   streamr)   
get_stringr"   )	r   entry_offsetcount
name_field
next_fieldr6   r   r   version_auxs	            r   _iter_version_auxiliariesz+GNUVersionSection._iter_version_auxiliariesO   s      %%f%=
%%f%=
u 
	.A 77#/1E
 ##..uZ/@AD*5$7KE*--L
	.s   BBc           
   #     K   | j                  d      }| j                  d      }| j                  d      }| d   }t        | j                               D ]{  }t        | j                  | j
                  |      }t        ||   dkD  d|dt        |             t        |      }|||   z   }| j                  |||         }	||	f |||   z  }} y	w)
z Yield all the version entries in the section
            Each time it returns the main version structure
            and an iterator to walk through its auxiliaries entries
        auxcntr;   	sh_offsetr<       z.Expected number of version auxiliary entries (z,) to be > 0for the following version entry: N)
r9   r>   r3   r   r+   r?   r   strr
   rF   )
r   	aux_fieldcount_fieldrD   rA   r6   r   versionaux_entries_offsetversion_auxiliaries_iters
             r   iter_versionszGNUVersionSection.iter_versionsa   s     
 $$U+	&&u-%%f-
K(t((*+ 	.A ##')E
 u[)A-  U-.
 enG!-i0@!@'+'E'E&k(:(<$ 333E*--L%	.s   CC)F)
r   r   r   r   r   r3   r9   rF   rR   __classcell__r/   s   @r   r&   r&   6   s"    E
1.$.r   r&   c                   8     e Zd ZdZ fdZd Z fdZd Z xZS )GNUVerNeedSectionz ELF SUNW or GNU Version Needed table section.
        Has an associated StringTableSection that's passed in the constructor.
    c           	          t         t        |   ||||d|j                  j                  |j                  j
                         d | _        y )Nvn)r(   rV   r   structsElf_VerneedElf_Vernaux_has_indexesr   r-   r   r.   r)   r/   s        r   r   zGNUVerNeedSection.__init__   sB    /g{D++W__-H-H	J !r   c                     | j                   4d| _         | j                         D ]  \  }}|D ]  }|d   s	d| _            | j                   S )z Return True if at least one version definition entry has an index
            that is stored in the vna_other field.
            This information is used for symbol versioning
        F	vna_otherT)r\   rR   )r   r6   vernaux_itervernauxs       r   has_indexeszGNUVerNeedSection.has_indexes   sd    
 $ %D#'#5#5#7 <+ G{+,0)    r   c              #      K   t         t        |          D ].  \  }}| j                  j	                  |d         |_        ||f 0 y w)Nvn_file)r(   rV   rR   r)   r@   r   )r   verneedra   r/   s      r   rR   zGNUVerNeedSection.iter_versions   sN      %&7 L N 	#GW++66wy7IJGL7""	#s   AAc                 b    | j                         D ]  \  }}|D ]  }|d   |k(  s||fc c S   y)z Get the version information located at index #n in the table
            Return boths the verneed structure and the vernaux structure
            that contains the name of the version
        r_   NrR   )r   indexre   r`   ra   s        r   get_versionzGNUVerNeedSection.get_version   sM    
 &*%7%7%9 	,!G\' ,;'50"G++,	,
 r   )	r   r   r   r   r   rb   rR   ri   rS   rT   s   @r   rV   rV      s    !!#

r   rV   c                   (     e Zd ZdZ fdZd Z xZS )GNUVerDefSectionz ELF SUNW or GNU Version Definition table section.
        Has an associated StringTableSection that's passed in the constructor.
    c           	          t         t        |   ||||d|j                  j                  |j                  j
                         y )Nvd)r(   rk   r   rY   
Elf_VerdefElf_Verdauxr]   s        r   r   zGNUVerDefSection.__init__   s8    .g{D**GOO,G,G	Ir   c                 P    | j                         D ]  \  }}|d   |k(  s||fc S  y)z Get the version information located at index #n in the table
            Return boths the verdef structure and an iterator to retrieve
            both the version names and dependencies in the form of
            verdaux entries
        vd_ndxNrg   )r   rh   verdefverdaux_iters       r   ri   zGNUVerDefSection.get_version   s>     %)$6$6$8 	, FLh5(|++	, r   )r   r   r   r   r   ri   rS   rT   s   @r   rk   rk      s    I

r   rk   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )GNUVerSymSectionzz ELF SUNW or GNU Versym table section.
        Has an associated SymbolTableSection that's passed in the constructor.
    c                 >    t         t        |   |||       || _        y r   )r(   ru   r   symboltable)r   r-   r   r.   rw   r/   s        r   r   zGNUVerSymSection.__init__   s    .vtWE&r   c                     | d   | d   z  S )z( Number of symbols in the table
        sh_size
sh_entsizer    r2   s    r   num_symbolszGNUVerSymSection.num_symbols   s     I$|"444r   c                     | d   || d   z  z   }t        | j                  j                  | j                  |      }| j                  j                  |      j                  }t        ||      S )z Get the symbol at index #n from the table (Symbol object)
            It begins at 1 and not 0 since the first entry is used to
            store the current version of the syminfo table
        rJ   rz   r<   )r   rY   
Elf_Versymr?   rw   
get_symbolr   r   )r   nrA   r   r   s        r   r~   zGNUVerSymSection.get_symbol   si     K(1tL/A+AALL##KK#%
 **1-22eT""r   c              #   n   K   t        | j                               D ]  }| j                  |        yw)z, Yield all the symbols in the table
        N)r>   r{   r~   )r   is     r   iter_symbolszGNUVerSymSection.iter_symbols   s4      t'')* 	%A//!$$	%s   35)	r   r   r   r   r   r{   r~   r   rS   rT   s   @r   ru   ru      s    '5
#%r   ru   N)	constructr   common.utilsr   r   sectionsr   r   objectr
   r"   r&   rV   rk   ru   r    r   r   <module>r      s^      3 % f  . v  "G. G.T() (V( , %w  %r   