³ò
¡AWFc           @   s¾  d  Z  d Z d Z y e Wn du \ Z Z n Xyž d d k Z d d k Z d d k Z d d k Z d d k	 Z	 e i
 i e i
 i e	 i e i ƒ  ƒ d d d	 ƒ ƒ e e e i
 ƒ Z d d
 k l Z l Z Wn1 y e i i d e ƒ Wn e GHn X‚  n Xe i
 i e i d ƒ d Z e i
 i e ƒ d Z e i d ƒ i e ƒ i ƒ  Z d „  Z d „  Z d „  Z d d k Z d Z  d Z! d Z" d Z# d Z$ d Z% d Z& d Z' d Z( d Z) d Z* d Z+ d Z, d Z- d Z. d Z/ d Z0 d Z1 d Z2 d Z3 d Z4 d Z5 d Z6 d Z7 d Z8 d Z9 d Z: d Z; d Z< d Z= d Z> d Z? d Z@ d ZA d ZB d ZC d ZD d ZE d ZF d ZG d ZH d ZI d ZJ d ZK d ZL d ZM d ZN d ZO d ZP d ZQ d ZR d ZS d ZT d ZU d ZV d  ZW d! ZX d" ZY d# ZZ d$ Z[ d% Z\ d Z] d Z^ d Z_ d& Z` d Za d Zb d Zc d' Zd d( Ze d Zf d Zg d Zh d Zi d Zj d' Zk d( Zl d) d* d+ e0 e3 eE f g g d, e! e7 g g d- e! e? g g d. e$ eE g g d/ e  d g g d0 e" d g g d1 e" d g g d2 e$ d g g d3 e! d g g d4 e! d g g d5 e! d g g d6 e! d g g d7 e! d g g d8 e! eF g g g Zm d9 e$ d g g d: e$ eM g g d; e$ d g g d< e  d g g d= e" d g g d> e$ d g g d? e$ eF g g d@ e$ d g g dA e$ d g g dB e$ d g g g
 Zn dC e$ d g g dD e  d g g dE e$ d g g dF e% d g g dG e% d g g dH e! eF g g g Zo d Zp d Zq d Zr d Zs d Zt d Zu d Zv d" Zw d# Zx dI Zy dJ e$ eq g g dK e" d g g dL e  d g g dM e  d g g dN e$ d g g dO e$ d g g dP e$ d g g dQ e$ d g g g Zz d Z{ d Z| d Z} dR dv dS „  ƒ  YZ~ dT dw dU „  ƒ  YZ dV dx dW „  ƒ  YZ€ dX „  Z� dY „  Z‚ dZ „  Zƒ d[ „  Z„ d\ „  Z… d] „  Z† d^ „  Z‡ d_ „  Zˆ d` „  Z‰ da „  ZŠ db „  Z‹ dc „  ZŒ dd „  Z� de „  ZŽ df „  Z� dg „  Z� dh „  Z‘ di „  Z’ dj „  Z“ dk „  Z” dl „  Z• dm „  Z– dn „  Z— do „  Z˜ dp „  Z™ eš dq j ož e Z› e i d Zœ e e j oR yD e� eœ ƒ o3 ež dr eœ d ƒ o e i d Z› e i d Zœ n WqŽqŽXn ež ds e› e eŸ eœ ƒ ƒ f ƒ p dt GHn d S(y   sÒ   routines for creating and dumping ELF files

   this is my plan for the future direction of postForth, to ensure unique
   hashes for every newly defined word in a dictionary, no matter how many
   there may bes2  
    elf -- routines for creating and dumping ELF files
    Copyright (C) 2005  John Comeau <jc.jcomeau.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    s7   Not all needed libraries found, upgrade or check path: i   i    iÿÿÿÿNi   t   libt   python(   t   gplt	   jclicenses   %s
s   [0-9A-Za-z]+c          G   s   t  S(   N(   t   False(   t   whatever(    (    s   ./elf.pyt
   DebugPrint4   s    c          G   s´   |  \ } } t  | ƒ t i j o t | ƒ } n t t d ƒ o | i | ƒ Sn^ d } xP t d t | ƒ ƒ D]9 } | | | } | t | ƒ d j o | | } qo qo W| Sd S(   s   for pythons without str.joint   joint    i    i   N(	   t   typet   typest
   StringTypet   evalt   hasattrt   strR   t   ranget   len(   t   argst   stringt   arrayt   joinedt   index(    (    s   ./elf.pyR   7   s    & c          G   sg   |  \ } } t  | ƒ p
 d } n t d d ƒ o | i | ƒ Sn  t i t i | ƒ ƒ i | ƒ Sd S(   s   for pythons without str.splitR   t   splitN(   R   t   NoneR   R   t   ret   compilet   escape(   R   R   t   string_to_split(    (    s   ./elf.pyR   E   s
    s   <Is   <Hs   <Bi   i   i   i   i   i   i ÿ  iÿÿ  i   iÿ  iñÿ  iòÿ  i	   i
   i   i   piÿÿÿl        l   ÿÿ l      ` i   i   t   e_idents   <16ss   ELF%c%c%c         t   e_typet	   e_machinet	   e_versiont   e_entryt   e_phofft   e_shofft   e_flagst   e_ehsizet   e_phentsizet   e_phnumt   e_shentsizet   e_shnumt
   e_shstrndxt   sh_namet   sh_typet   sh_flagst   sh_addrt	   sh_offsett   sh_sizet   sh_linkt   sh_infot   sh_addralignt
   sh_entsizet   st_namet   st_valuet   st_sizet   st_infot   st_othert   st_shndxi €t   p_typet   p_offsett   p_vaddrt   p_paddrt   p_fileszt   p_memszt   p_flagst   p_alignt   struct_elementc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         G   sN   | |  _  | d |  _ | d |  _ t | ƒ o | d |  _ n
 d  |  _ d  S(   Ni    i   (   t   namet   packinfot   defaultR   t   valueR   (   t   selfRC   t   itemt   init(    (    s   ./elf.pyt   __init__ï   s
    	c         C   s    t  d |  i |  i |  i ƒ |  i o t i |  i |  i ƒ Sn\ t |  i ƒ d j o t i |  i |  i d ƒ Sn( t d t	 t
 |  i g |  i ƒ ƒ ƒ Sd  S(   Ns   getvalue of %si   i    s   struct.pack%s(   R   RC   RD   RE   RF   t   structt   packR   R   t   reprt   tuple(   RG   (    (    s   ./elf.pyt   getvalueõ   s    
c         C   sE   |  i  o |  i  Sn- t |  i ƒ d j o |  i d Sn |  i Sd  S(   Ni   i    (   RF   R   RE   (   RG   (    (    s   ./elf.pyt   rawvalueý   s
    
(   t   __name__t
   __module__RJ   RO   RP   (    (    (    s   ./elf.pyRB   î   s   		t   elf_filec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s«   this class builds the structure of an ELF file

    dumping a gcc-created ELF file, one finds the sections in this order:
    .hash, .dynsym, .dynstr, .text, .data, .bss
 c         C   sj   d \ |  _  |  _ t ƒ  t ƒ  g  |  _ |  _ |  _ |  i i d i ƒ  |  _ t	 ƒ  g |  _
 d |  _ d  S(   Nt    R   R%   i   i   (   s    R   i   (   t   strtabt   textt   symtabt
   elf_headert   headert   pheaderst   fieldRP   t	   phentsizet   section_headert   sheaderst
   text_align(   RG   (    (    s   ./elf.pyRJ     s
    #c      	   C   s4  t  t |  i i ƒ  d ƒ ƒ } t d | ƒ t  |  i ƒ o^t o/ |  i t | t	 | d |  i
 t t Bd ƒ n |  i t d t	 | t |  i
 t  |  i ƒ d d ƒ t  t |  i d d ƒ ƒ t  t |  i d ƒ ƒ t t B|  i ƒ |  i d t t t B| t |  i
 t  |  i ƒ d ƒ t  t |  i d ƒ ƒ d ƒ |  i d
 t t | t |  i
 t  |  i ƒ d ƒ t  t |  i d ƒ ƒ t  t |  i d ƒ ƒ d ƒ n | t |  i
 t  |  i ƒ d ƒ t  t |  i d ƒ ƒ t  t |  i d ƒ ƒ } t	 | t |  i
 t  |  i ƒ d ƒ |  i i d _ | |  i i d _ | |  i i d _ t  |  i ƒ |  i i d _ t  |  i ƒ |  i i d _ t  |  i ƒ d |  i i d _ t |  i i ƒ  d ƒ } x! |  i D] } | | i ƒ  7} q½Wt | d ƒ } | t |  i d ƒ t |  i d ƒ 7} x! |  i D] } | | i ƒ  7} qW| S(   Ni   s   phoff: 0x%xi   i    i   s   .text i   s   .texti   s	   .shstrtabR    R!   R"   R&   R(   R)   i   i   (   R   t   nopadRY   RO   R   RV   R   t
   addsegmentt   PT_PHDRt   SysVloadaddrR\   t   PF_Rt   PF_Xt   PT_LOADt   nopadlenRZ   t   padRU   R_   t
   addsectiont   SHT_PROGBITSt	   SHF_ALLOCt   SHF_EXECINSTRt
   SHT_STRTABR[   RF   R^   (   RG   t   phofft   shoffRF   RY   (    (    s   ./elf.pyRO     sH    
>   O- 
 &
 c         G   s„   |  i  t h  | d d <| d d <| d d <| d d <| d d	 <| d d
 <| d d <| d d <ƒ g 7_  d S(   s9   add a program header, adjusting other things as necessaryi    R:   i   R;   i   R<   R=   i   R>   R?   i   R@   i   RA   N(   RZ   t   program_header(   RG   R   (    (    s   ./elf.pyRa   6  s    'c         G   sÓ   | d d |  i  j o |  i  i | d d ƒ } n' t |  i  ƒ } |  i  | d d 7_  |  i t h  | d <| d d <| d d <| d t d	 <| d d
 <| d d <| d d <ƒ g 7_ d S(   s9   add a section header, adjusting other things as necessaryi    RT   R*   i   R+   i   R,   i   R-   R.   i   R/   i   R2   N(   RU   R   R   R^   R]   Rc   (   RG   R   R*   (    (    s   ./elf.pyRi   <  s    $(   RQ   RR   t   __doc__RJ   RO   Ra   Ri   (    (    (    s   ./elf.pyRS     s
   		%	t   elf_structurec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c      
   G   s1  d h  g  |  _  |  _ |  _ xs t t | ƒ ƒ D]_ } | | d } |  i i | ƒ t | | | d ƒ |  i | <|  i  |  i | i  d 7_  q0 Wt | ƒ o� t i |  i  | d d |  i	 ƒ  !ƒ } t
 d | ƒ d } xQ |  i D]B } t
 d | t | | ƒ f ƒ | | |  i | _ | d 7} qã Wn d  S(   Nt   <i    i   t
   initvaluess   initializing %s with %s(   RD   R[   t   namesR   R   t   appendRB   RK   t   unpackt   getsizeR   RM   RF   (   RG   t
   descriptorRI   R   RC   Rt   (    (    s   ./elf.pyRJ   H  s      !&
 c         C   s5   d } x( |  i  D] } | |  i | i ƒ  7} q W| S(   NR   (   Ru   R[   RO   (   RG   t   dataRC   (    (    s   ./elf.pyRO   W  s
    
 c         C   s   t  i |  i ƒ S(   N(   RK   t   calcsizeRD   (   RG   (    (    s   ./elf.pyRx   \  s    (   RQ   RR   RJ   RO   Rx   (    (    (    s   ./elf.pyRr   G  s   		c          G   sQ  d g } t  i d ƒ } t |  ƒ o( t |  d ƒ t i j o |  d }  n xý |  D]õ } | i | ƒ | o t t | ƒ d ƒ } n t | ƒ d } t | ƒ } d g | d g | } } d | G| G| G| G| GHxU t d t | ƒ ƒ D]> } | | }	 t	 |	 ƒ }
 |
 | } | | | | <| | | <qî Wd | G| G| G| G| GHqT Wd S(	   s®   make hashtable from strings

    found number of buckets equal to len(names)/2 in gcc-generated file
    chains of course is strings+1, the extra being the empty symtab entryR   t   GCC_ELF_HASHi    i   i   s   before "%s": s   after "%s": N(
   t   ost   getenvR   R	   R
   R   Rv   t   intR   t   elf_hash(   Ru   t   symbolst	   gcc_styleRC   t   nbucketst   nchainst   bucketst   chainsR   t   symbolt   hasht   bucket(    (    s   ./elf.pyt   make_hashtable_  s*    	'  

c         C   sN   d } | d j o3 t  |  ƒ | d j o d | t  |  ƒ | } n |  | S(   s%   return string as a multiple of lengthR   i    RT   (   R   (   R   t   lengtht   padding(    (    s   ./elf.pyRh   y  s    $c         C   s   t  t d |  | ƒ ƒ S(   NRT   (   R   Rh   (   t   strlenR‹   (    (    s   ./elf.pyt   padlen€  s    c         C   s   |  S(   s?   nop version of pad, for 'commenting out' padding during testing(    (   R   R‹   (    (    s   ./elf.pyR`   ƒ  s    c         C   s   |  S(   s"   nop version of nopadlen, see nopad(    (   R�   R‹   (    (    s   ./elf.pyRg   ‡  s    c          G   s   t  t ƒ } | S(   N(   Rr   t   symtab_entry_struct(   R   t   entry(    (    s   ./elf.pyt   symtab_entry‹  s    c         C   s‚   t  d |  ƒ d } xT |  D]L } | d >d @t | ƒ } | d @} | o | | d ?N} n | | M} q Wt  d |  | f ƒ | S(   s¡  hash a symbol according to this implementation of the algorithm from
    the spec in pmft11.pdf:

unsigned long elf_hash (const unsigned char *name)
{
 unsigned long h = 0, g;
 while (*name)
 {
  h=(h << 4) + *name++;
  if (g = h & 0xf0000000)
   h ^= g >> 24;
  h &= ~g;
 }
 return h;
}

 this will probably work with unicode characters above chr(127) also, but the
 standard doesn't allow that as far as I can see.
t   hashingi    i   l   ÿÿ l      ` i   s   hash value of "%s": 0x%x(   R   t   ord(   R‡   Rˆ   t   bytet   high_nybble(    (    s   ./elf.pyR€   �  s     
c          G   sW   t  t ƒ } t |  ƒ o: x7 |  d i ƒ  D]! } |  d | g | i | _ q* Wn | S(   Ni    (   Rr   t   section_header_structR   t   keysR[   RE   (   RI   RY   t   key(    (    s   ./elf.pyR]   ®  s     #c          G   sW   t  t ƒ } t |  ƒ o: x7 |  d i ƒ  D]! } |  d | g | i | _ q* Wn | S(   Ni    (   Rr   t   program_header_structR   R—   R[   RE   (   RI   RY   R˜   (    (    s   ./elf.pyRp   µ  s     #c         C   s   t  t |  ƒ } | S(   N(   Rr   R–   (   R]   RY   (    (    s   ./elf.pyt   dump_section_header¼  s    c          C   ss   t  t ƒ }  t i |  i ƒ g |  i d _ t i t ƒ  i ƒ g |  i d _ t i t ƒ  i ƒ g |  i d _ |  S(   NR$   R%   R'   (	   Rr   t   elf_header_structRK   R{   RD   R[   RE   Rp   R]   (   RY   (    (    s   ./elf.pyRX   À  s    c         C   s;   t  |  ƒ } | i ƒ  } | i ƒ  t t | ƒ } | i ƒ  S(   N(   t   opent   readt   closeRr   R›   RO   (   t   filenamet   fileRz   RY   (    (    s   ./elf.pyt   dump_elf_headerÉ  s
    
c         C   s   |  | |  i  d | ƒ !S(   NRT   (   R   (   Rz   t   offset(    (    s   ./elf.pyt	   dump_nameÐ  s    c         C   s@  t  |  ƒ } | i ƒ  } | i ƒ  t t | ƒ } | | _ | i d i ƒ  | _ | i d o t	 | ƒ | _	 n xÈ | i	 D]½ } | i d i ƒ  } t
 | i | i | i d i ƒ  ƒ Gd | GH| t j o t | | ƒ q{ | t j p | t j o t | | ƒ q{ t | i | i d i ƒ  | i d i ƒ  ƒ q{ Wd  S(   NR)   R"   R+   R*   s   type: %dR.   R/   (   Rœ   R�   Rž   Rr   R›   Rz   R[   RP   t   name_section_indext   section_headersR£   t   names_offsett   SHT_HASHt   dump_hash_tablet
   SHT_DYNSYMt
   SHT_SYMTABt   dump_symbol_tablet   dump_raw(   RŸ   R    Rz   R]   t   section_type(    (    s   ./elf.pyt   dump_elf_fileÓ  s(    
	
 c         C   s   t  i d t |  ƒ d |  ƒ S(   Ns   <%dIi   (   RK   Rw   R   (   t   packed(    (    s   ./elf.pyt   unpack_integersé  s    c         C   sµ   g  } |  i  t |  i d i ƒ  ƒ } |  i d i ƒ  } |  i d i ƒ  } t | |  i | ƒ } | i d i ƒ  |  _ x5 t | ƒ D]' } t | | | ƒ } | i | ƒ q† W| S(   NR"   R(   R'   R.   (	   Rz   R   R[   RP   Rš   R¤   R¦   R   Rv   (   R    t   entriest   headerst   sectionst   section_header_entry_sizet   section_namesR   R]   (    (    s   ./elf.pyR¥   ì  s      c         C   sQ  d GH|  i  | i d i ƒ  } |  i | i d i ƒ  } t |  | ƒ | i d i ƒ  } t | d | !ƒ |  _ |  i d |  i d } } |  i d d | !} |  i d | } | G| GHx’ t t |  i	 ƒ ƒ D]{ }	 t
 |  i |  i	 |	 i d i ƒ  ƒ }
 d	 |	 |
 |  i	 |	 i d
 i ƒ  |  i	 |	 i d i ƒ  t |  |
 | | ƒ f GHqÎ Wd  S(   Ns   dumping hash tableR.   R0   R/   i    i   i   R4   s+   symbol table entry %d: "%s", 0x%x, 0x%x, %sR5   R6   (   Rz   R[   RP   R¥   R«   R°   t
   hash_tableR   R   t   symbol_tablet   dump_stringt   linked_stringst	   dump_hash(   R    R]   t   tablet   linked_symbolst   sizet   nbuckett   nchainR…   R†   R�   R   (    (    s   ./elf.pyR¨   ú  s&    	 		c         C   s~   t  | ƒ } | t | ƒ } | | } d | } x: t o2 | d | | 7} | | } | d j o Pq3 q3 Wd | | | f S(   Ns   %ds   ->%di    s   0x%x -> %d: %s(   R€   R   t   True(   R    R   R…   R†   Rˆ   R‰   t   chaint
   chain_list(    (    s   ./elf.pyRº     s    



c   
      C   s¿  t  t ƒ i ƒ  } d | GH|  i | i d i ƒ  } | i d i ƒ  } d | GH| i d i ƒ  } g  |  _ xK t d t | d | !ƒ | ƒ D]* } |  i i	 t  t | | | | !ƒ ƒ q� W|  i
 | i d i ƒ  } | i d i ƒ  } |  i | i d i ƒ  |  _ |  i d | !|  _ d t |  i ƒ GHxŽ t t |  i ƒ ƒ D]w }	 |	 | j  o q@n d	 |	 t |  i |  i |	 i d
 i ƒ  ƒ |  i |	 i d i ƒ  |  i |	 i d i ƒ  f GHq@Wd  S(   Ns   symtab entry size: %dR.   R1   s   local symbols: %dR/   i    R0   s   linked strings: "%s"s'   symbol table entry %d: "%s", 0x%x, 0x%xR4   R5   R6   (   Rr   R�   Rx   Rz   R[   RP   R·   R   R   Rv   R¥   R¹   RM   R¸   (
   R    R]   t   symtab_entry_sizeR·   t   local_symbolsR½   R¢   R¹   t   strings_sizeR�   (    (    s   ./elf.pyR«     s2    			   	c         C   s   |  | |  i  d | ƒ !S(   NRT   (   R   (   t   string_tableR¢   (    (    s   ./elf.pyR¸   2  s    c         C   s   d GHt  |  d | !ƒ GHd  S(   Ns   dumping raw datai    (   RM   (   Rz   R½   (    (    s   ./elf.pyR¬   5  s    c           C   s
   t  ƒ  g S(   N(   R‘   (    (    (    s   ./elf.pyRW   9  s    c          G   s	   d GHd  S(   Ns   dumping symbol table:(    (   R   (    (    s   ./elf.pyt   dump_symtab<  s    c         C   s]   t  ƒ  } t |  ƒ i ƒ  i t d ƒ ƒ | _ t | d ƒ } | i | i ƒ  ƒ | i ƒ  t	 S(   Ni�   t   wb(
   RS   Rœ   R�   t   rstript   chrRV   t   writeRO   Rž   RÀ   (   t
   objectfilet   elffilet   elf(    (    s   ./elf.pyt   create_elf_file?  s    	$
c           C   s	   t  GHd S(   sB   default entry point, make symlinks to other entry points as neededN(   Rq   (    (    (    s   ./elf.pyRÎ   G  s    t   __main__s   type(%s) == types.FunctionTypes   %s%sR   (   i   i    (    (    (    (    Rq   t	   Copyrightt   errormessageRÀ   R   t   sysR}   R
   R   t   pwdt   pathRv   R   t   getpwuidt   geteuidRM   t   com.jcomeauR   R   t   stderrRË   R   t   argvt   myselft   splitextt   commandR   t   searcht   groupt   originalselfR   RK   t
   Elf32_Addrt
   Elf32_Halft	   Elf32_Offt   Elf32_Swordt
   Elf32_Wordt   uchart	   EI_NIDENTt   EI_MAG0t   EI_MAG1t   EI_MAG2t   EI_MAG3t   EI_CLASSt   EI_DATAt
   EI_VERSIONt   EI_PADt   ELFCLASSNONEt
   ELFCLASS32t
   ELFCLASS64t   ELFDATANONEt   ELFDATA2LSBt   ELFDATA2MSBt   ET_NONEt   ET_RELt   ET_EXECt   ET_DYNt   ET_COREt	   ET_LOPROCt	   ET_HIPROCt   EM_NONEt   EM_M32t   EM_SPARCt   EM_386t   EM_68Kt   EM_88Kt   EM_860t   EM_MIPSt   EV_NONEt
   EV_CURRENTt	   SHN_UNDEFt   SHN_LORESERVEt
   SHN_LOPROCt
   SHN_HIPROCt   SHN_ABSt
   SHN_COMMONt   SHN_HIRESERVEt   SHT_NULLRj   Rª   Rm   t   SHT_RELAR§   t   SHT_DYNAMICt   SHT_NOTEt
   SHT_NOBITSt   SHT_RELt	   SHT_SHLIBR©   t
   SHT_LOPROCt
   SHT_HIPROCt
   SHT_LOUSERt
   SHT_HIUSERt	   SHF_WRITERk   Rl   t   SHF_MASKPROCt	   STB_LOCALt
   STB_GLOBALt   STB_WEAKt
   STB_LOPROCt
   STB_HIPROCt
   STT_NOTYPEt
   STT_OBJECTt   STT_FUNCt   STT_SECTIONt   STT_FILEt
   STT_LOPROCt
   STT_HIPROCR›   R–   R�   t   PT_NULLRf   t
   PT_DYNAMICt	   PT_INTERPt   PT_NOTEt   PT_SHLIBRb   t	   PT_LOPROCt	   PT_HIPROCRc   R™   Rd   t   PF_WRe   RB   RS   Rr   RŠ   Rh   RŽ   R`   Rg   R‘   R€   R]   Rp   Rš   RX   R¡   R£   R®   R°   R¥   R¨   Rº   R«   R¸   R¬   RW   RÇ   RÏ   RÎ   RQ   t   functionR   R   R   RN   (    (    (    s   ./elf.pys   <module>   s|  <5				
	B																										"",