
    g2                        S r SSKrSSKrSSKJr   " S S\R
                  5      r " S S\R
                  5      r " S S	\R
                  5      r " S
 S\R
                  5      r	 " S S\R
                  5      r
 " S S\R
                  5      r " S S\R
                  5      r " S S\R
                  5      r " S S\R
                  5      rSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSr Sr!Sr"Sr#Sr$Sr%S r&Sr' " S! S"\RP                  5      r)\ " S# S$5      5       r*\ " S% S&5      5       r+\ " S' S(5      5       r,\ " S) S*5      5       r-\ " S+ S,5      5       r.S-\.S.\S/\/4S0 jr0\ " S1 S25      5       r1\ " S3 S45      5       r2\ " S5 S65      5       r3 " S7 S8\Rh                  5      r5 " S9 S:\Rh                  5      r6\ " S; S<5      5       r7S=\7S.\4S> jr8\ " S? S@5      5       r9SA\9S.\4SB jr: " SC SD\Rh                  5      r;\ " SE SF5      5       r<S=\<S.\4SG jr=SH\>\?   S/\/4SI jr@\ " SJ SK5      5       rASL\AS.\4SM jrBg)NzaDefinitions for the executable and linkable file format 'ELF'.
based on file:///usr/include/elf.h    N)	dataclassc                   <    \ rS rSrSr\" S/5      r\" S/5      rSrg)ElfClass	   zWord size of the target machine       N)	__name__
__module____qualname____firstlineno____doc__bytesBITS_32BITS_64__static_attributes__r	       */mnt/public/Repos/urclelf/./elf/structs.pyr   r   	   s    %QCjGQCjGr   r   c                   <    \ rS rSrSr\" S/5      r\" S/5      rSrg)	Endianess   zEndianess of the target machiner   r   r	   N)	r
   r   r   r   r   r   LSBMSBr   r	   r   r   r   r      s    %
*C
*Cr   r   c                   ,    \ rS rSrSr\" / SQ5      rSrg)
ElfVersion   zAlways version 1r   r   r   r   r	   Nr
   r   r   r   r   r   CURRENTr   r	   r   r   r   r      s    L!Gr   r   c                   <    \ rS rSrSr\" S/5      r\" S/5      rSrg)OSABI   *Operating system ABI of the target machiner      r	   N)	r
   r   r   r   r   r   SYSTEM_VLINUXr   r	   r   r   r!   r!      s    0aSzH1#JEr   r!   c                   h    \ rS rSrSr\" SS/5      r\" SS/5      r\" SS/5      r\" SS/5      r	Sr
g)	FileType   zSpecifies the type of the filer   r   r   r$   r	   N)r
   r   r   r   r   r   UNKNOWNRELOCATABLE
EXECUTABLESHARED_OBJECTr   r	   r   r   r(   r(      s9    $QFmGA-K1vJ1a&MMr   r(   c                   T    \ rS rSrSr\" SS/5      r\" SS/5      r\" SS/5      rSr	g)	TargetISA#   z%Instruction set of the target machiner   r$   >   r	   N)
r
   r   r   r   r   r   r*   X86X64r   r	   r   r   r/   r/   #   s-    +QFmG
A-C
q	
Cr   r/   c                   *    \ rS rSrSr\" S/5      rSrg)
ABIVersion)   r#   r   r	   Nr   r	   r   r   r5   r5   )   s    0QCjGr   r5   c                   |    \ rS rSrSr\" / SQ5      r\" / SQ5      r\" / SQ5      r\" / SQ5      r	\" / SQ5      r
Srg	)
Elf32SectionHeaderType-   z5Determines what the contents of the section representr   r   r   r   r   )r   r   r   r   )r$   r   r   r   )   r   r   r   r	   N)r
   r   r   r   r   r   NULLPROGRAM_DATASYMBOL_TABLESTRING_TABLEr+   r   r	   r   r   r8   r8   -   s7    ;D&L&L&L%Kr   r8   c                   @    \ rS rSrSr\" / SQ5      r\" / SQ5      rSrg)Elf32ProgramHeaderType5   z-Determines what the program header representsr:   r   r	   N)	r
   r   r   r   r   r   r<   LOADABLEr   r	   r   r   rA   rA   5   s    3D\"Hr   rA      4   @       (   r   r   r;   r$         c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
KnownSectionNamesU   z.nullz	.shrtrtabz.symtabz.strtabz.textz	.rel.textr	   N)r
   r   r   r   r<   SECTION_HEADER_STRING_TABLEr>   r?   TEXTRELOCATION_TEXTr   r	   r   r   rL   rL   U   s!    D"-LLD!Or   rL   c                   |    \ rS rSr% SrSr\\S'   \\S'   \	" S/5      r
