import sys
import os
import sre

class DisassDynSYMS:
	header = []
	table = []
	
	def __init__(self, filename, parent):
		self.parent = parent
		self.init(os.popen('objdump -T %s'%filename).readlines())
		
	def __str__(self):
		string = self.header[-1]
		string += "".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, "DYN::%s"%parts[3:])
		
	def getSubName(self,address, clean = False):
		name = ""
		address = address.strip()
		
		for i in self.table:
			thisentry = i.split()
			
			if (len(thisentry) == 0):
				continue
			
			#test = thisentry[0].find(address)
			#print >>sys.stderr, (" DynSyms.getSubName(): %s:%s:%d" % (address,thisentry[0],test))
			if (thisentry[0].find(address) > -1):
				if (not clean): name += "DYN::"
				name += "%s"%thisentry[-1]

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

