Guest User

mclf.py

a guest
Feb 2nd, 2015
575
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. """
  3. Loader for TLBIN files (MCLF format)
  4.  
  5. Layout:
  6.   [HDR + textsz] - text, mapped at txt addr
  7.   [datasz]       - data, mapped at data addr
  8.   [0 bytes]      - bss, mapped at data addr + datasz ?
  9.   [521 bytes]    - sig, not mapped
  10. """
  11. import struct
  12.  
  13. class Error(Exception) :
  14.     pass
  15.  
  16. class Readable(object) :
  17.     def get(self, fmt, pos=None) :
  18.         if pos is not None :
  19.             self.f.seek(pos)
  20.         sz = struct.calcsize(fmt)
  21.         dat = self.f.read(sz)
  22.         if len(dat) != sz :
  23.             raise Error("short read")
  24.         return struct.unpack('<' + fmt, dat)
  25.     def __str__(self) :
  26.         def fmt(x) :
  27.             if isinstance(x, int) :
  28.                 return '0x%x' % x
  29.             else :
  30.                 return repr(x)
  31.         nms = self.__fields__.split(',')
  32.         fs = ','.join('%s=%s' % (nm, fmt(getattr(self, nm))) for nm in nms)
  33.         return '[%s %s]' % (self.__class__.__name__, fs)
  34.     __repr__ = __str__
  35.  
  36. class Segment(Readable) :
  37.     __fields__ = 'start,len'
  38.     def __init__(self, f) :
  39.         self.f = f
  40.         self.load()
  41.     def load(self) :
  42.         self.start, self.len = self.get('II')
  43.  
  44. class MCLF(Readable) :
  45.     __fields__ = 'mag,vmag,vmin,flags,memType,serviceType,ninstances,uuid,driverId,numThreads,text,data,bssLen,entry,serviceVersion'
  46.     MAG = 'MCLF'
  47.  
  48.     def __init__(self, fn) :
  49.         self.f = file(fn, 'rb')
  50.         self.load()
  51.  
  52.     def getSegment(self) :
  53.         return Segment(self.f)
  54.  
  55.     def loadV1(self) :
  56.         self.flags,self.memType,self.serviceType,self.ninstances = self.get('IIII')
  57.         self.uuid = self.get('16s')[0].encode('hex')
  58.         self.driverId,self.numThreads = self.get('II')
  59.         self.text = self.getSegment()
  60.         self.data = self.getSegment()
  61.         self.bssLen,self.entry = self.get('II')
  62.         self.serviceVersion = None
  63.  
  64.     def loadV2(self) :
  65.         self.serviceVersion = self.get('I')[0]
  66.  
  67.     def load(self) :
  68.         self.mag,self.vmin,self.vmag = self.get("4sHH")
  69.         if self.mag != self.MAG :
  70.             raise Error("bad magic")
  71.         if self.vmag < 1 :
  72.             raise Error("unsupported version")
  73.         if self.vmag >= 1 :
  74.             self.loadV1()
  75.         if self.vmag >= 2 :
  76.             self.loadV2()
  77.  
  78. def test() :
  79.     import sys
  80.     def sz(f) :
  81.         f.seek(0, 2)
  82.         return f.tell()
  83.     for fn in sys.argv[1:] :
  84.         print fn
  85.         m = MCLF(fn)
  86.         print m
  87.         #print 'sz=%x xtra=%x' % (sz(m.f), sz(m.f) - (m.text.len + m.data.len))
  88.         print
  89.  
  90. if __name__ == '__main__' :
  91.     test()
RAW Paste Data