#!/usr/bin/python # # ### docco. prefix all external symbols, write a new .o file # import sys import coff_file def apply_prefix(prefix, src_fname, dst_fname, rename_one=None): tm, fileflags, symbols, sections = coff_file.read_file(src_fname) for i in range(len(symbols)): # Symbol exists (not a placeholder): # 1) the specific symbol we're looking for # 2) otherwise, is externally visible, and is within a SECTION # (meaning: defined, rather than just declared). The section # number specifies definition, rather than external reference. if symbols[i] \ and (rename_one == symbols[i][0] or (rename_one is None and symbols[i][4] == coff_file.C_EXT and symbols[i][2] > 0)): name = symbols[i][0] print 'Renaming: %s ==> %s' % (name, prefix + name) symbols[i] = (prefix + name,) + symbols[i][1:] coff_file.write_file(dst_fname, tm, fileflags, symbols, sections) if __name__ == '__main__': if len(sys.argv) != 4 and len(sys.argv) != 5: print 'USAGE: %s PREFIX [SYMBOL] SOURCE DEST' \ % (os.path.basename(sys.argv[0]),) sys.exit(1) if len(sys.argv) == 5: apply_prefix(sys.argv[1], sys.argv[3], sys.argv[4], sys.argv[2]) else: apply_prefix(*sys.argv[1:])