buildfarm/utils/count_ctors.py
author Mike Hommey
Mon, 06 Dec 2010 12:28:05 -0500
changeset 976 0ee69e37e575502b437944e16059800346ddb96d
parent 709 6c1a3110a600faa85ba82d55116f1dc0144b647c
child 3406 47b77f5fcd442dad86fd1b069befddd936c540e8
permissions -rwxr-xr-x
bug 579131: Keep track of .ctors to fight global initializers. r=bhearsum

#!/usr/bin/python
import subprocess, re
def count_ctors(filename):
    proc = subprocess.Popen(['readelf', '-W', '-S', filename], stdout=subprocess.PIPE)

    for line in proc.stdout:
        f = line.split()
        if len(f) != 11:
            continue
        if f[1] == ".ctors" and f[2] == "PROGBITS":
            return int(f[5], 16) / int(f[10]) - 2;
        if f[1] == ".init_array" and f[2] == "PROGBITS":
            return int(f[5], 16) / int(f[10]);

if __name__ == '__main__':
    import sys
    for f in sys.argv[1:]:
        print "%s\t%s" % (count_ctors(f), f)