Ë
    Öùd6  ã                   óî   — d dl Z d dlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ d	d
lmZ d	dlmZmZmZ  G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ dee«      Z G d„ dee«      Zy)é    N)Údefaultdicté   )ÚELFHashTableÚGNUHashTable)ÚSectionÚSymbol)Ú
ENUM_D_TAG)ÚSegment)ÚRelocationTableé   )ÚELFError)Ú
elf_assertÚstruct_parseÚparse_cstring_from_streamc                   ó   — e Zd ZdZd„ Zd„ Zy)Ú_DynamicStringTablezŠ Bare string table based on values found via ELF dynamic tags and
        loadable segments only.  Good enough for get_string() only.
    c                 ó    — || _         || _        y ©N)Ú_streamÚ_table_offset)ÚselfÚstreamÚtable_offsets      ú6/usr/lib/python3/dist-packages/elftools/elf/dynamic.pyÚ__init__z_DynamicStringTable.__init__   s   € ØˆŒØ)ˆÕó    c                 ór   — t        | j                  | j                  |z   «      }|r|j                  d«      S dS )zI Get the string stored at the given offset in this string table.
        zutf-8Ú )r   r   r   Údecode)r   ÚoffsetÚss      r   Ú
get_stringz_DynamicStringTable.get_string   s5   € ô & d§l¡l°D×4FÑ4FÈÑ4OÓPˆÙ$%ˆq�x‰x˜Ó Ð-¨2Ð-r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   © r   r   r   r      s   „ ñò*ó.r   r   c                   ó<   — e Zd ZdZ eg d¢«      Zd„ Zd„ Zd„ Zd„ Z	y)Ú
DynamicTaga“   Dynamic Tag object - representing a single dynamic tag entry from a
        dynamic section.

        Allows dictionary-like access to the dynamic structure. For special
        tags (those listed in the _HANDLED_TAGS set below), creates additional
        attributes for convenience. For example, .soname will contain the actual
        value of DT_SONAME (fetched from the dynamic symbol table).
    )Ú	DT_NEEDEDÚDT_RPATHÚ
DT_RUNPATHÚ	DT_SONAMEÚDT_SUNW_FILTERc                 óô   — |€t        d«      ‚|| _        |j                  | j                  v rLt	        | |j                  dd  j                  «       |j                  | j                  j                  «      «       y y )Nz(Creating DynamicTag without string tableé   )r   ÚentryÚd_tagÚ_HANDLED_TAGSÚsetattrÚlowerr"   Úd_val)r   r1   Ústringtables      r   r   zDynamicTag.__init__1   si   € ØÐÜÐEÓFÐFØˆŒ
Ø�;‰;˜$×,Ñ,Ñ,Ü�D˜%Ÿ+™+ a b˜/×/Ñ/Ó1Ø×*Ñ*¨4¯:©:×+;Ñ+;Ó<õ>ð -r   c                 ó    — | j                   |   S )z/ Implement dict-like access to entries
        )r1   )r   Únames     r   Ú__getitem__zDynamicTag.__getitem__9   s   € ð �z‰z˜$ÑÐr   c                 óP   — d| j                   j                  ›d| j                   ›d�S )Nú<DynamicTag (z): ú>)r1   r2   )r   s    r   Ú__repr__zDynamicTag.__repr__>   s   � Ø*.¯*©*×*:Ó*:¸D¿J»JÐGÐGr   c                 ó  — | j                   j                  | j                  v r5dt        | | j                   j                  dd  j	                  «       «      z  }nd| j                   j
                  z  }d| j                   j                  ›d|›d�S )Nz"%s"r0   z%#xr<   z) r=   )r1   r2   r3   Úgetattrr5   Úd_ptr)r   r!   s     r   Ú__str__zDynamicTag.__str__A   so   € Ø�:‰:×Ñ˜t×1Ñ1Ñ1Øœ  t§z¡z×'7Ñ'7¸¸Ð';×'AÑ'AÓ'CÓDÑD‰Aà˜Ÿ
