Parse the Txt file to get a list of .o file names.
I have a txt file, for example:
test.txt
Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT|00000004| |.data
__ctype_tab |00000000| r | OBJECT|00000101| |.rodata
Symbols from _ashldi3.o:
Name Value Class Type Size Line Section
__ashldi3 |00000000| T | FUNC|00000050| |.text
Symbols from _ashrdi3.o:
Name Value Class Type Size Line Section
__ashrdi3 |00000000| T | FUNC|00000058| |.text
Symbols from _fixdfdi.o:
Name Value Class Type Size Line Section
__fixdfdi |00000000| T | FUNC|0000004c| |.text
__fixunsdfdi | | U | NOTYPE| | |*UND*
Symbols from _fixsfdi.o:
Name Value Class Type Size Line Section
__fixsfdi |00000000| T | FUNC|0000004c| |.text
__fixunssfdi | | U | NOTYPE| | |*UND*
Symbols from _fixunssfdi.o:
Name Value Class Type Size Line Section
__cmpdi2 | | U | NOTYPE| | |*UND*
__fixunssfdi |00000000| T | FUNC|00000228| |.text
__floatdidf | | U | NOTYPE| | |*UND*
What I want to do is I get a function whose type is NOTYPE. I need to find txt and find under which .o it is defined (i.e. with type FUNC). When I receive the .o file, I can see other functions as NOTYPE. Then I have to search where they are defined. Continues. Finally, I want to return a list of the names of all .o files containing functions.
My code snippet:
notypeDict , funcDict = {} , {}
notypeList , funcList = [] , []
currObj , prevObj = '' , ''
fp = open(r'C:\test.txt','r') # file path cms here
fileList = fp.readlines()
for line in fileList:
if '.o' in line: # line containg .o
currObj=line.split()[-1][0:-1]
if '|' not in line: # line containg |
pass
else: # other lines
dataList=[dataItem.strip() for dataItem in line.strip().split('|')] # a list of each word in line
name=dataList[0].strip() # name of the function
notypeDict[prevObj] = notypeList # notypeDict is a dictionary which contains .o as key and a list of NOTYPE function name
funcDict[prevObj] = funcList # funcDict is a dictionary which contains .o as key and a list of FUNC function names
if prevObj == currObj :
pass
if prevObj != currObj :
notypeList , funcList = [] , []
if dataList[3] == 'NOTYPE' :
notypeList.append(name)
if dataList[3] == 'FUNC' :
funcList.append(name)
prevObj = currObj
print 'notypeDict' , notypeDict
print '\n\nfuncDict' , funcDict
Here I will get two dictionaries, notypeDict and funcDict.
notypeDict has .o as key and NOTYPE list as value funcDict has .o as key and FUNC function list as value.
I got this far.
But don't get ideas on how to get started with my goal.
I think my question is clear.
Please help me.
a source to share
I would use regular expressions with capture groups for different kinds of interesting lines in your file; I would go through the file line by line, and when I found the interesting line (i.e. matched the regex), I processed the captured data from the regex appropriately.
After creating dictionaries, etc. the answers to data-driven questions are simple.
a source to share
What do you think the following does?
if '.o' in line: # line containg .o
currObj=line.split()[-1][0:-1]
if '|' not in line: # line containg |
pass
else: # other lines
It will find lines with '.o' or '|' or other?
No. In fact, this is not the case.
It finds lines containing ".o". And does something with them.
Then it checks that line again for '|' or another. "All your .o 'lines are processed twice.
Once ".o", then again as "not |".
You can mean elif
instead if
.
This code
if prevObj == currObj :
pass
if prevObj != currObj :
notypeList , funcList = [] , []
This is more difficult than necessary. Doesn't cause a problem per se, but it's silly.
This code
if dataList[3] == 'NOTYPE' :
notypeList.append(name)
if dataList[3] == 'FUNC' :
funcList.append(name)
probably good. However, this looks bad because the terms are exclusive and look better than elif
.
a source to share
How about this code? It is based on your two dictionaries. Just call find_dep_for_func(notype_funcname)
.
def find_ofile(funcname):
"""This will find .o file for given function."""
for ofile, fns in funcDict.iteritems():
if funcname in fns:
return ofile
raise Exception("Cannot find function "+funcname)
def find_dependencies(ofile, deps = None):
"""This will find dependent .o files for given .o file."""
olist = deps if deps else set([])
for fn in notypeDict[ofile]:
ofile = find_ofile(fn)
if not ofile in olist:
olist.add(ofile)
olist = find_dependencies(ofile, olist)
return olist
def find_dep_for_func(notype_funcname):
return find_dependencies(find_ofile(funcname))
a source to share