\\S'   \R                  r\	" S5      rS	 rS
rg)ELFIdentifier]   z:identifies the ELF, goes in the header - AKA char* e_idents   ELF	elf_class	endianessr   os_abi   c                    U R                   U R                  R                  -   U R                  R                  -   U R                  -   U R
                  R                  -   U R                  R                  -   U R                  -   $ )N)magicrT   valuerU   versionrV   abi_versionpaddingselfs    r   	__bytes__ELFIdentifier.__bytes__h   s    zzDNN0004>>3G3GG$,,VY]YdYdYjYjjmqm}m}  nD  nD  D  GK  GS  GS  S  	Sr   r	   N)r
   r   r   r   r   rY   r   __annotations__r   r   r[   r!   r5   r   r\   r]   r`   r   r	   r   r   rR   rR   ]   s@    DEQCjGM$$KAhGSr   rR   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \	R                  r\\S'   \\S'   \\S'   \\S	'   \\S
'   \\S'   \\S'   \\S'   \\S'   \\S'   S rSrg)	ElfHeaderl   z,provides important information about the ELFelf_identifier	file_type
target_isaentry_pointprogram_header_offsetsection_header_offsetflagself_header_sizeprogram_header_entry_sizeprogram_header_countsection_header_entry_sizesection_header_countsection_header_name_indexc                 B   U R                   R                  [        R                  :X  a  SnOSn[	        5       nU[	        U R                   5      -  nX R
                  R                  -  nX R                  R                  -  nX R                  R                  -  nU[        R                  " UU R                  U R                  U R                  U R                  U R                  U R                   U R"                  U R$                  U R&                  U R(                  5      -  nU$ )N
LLLIHHHHHH
IIIIHHHHHH)rf   rT   r   r   r   rg   rZ   rh   elf_versionstructpackri   rj   rk   rl   rm   rn   ro   rp   rq   rr   )r_   formatresults      r   r`   ElfHeader.__bytes__   s    ((H,<,<<!F!F%++,,..&&&//'''""(((&++f&&&&JJ  **%%**%%**
, 
	, r   r	   N)r
   r   r   r   r   rR   rb   r(   r/   r   r   rv   intr`   r   r	   r   r   rd   rd   l   sb    6!!$$KJ""""""r   rd   c                   p    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S	'   \\S
'   S rSr	g)Elf32ProgramHeader   z$Defines a segment loaded into memoryheader_typefile_offsetvirtual_addressphysical_address	file_sizememory_sizerl   alignc                     U R                   R                  [        R                  " SU R                  U R
                  U R                  U R                  U R                  U R                  U R                  5      -   $ )NIIIIIII)r   rZ   rw   rx   r   r   r   r   r   rl   r   r^   s    r   r`   Elf32ProgramHeader.__bytes__   s      &&Y@P@PRVRfRfhlh}h}  @D  @N  @N  PT  P`  P`  bf  bl  bl  nr  nx  nx  *y  y  	yr   r	   N)
