#!/usr/bin/python

## parse objdump output to get a (partial) list of who calls who

# output of nm -u chopped to just be the undefined symbols
f = open('./undefined.txt')
undefined = [line.strip() for line in f.readlines()]

# output of
# objdump -rl -w -j '.text' object-file  > test.txt
f = open('./test.txt')
fl = [line.strip() for line in f.readlines()]

all = {}
i = 0
while i < len(fl):
    if not fl[i].endswith('():'):
        i += 1
        continue

    fn = fl[i][:-3]
    i += 1
    while not fl[i].endswith('():'):
        if fl[i].startswith('/tmp/bora/'):
            lineno = fl[i]
            i += 1
        else:
            # nothing more to do...
            break

        undefs = []
        while fl[i].startswith('000'):
            r = fl[i][35:-19]
            # is this an undefined reloc we are interested in?
            if r in undefined:
                undefs.append((lineno, r))
            i += 1

        if undefs:
            for (lineno, reloc) in undefs:
                if not all.has_key(fn):
                    all[fn] = {}

                if not all[fn].has_key(reloc):
                    all[fn][reloc] = []

                if lineno not in all[fn][reloc]:
                    all[fn][reloc].append(lineno)

for (fn, usage) in all.items():

    print "%s calls:" % fn
    for (f, lines) in usage.items():
        print " + %s" % (f)
        for l in lines:
            print " |- %s" % l

    print
    
