import sys
import os
import sre

class DisassSYMS:
	header = []
	table = []
	parent = None
	
	def __init__(self, filename, parent):
		self.parent = parent
		self.init(os.popen('objdump -t %s'%filename).readlines())
		
		
	def __str__(self):
		return "".join(self.table)
	
	def init(self, lines):
		self.header = lines[0:4]
		self.table = lines[4:]
		if (lines[4] == "no symbols\n"): return
		for line in self.table:
			if (len(line) > 14):
				address = line[:8]
				parts = line[14:].split()
				self.parent.registerLookup(address, "SYM::%s"%parts[3:])

		
	def getSubName(self,address, clean=False):
		name = ""
		address = address.strip()
		
		for i in self.table:
			#print >>sys.stderr, (" Syms.getSubName(): %s:%s" % (address,i))
			if (i.find(address) > -1):
				if (not clean): name = "SYMS::"
				name += "%s"%i.split()[-1]

		return name
		
	def printTXT(self,outfile=sys.stdout):
		print >>outfile,("\n\nSymbols:\n%s"%(self))