r
   r   r   r   r   rA   rb   r|   r`   r   r	   r   r   r~   r~      s8    *''NJJyr   r~   c                   &    \ rS rSr% \\   \S'   Srg)Elf32ProgramHeaderTable   entriesr	   N)r
   r   r   r   listr~   rb   r   r	   r   r   r   r          $%%r   r   c                       \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S	'   \\S
'   \\S'   \S 5       r\S\4S j5       r	Sr
g)Elf32SectionHeader   
name_indexr   rl   r   r   r   
link_indexinfor   
entry_sizec                  B    [        S[        R                  SSSSSSSSS9
$ Nr   )
r   r   rl   r   r   r   r   r   r   r   )r   r8   r<   r	   r   r   nullElf32SectionHeader.null   s3    !055
 	
r   typec                 &    [        SU SSSSSSSSS9
$ r   )r   )r   s    r   blank_of_type Elf32SectionHeader.blank_of_type   s-    !
 	
r   r	   N)r
   r   r   r   r|   rb   r8   staticmethodr   r   r   r	   r   r   r   r      sg     O''JNO
IJO
 
 
2 
 
r   r   headerrT   returnc                    U[         R                  :X  a  SnOSn[        R                  " SU R                  5      U R
                  R                  -   [        R                  " UU R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  5	      -   $ )NLLLLIILLIIIIIIIII)r   r   rw   rx   r   r   rZ   rl   r   r   r   r   r   r   r   )r   rT   ry   s      r   section_header_to_bytesr      s    H$$$;;sF--.kk&	 r   c                   &    \ rS rSr% \\   \S'   Srg)Elf32SectionHeaderTable   r   r	   N)r
   r   r   r   r   r   rb   r   r	   r   r   r   r      r   r   r   c                   v    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \	\S'   \
S 5       r\
S\\   4S	 j5       rS
rg)Section   namedatar   r   is_executablec                 h    [        [        R                  [        5       S[        R                  S5      $ )Nr   F)r   rL   r<   r   r8   )clss    r   r   Section.null   s&    (--uw;Q;V;VX]^^r   namesc                     [         R                  n[        5       nU H#  nX4R                  S5      [        S/5      -   -  nM%     [	        U5      n[
        R                  nSn[        X#XVU5      $ )Nasciir   F)rL   rN   r   encodelenr8   r?   r   )r   r   r   r   section_namer   r   r   s           r   section_namesSection.section_names   sf     !<<w!L''05!:==D "$i%22t;mDDr   r	   N)r
   r   r   r   strrb   r   r|   r8   boolclassmethodr   r   r   r   r	   r   r   r   r      sT    
I
K
  _ _ 
E$s) 
E 
Er   r   c                   R    \ rS rSr% Sr\\S'   \\S'   \\S'   \	\
\4   \S'   S rSrg	)
Elf32i	  zELF binary file
elf_headerprogram_header_tablesection_header_tablesectionsc                    [        [        U R                  5      5      n[        5       n[        5       nU R                  R                   H  nUR                  [        U5      5        M     U R                  R                   H;  nUR                  [        XPR                  R                  R                  5      5        M=     SU4/nU R                  R                  (       a.  U(       a'  UR                  U R                  R                  U45        U R                  R                  (       a.  U(       a'  UR                  U R                  R                  U45        U R                  R                  5        H!  u  pxUR                  U[        U5      45        M#     Sn	U H  u  pU
[        U5      -   nX:  d  M  Un	M     [        U	5      nU H  u  pUSU
 U-   X[        U5      -   S -   nM      [        U5      $ )z/Converts entire ELF to machine-ready executabler   N)	bytearrayr   r   r   r   extendr   r   rf   rT   rj   appendrk   r   itemsr   )r_   elf_header_binprogram_header_table_binsection_header_table_binprogram_headersection_headerfile_elementsr   r   r   offsetrequired_sizerz   s                r   r`   Elf32.__bytes__  s   "5#9:#,; #,; "77??N$++E.,AB @ #77??N$++,CNTcTcTrTrT|T|,}~ @ 89.6I5J??005M  $//"G"GIa!bc??005M  $//"G"GIa!bc!%!4!4!6K  +y!?@ "7 	)LF"SY.M()	 * 9%)LFGV_t+fCI5E5F.GGF * V}r   r	   N)r
   r   r   r   r   rd   rb   r   r   dictr|   r   r`   r   r	   r   r   r   r   	  s+    11113: r   r   c                   0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)
SymbolTypei5  r   r   r   r$   r;         r	   N)r
   r   r   r   NONEDATA_OBJECTFUNCTIONSECTIONFILENAMECOMMONTHREAD_LOCAL_STORAGEr   r	   r   r   r   r   5  s&    DKHGHFr   r   c                        \ rS rSrSrSrSrSrg)SymbolBindingi>  r   r   r   r	   N)r
   r   r   r   LOCALGLOBALWEAKr   r	   r   r   r   r   >  s    EFDr   r   c                   \    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   S	rg
)Elf32SymbolTableEntryiC  r   rZ   sizer   bindingothersection_indexr	   N)	r
   r   r   r   r|   rb   r   r   r   r	   r   r   r   r   C  s(    OJ
I
Jr   r   entryc           	         U[         R                  :X  ah  [        R                  " SU R                  U R
                  U R                  S-  -   U R                  U R                  U R                  U R                  5      $ [        R                  " SU R                  U R                  U R                  U R
                  U R                  S-  -   U R                  U R                  5      $ )NIBBHLLr;   IIIBBH)r   r   rw   rx   r   r   r   r   r   rZ   r   )r   rT   s     r   symbol_table_entry_to_bytesr   M  s    H$$${{8U%5%5

u}}XYGY7Z\a\g\gini|i|  D  J  J  LQ  LV  LV  W  	W{{8U%5%5u{{EJJQVQ[Q[`e`m`mqr`rPsuz  vA  vA  CH  CV  CV  W  	Wr   c                   ,    \ rS rSr% \\   \S'   S rSrg)SymbolTableiT  r   c                     Sn[        U R                  5       H.  u  p#UR                  [        R                  :X  d  M%  X!:  d  M,  UnM0     US-   $ )Nr   r   )	enumerater   r   r   r   )r_   &largest_symbol_index_with_local_symbolindexsymbols       r   )get_symbol_index_of_first_nonlocal_symbol5SymbolTable.get_symbol_index_of_first_nonlocal_symbolX  sG    12.&t||4ME~~!4!449g9>6 5 699r   r	   N)	r
   r   r   r   r   r   rb   r   r   r	   r   r   r   r   T  s    '((:r   r   symbolsc           	      v    SR                  U R                   Vs/ s H  n[        X!5      PM     sn5      $ s  snf Nr   )joinr   r   )r   rT   r   s      r   symbol_table_to_bytesr   a  s2    88RYRaRabRa0CRabccb   6c                   @    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrSrg)X86RelocationTypeie  r   r   r   r$   r;   r   r   rW      r   
   r	   N)r
   r   r   r   r   	SIMPLE_32PC_RELATIVE_32GLOBAL_OFFSET_TABLE_INDEX_32(PROCEDURE_LOOKUP_TABLE_ENTRY_RELATIVE_32COPYSYMBOL_VALUE_NO_ADDEND_32JUMP_SLOT_32R_386_RELATIVELOAD_ADDRESS_RELATIVE_32R_386_GOTPCr   r	   r   r   r   r   e  s=    DIN#$ /0,D !LN Kr   r   c                   >    \ rS rSr% \\S'   \\S'   \\S'   \\S'   Srg)RelocationTableEntryWithAddendir  r   r   symbol_indexaddendr	   N)r
   r   r   r   r|   rb   r   r   r	   r   r   r  r  r  s    K
Kr   r  c                     U[         R                  :X  a  SnSnOSnSn[        R                  " X R                  U R
                  U-  U R                  -   U R                  5      $ )NLLlrG   IIir   )r   r   rw   rx   r   r  r   r  )r   rT   ry   symbol_index_bit_offsets       r   relocation_table_entry_to_bytesr  y  s\    H$$$"$"#;;v||e.@.@D[.[_d_i_i-ikpkwkwxxr   stringsc           	          S[        / 5      R                  U  Vs/ s H  oR                  S5      S-   PM     sn5      -   $ s  snf )N    r   )r   r   r   )r  strings     r   !make_null_terminated_string_tabler    s;    59>>PW"XPWf==#9E#APW"XYYY"Xs   A 
c                   &    \ rS rSr% \\   \S'   Srg)RelocationTablei  r   r	   N)r
   r   r   r   r   r  rb   r   r	   r   r   r  r    s    011r   r  tablec           	      v    SR                  U R                   Vs/ s H  n[        X!5      PM     sn5      $ s  snf r   )r   r   r  )r  rT   
relocations      r   relocation_table_to_bytesr    s3    88^c^k^kl^kPZ4ZK^klmmlr   )Cr   rw   enumdataclassesr   Enumr   r   r   r!   r(   r/   r5   r8   rA   E_IDENT_SIZEELF32_HEADER_SIZEELF64_HEADER_SIZEELF32_PROGRAM_HEADER_SIZEELF32_SECTION_HEADER_SIZEELF64_SECTION_HEADER_SIZE
ELFCLASS32ELFDATA2LSB
EV_CURRENTELF_OSABI_SYSVET_EXECEM_ARMPT_LOADPF_RPF_WPF_XSHT_NULLSHT_PROGBITS
SHT_STRTAB	SHF_ALLOCSHF_EXECINSTRELF32_SYMBOL_TABLE_ENTRY_SIZEELF64_SYMBOL_TABLE_ENTRY_SIZE!ELF32_RELOCATION_TABLE_ENTRY_SIZE!ELF64_RELOCATION_TABLE_ENTRY_SIZEStrEnumrL   rR   rd   r~   r   r   r   r   r   r   r   IntEnumr   r   r   r   r   r   r   r  r  r   r   r  r  r  r	   r   r   <module>r=     s6  &   !tyy 
		 
" "DII 
"tyy "		  &TYY &#TYY #
      


	

	 "  " $& !$& !" " S S S ) ) )V y y y & & & )
 )
 )
V$6 8 PU ( & & & E E E0 ) ) )V DLL 
   W'< W W 
: 
: 
:d; d8 d    y+I yV^ yZtCy ZU Z 2 2 2n_ n nr   