™
×(Ñ(Ñ(ˆAøØ)-¯©×)9Ó)9º1Ð=Ð=r   N)
r#   r$   r%   r&   Ú	frozensetr3   r   r:   r>   rB   r'   r   r   r)   r)   $   s-   „ ññ ò	ó€Mò>ò ò
Hó>r   r)   c                   óJ   — e Zd ZdZd„ Zd„ Zd„ Zdd„Zdd„Zd„ Z	d	„ Z
d
„ Zd„ Zy)ÚDynamiczA Shared functionality between dynamic sections and segments.
    c                 óÔ   — || _         |j                  | _        || _        |sdnd| _        || _        | j                  j                  j                  «       | _        || _	        || _
        y)aÿ  
        stream:
            The file-like object from which to load data

        elffile:
            The parent elffile object

        stringtable:
            A stringtable reference to use for parsing string references in
            entries

        position:
            The file offset of the dynamic segment/section

        empty:
            Whether this is a degenerate case with zero entries. Normally, every
            dynamic table will have at least one entry, the DT_NULL terminator.
        éÿÿÿÿr   N)ÚelffileÚstructsÚ
elfstructsr   Ú	_num_tagsÚ_offsetÚElf_DynÚsizeofÚ_tagsizeÚ_emptyÚ_stringtable)r   r   rH   r7   ÚpositionÚemptys         r   r   zDynamic.__init__L   sZ   € ð& ˆŒØ!Ÿ/™/ˆŒØˆŒÙ#(™¨aˆŒØˆŒØŸ™×/Ñ/×6Ñ6Ó8ˆŒØˆŒð (ˆÕr   c                 óš   — d}| j                  |¬«      D ]  }|d   } n d}|r%t        | j                  j                  |«      d«      }||fS )zH Return the virtual address and file offset of a dynamic table.
        N©ÚtyperA   )Ú
_iter_tagsÚnextrH   Úaddress_offsets)r   Útag_nameÚptrÚtagr    s        r   Úget_table_offsetzDynamic.get_table_offsetj   s^   € ð ˆØ—?‘?¨�?Ó1ò 	ˆCØ�g‘,ˆCÙð	ð ˆÙÜ˜$Ÿ,™,×6Ñ6°sÓ;¸TÓBˆFà�Fˆ{Ðr   c                 ó  — | j                   r| j                   S | j                  d«      \  }}|�'t        | j                  |«      | _         | j                   S | j                  j                  d«      | _         | j                   S )zÅ Return a string table for looking up dynamic tag related strings.

            This won't be a "full" string table object, but will at least
            support the get_string() function.
        Ú	DT_STRTABz.dynstr)rQ   r]   r   r   rH   Úget_section_by_name)r   Ú_r   s      r   Ú_get_stringtablezDynamic._get_stringtablez   sy   € ð ×ÒØ×$Ñ$Ð$ð
 ×/Ñ/°Ó<‰ˆˆ<ØÐ#Ü 3°D·L±LÀ,Ó OˆDÔØ×$Ñ$Ð$ð !ŸL™L×<Ñ<¸YÓGˆÔØ× Ñ Ð r   Nc              #   óª   K  — | j                   ryt        j                  «       D ]+  }| j                  |«      }|�|d   |k(  r|–— |d   dk(  sŒ+ y y­w)z; Yield all raw tags (limit to |type| if specified)
        Nr2   ÚDT_NULL)rP   Ú	itertoolsÚcountÚ_get_tag)r   rV   Únr\   s       r   rW   zDynamic._iter_tags�   sZ   è ø€ ð �;Š;ØÜ—‘Ó"ò 	ˆAØ—-‘- Ó"ˆCØˆ|˜s 7™|¨tÒ3Ø’	Ø�7‰|˜yÓ(Ùñ	ùs   ‚AAÁAc              #   ór   K  — | j                  |¬«      D ]  }t        || j                  «       «      –— Œ  y­w)z7 Yield all tags (limit to |type| if specified)
        rU   N)rW   r)   rb   )r   rV   r\   s      r   Ú	iter_tagszDynamic.iter_tagsœ   s8   è ø€ ð —?‘?¨�?Ó-ò 	;ˆCÜ˜S $×"7Ñ"7Ó"9Ó:Ó:ñ	;ùs   ‚57c                 óä   — | j                   dk7  r|| j                   k\  rt        |«      ‚| j                  || j                  z  z   }t	        | j
                  j                  | j                  |¬«      S )z3 Get the raw tag at index #n from the file
        rG   ©Ú
stream_pos)rK   Ú
IndexErrorrL   rO   r   rJ   rM   r   )r   rh   r    s      r   rg   zDynamic._get_tag¢   s`   € ð �>‰>˜RÒ A¨¯©Ò$7Ü˜Q“-ÐØ—‘  D§M¡MÑ 1Ñ1ˆÜØ�O‰O×#Ñ#Ø�L‰LØôð 	r   c                 óT   — t        | j                  |«      | j                  «       «      S )zC Get the tag at index #n from the file (DynamicTag object)
        )r)   rg   rb   )r   rh   s     r   Úget_tagzDynamic.get_tag­   s#   € ô ˜$Ÿ-™-¨Ó*¨D×,AÑ,AÓ,CÓDÐDr   c                 óð   — | j                   dk7  r| j                   S t        j                  «       D ]D  }| j                  |«      }|j                  j
                  dk(  sŒ.|dz   | _         | j                   c S  y)zG Number of dynamic tags in the file, including the DT_NULL tag
        rG   rd   r   N)rK   re   rf   rp   r1   r2   )r   rh   r\   s      r   Únum_tagszDynamic.num_tags²   sb   € ð �>‰>˜RÒØ—>‘>Ð!ä—‘Ó"ò 	&ˆAØ—,‘,˜q“/ˆCØ�y‰y�‰ )Ó+Ø!" Q¡�”Ø—~‘~Ò%ñ		&r   c           
      ó�  — i }t        | j                  d«      «      r„t        | j                  | j	                  d«      d   t        | j                  d«      «      d   d«      |d<   t        | j                  d«      «      d   }t        |d   j                  |k(  d|z  «       t        | j                  d	«      «      r„t        | j                  | j	                  d	«      d   t        | j                  d
«      «      d   d«      |d<   t        | j                  d«      «      d   }t        |d   j                  |k(  d|z  «       t        | j                  d«      «      rmt        | j                  | j	                  d«      d   t        | j                  d«      «      d   t        | j                  d«      «      d   t        d	   k(  «      |d<   |S )z¹ Load all available relocation tables from DYNAMIC tags.

            Returns a dictionary mapping found table types (REL, RELA,
            JMPREL) to RelocationTable objects.
        ÚDT_RELr   ÚDT_RELSZr6   FÚRELÚ	DT_RELENTzExpected DT_RELENT to be %sÚDT_RELAÚ	DT_RELASZTÚRELAÚ
DT_RELAENTzExpected DT_RELAENT to be %sÚ	DT_JMPRELÚDT_PLTRELSZÚ	DT_PLTRELÚJMPREL)	Úlistrj   r   rH   r]   rX   r   Ú
entry_sizer	   )r   ÚresultÚrelentszs      r   Úget_relocation_tableszDynamic.get_relocation_tables¾   sœ  € ð ˆä�—‘˜xÓ(Ô)Ü+¨D¯L©LØ×%Ñ% hÓ/°Ñ2Ü�T—^‘^ JÓ/Ó0°Ñ9¸5óBˆF�5‰Mô ˜DŸN™N¨;Ó7Ó8¸ÑAˆHÜ�v˜e‘}×/Ñ/°8Ñ;Ø-°Ñ8ô:ô �—‘˜yÓ)Ô*Ü,¨T¯\©\Ø×%Ñ% iÓ0°Ñ3Ü�T—^‘^ KÓ0Ó1°'Ñ:¸DóBˆF�6‰Nô ˜DŸN™N¨<Ó8Ó9¸'ÑBˆHÜ�v˜f‘~×0Ñ0°HÑ<Ø.°Ñ9ô;ô �—‘˜{Ó+Ô,Ü.¨t¯|©|Ø×%Ñ% kÓ2°1Ñ5Ü�T—^‘^ MÓ2Ó3°GÑ<Ü�T—^‘^ KÓ0Ó1°'Ñ:¼jÈÑ>SÑSó UˆF�8Ñð
 ˆr   r   )r#   r$   r%   r&   r   r]   rb   rW   rj   rg   rp   rr   r„   r'   r   r   rE   rE   I   s5   „ ñò(ò<ò !ó,
ó;ò	òEò

&ó!r   rE   c                   ó   — e Zd ZdZd„ Zy)ÚDynamicSectionzH ELF dynamic table section.  Knows how to process the list of tags.
    c           	      óÊ   — t        j                  | |||«       |j                  |d   «      }t        j                  | | j                  | j
                  || d   | d   dk(  «       y )NÚsh_linkÚ	sh_offsetÚsh_typeÚ
SHT_NOBITS)r   r   Úget_sectionrE   r   rH   )r   Úheaderr9   rH   r7   s        r   r   zDynamicSection.__init__å   s]   € Ü×Ñ˜˜v t¨WÔ5Ø×)Ñ)¨&°Ñ*;Ó<ˆÜ×Ñ˜˜tŸ{™{¨D¯L©L¸+Ø�Ñ˜t I™°,Ñ>õ	@r   N)r#   r$   r%   r&   r   r'   r   r   r†   r†   â   s   „ ñó@r   r†   c                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚDynamicSegmentzH ELF dynamic table segment.  Knows how to process the list of tags.
    c           	      óx  — d }|j                  «       D ]4  }t        |t        «      sŒ|d   |d   k(  sŒ |j                  |d   «      } n t	        j
                  | ||«       t        j                  | |||| d   | d   dk(  «       | j                  j                  j                  «       | _
        d | _        d | _        y )Nr‰   Úp_offsetrˆ   Úp_fileszr   )Úiter_sectionsÚ
isinstancer†   rŒ   r
   r   rE   rJ   ÚElf_SymrN   Ú_symbol_sizeÚ_num_symbolsÚ_symbol_name_map)r   r�   r   rH   r7   Úsections         r   r   zDynamicSegment.__init__ï   s¾   € ð ˆØ×,Ñ,Ó.ò 	ˆGÜ˜7¤NÕ3Ø˜KÑ(¨F°:Ñ,>Ó>Ø%×1Ñ1°'¸)Ñ2DÓE�Ùð		ô
 	×Ñ˜˜v vÔ.Ü×Ñ˜˜v w°¸TÀ*Ñ=MØ�*Ñ Ñ"ô	$à ŸO™O×3Ñ3×:Ñ:Ó<ˆÔØ ˆÔØ $ˆÕr   c                 óv  — | j                   �| j                   S | j                  d«      \  }}|�,t        | j                  || «      }|j	                  «       | _         | j                   €B| j                  d«      \  }}|�,t        | j                  || «      }|j	                  «       | _         | j                   €ã| j                  d«      \  }}|�|€t        d«      ‚d}| j                  «       D ]N  }|d   }	|d   dk(  r/| j                  |d	   k7  rt        d
|d	   | j                  fz  «      ‚|	|kD  sŒE|�||	kD  sŒM|	}ŒP |€B| j                  j                  «       D ]%  }
|
d   |k  sŒ||
d   |
d   z   k  sŒ|
d   |
d   z   }Œ' |}||z
  | j                  z  | _         | j                   €t        d«      ‚| j                   S )zA Number of symbols in the table recovered from DT_SYMTAB
        NÚDT_GNU_HASHÚDT_HASHÚ	DT_SYMTABú#Segment does not contain DT_SYMTAB.rA   r2   Ú	DT_SYMENTr6   zDT_SYMENT (%d) != Elf_Sym (%d).Úp_vaddrr’   z&Cannot determine the end of DT_SYMTAB.)
r—   r]   r   rH   Úget_number_of_symbolsr   r   rj   r–   Úiter_segments)r   ra   Úgnu_hash_offsetÚhash_sectionÚhash_offsetÚtab_ptrÚ
tab_offsetÚnearest_ptrr\   Útag_ptrÚsegmentÚend_ptrs               r   Únum_symbolszDynamicSegment.num_symbols  s	  € ð ×ÑÐ(Ø×$Ñ$Ð$ð "×2Ñ2°=ÓAÑˆˆ?ØÐ&Ü'¨¯©°oÀtÓLˆLØ ,× BÑ BÓ DˆDÔð ×ÑÐ$Ø!×2Ñ2°9Ó=‰NˆAˆ{ØÐ&ä+¨D¯L©L¸+ÀtÓL�Ø$0×$FÑ$FÓ$H�Ô!à×ÑÐ$ð #'×"7Ñ"7¸Ó"DÑˆG�ZØˆ *Ð"4ÜÐDÓEÐEØˆKØ—~‘~Ó'ò 
*�Ø˜g™,�Ø�w‘< ;Ò.Ø×(Ñ(¨C°©LÒ8ô 'Ð'HØ(+¨G©°d×6GÑ6GÐ'Hñ(Ió Jð Jà˜gÓ%Ø$Ð,°¸gÓ0EØ")‘Kð
*ð Ð"à#Ÿ|™|×9Ñ9Ó;ò O�GØ 	Ñ*¨gÓ5Ø#¨°	Ñ(:¸WÀZÑ=PÑ(PÓQØ&-¨iÑ&8¸7À:Ñ;NÑ&N™ðOð
 "ˆGØ!(¨7Ñ!2°t×7HÑ7HÑ HˆDÔà×ÑÐ$ÜÐCÓDÐDà× Ñ Ð r   c                 ó   — | j                  d«      \  }}|�|€t        d«      ‚t        | j                  j                  | j
                  ||| j                  z  z   ¬«      }| j                  «       }|j                  |d   «      }t        ||«      S )zG Get the symbol at index #index from the table (Symbol object)
        r�   rž   rl   Úst_name)
r]   r   r   rJ   r•   r   r–   rb   r"   r   )r   Úindexr¦   r§   ÚsymbolÚstring_tableÚsymbol_names          r   Ú
get_symbolzDynamicSegment.get_symbol:  s’   € ð #×3Ñ3°KÓ@Ñˆ�Øˆ?˜jÐ0ÜÐ@ÓAÐAäØ�O‰O×#Ñ#Ø�L‰LØ! E¨D×,=Ñ,=Ñ$=Ñ=ô?ˆð
 ×,Ñ,Ó.ˆØ"×-Ñ-¨f°YÑ.?Ó@ˆä�f˜kÓ*Ð*r   c                 óZ  — | j                   €]t        t        «      | _         t        | j	                  «       «      D ]-  \  }}| j                   |j
                     j                  |«       Œ/ | j                   j                  |«      }|r|D �cg c]  }| j                  |«      ‘Œ c}S dS c c}w )za Get a symbol(s) by name. Return None if no symbol by the given name
            exists.
        N)	r˜   r   r€   Ú	enumerateÚiter_symbolsr9   ÚappendÚgetr³   )r   r9   ÚiÚsymÚsymnumss        r   Úget_symbol_by_namez!DynamicSegment.get_symbol_by_nameK  s•   € ð × Ñ Ð(Ü$/´Ó$5ˆDÔ!Ü# D×$5Ñ$5Ó$7Ó8ò :‘��3Ø×%Ñ% c§h¡hÑ/×6Ñ6°qÕ9ð:à×'Ñ'×+Ñ+¨DÓ1ˆÙ8?¨GÖ4 q�—‘ Õ"Ò4ÐIÀTÐIùÒ4s   ÂB(c              #   ón   K  — t        | j                  «       «      D ]  }| j                  |«      –— Œ y­w)a0   Yield all symbols in this dynamic segment. The symbols are usually
            the same as returned by SymbolTableSection.iter_symbols. However,
            in stripped binaries, SymbolTableSection might have been removed.
            This method reads from the mandatory dynamic tag DT_SYMTAB.
        N)Úranger¬   r³   )r   r¹   s     r   r¶   zDynamicSegment.iter_symbolsY  s4   è ø€ ô �t×'Ñ'Ó)Ó*ò 	&ˆAØ—/‘/ !Ó$Ó%ñ	&ùs   ‚35N)	r#   r$   r%   r&   r   r¬   r³   r¼   r¶   r'   r   r   r�   r�   ì   s"   „ ñò%ò(5!òn+ò"Jó&r   r�   )re   Úcollectionsr   Úhashr   r   Úsectionsr   r   Úenumsr	   Úsegmentsr
   Ú
relocationr   Úcommon.exceptionsr   Úcommon.utilsr   r   r   Úobjectr   r)   rE   r†   r�   r'   r   r   ú<module>rÈ      sm   ðó å #ß ,ß %Ý Ý Ý 'Ý (ß NÑ Nô.˜&ô .ô">�ô ">ôJVˆfô Vôr@�W˜gô @ôt&�W˜gõ t&r   