|
14 | 14 |
|
15 | 15 |
|
16 | 16 | def main():
|
17 |
| - parser = argparse.ArgumentParser(description=__doc__) |
18 |
| - parser.add_argument( |
19 |
| - '--objcopy', |
20 |
| - required=True, |
21 |
| - help='The objcopy binary to run', |
22 |
| - metavar='PATH') |
23 |
| - parser.add_argument( |
24 |
| - '--nm', required=True, help='The nm binary to run', metavar='PATH') |
25 |
| - parser.add_argument( |
26 |
| - '--sofile', |
27 |
| - required=True, |
28 |
| - help='Shared object file produced by linking command', |
29 |
| - metavar='FILE') |
30 |
| - parser.add_argument( |
31 |
| - '--output', |
32 |
| - required=True, |
33 |
| - help='Final output shared object file', |
34 |
| - metavar='FILE') |
35 |
| - parser.add_argument( |
36 |
| - '--unstrippedsofile', |
37 |
| - required=True, |
38 |
| - help='Unstripped shared object file produced by linking command', |
39 |
| - metavar='FILE') |
40 |
| - args = parser.parse_args() |
41 |
| - |
42 |
| - copy_cmd = ["cp", args.sofile, args.output] |
43 |
| - result = subprocess.call(copy_cmd) |
44 |
| - |
45 |
| - objcopy_cmd = [args.objcopy] |
46 |
| - objcopy_cmd.append('--only-keep-debug') |
47 |
| - objcopy_cmd.append(args.unstrippedsofile) |
48 |
| - objcopy_cmd.append(args.output + '.debug') |
49 |
| - result = subprocess.call(objcopy_cmd) |
50 |
| - |
51 |
| - nm_cmd = subprocess.Popen( |
52 |
| - [args.nm, args.unstrippedsofile, '--format=posix', '--defined-only'], |
53 |
| - stdout=subprocess.PIPE) |
54 |
| - |
55 |
| - awk_cmd = subprocess.Popen(['awk', '{ print $1}'], |
56 |
| - stdin=nm_cmd.stdout, |
57 |
| - stdout=subprocess.PIPE) |
58 |
| - |
59 |
| - dynsym_out = open(args.output + '.dynsyms', 'w') |
60 |
| - sort_cmd = subprocess.Popen(['sort'], stdin=awk_cmd.stdout, stdout=dynsym_out) |
61 |
| - dynsym_out.close() |
62 |
| - |
63 |
| - nm_cmd = subprocess.Popen( |
64 |
| - [args.nm, args.unstrippedsofile, '--format=posix', '--defined-only'], |
65 |
| - stdout=subprocess.PIPE) |
66 |
| - |
67 |
| - awk_cmd = subprocess.Popen( |
68 |
| - ['awk', '{ if ($2 == "T" || $2 == "t" ||' + ' $2 == "D") print $1 }'], |
69 |
| - stdin=nm_cmd.stdout, |
70 |
| - stdout=subprocess.PIPE) |
71 |
| - |
72 |
| - funcsyms_out = open(args.output + '.funcsyms', 'w') |
73 |
| - sort_cmd = subprocess.Popen(['sort'], |
74 |
| - stdin=awk_cmd.stdout, |
75 |
| - stdout=funcsyms_out) |
76 |
| - funcsyms_out.close() |
77 |
| - |
78 |
| - keep_symbols = open(args.output + '.keep_symbols', 'w') |
79 |
| - sort_cmd = subprocess.Popen( |
80 |
| - ['comm', '-13', args.output + '.dynsyms', args.output + '.funcsyms'], |
81 |
| - stdin=awk_cmd.stdout, |
82 |
| - stdout=keep_symbols) |
83 |
| - |
84 |
| - # Ensure that the keep_symbols file is not empty. |
85 |
| - keep_symbols.write("\n") |
86 |
| - keep_symbols.close() |
87 |
| - |
88 |
| - objcopy_cmd = [ |
89 |
| - args.objcopy, '--rename-section', '.debug_frame=saved_debug_frame', |
90 |
| - args.output + '.debug', args.output + ".mini_debuginfo" |
91 |
| - ] |
92 |
| - subprocess.check_call(objcopy_cmd) |
93 |
| - |
94 |
| - objcopy_cmd = [ |
95 |
| - args.objcopy, '-S', '--remove-section', '.gdb_index', '--remove-section', |
96 |
| - '.comment', '--keep-symbols=' + args.output + '.keep_symbols', |
97 |
| - args.output + '.mini_debuginfo' |
98 |
| - ] |
99 |
| - subprocess.check_call(objcopy_cmd) |
100 |
| - |
101 |
| - objcopy_cmd = [ |
102 |
| - args.objcopy, '--rename-section', '.saved_debug_frame=.debug_frame', |
103 |
| - args.output + ".mini_debuginfo" |
104 |
| - ] |
105 |
| - subprocess.check_call(objcopy_cmd) |
106 |
| - |
107 |
| - xz_cmd = ['xz', args.output + '.mini_debuginfo'] |
108 |
| - subprocess.check_call(xz_cmd) |
109 |
| - |
110 |
| - objcopy_cmd = [ |
111 |
| - args.objcopy, '--add-section', |
112 |
| - '.gnu_debugdata=' + args.output + '.mini_debuginfo.xz', args.output |
113 |
| - ] |
114 |
| - subprocess.check_call(objcopy_cmd) |
115 |
| - |
116 |
| - # Clean out scratch files |
117 |
| - rm_cmd = [ |
118 |
| - 'rm', '-f', args.output + '.dynsyms', args.output + '.funcsyms', |
119 |
| - args.output + '.keep_symbols', args.output + '.debug', |
120 |
| - args.output + '.mini_debuginfo', args.output + '.mini_debuginfo.xz' |
121 |
| - ] |
122 |
| - result = subprocess.call(rm_cmd) |
123 |
| - |
124 |
| - return result |
| 17 | + parser = argparse.ArgumentParser(description=__doc__) |
| 18 | + parser.add_argument( |
| 19 | + '--objcopy', required=True, help='The objcopy binary to run', metavar='PATH') |
| 20 | + parser.add_argument('--nm', required=True, help='The nm binary to run', metavar='PATH') |
| 21 | + parser.add_argument( |
| 22 | + '--sofile', |
| 23 | + required=True, |
| 24 | + help='Shared object file produced by linking command', |
| 25 | + metavar='FILE') |
| 26 | + parser.add_argument( |
| 27 | + '--output', required=True, help='Final output shared object file', metavar='FILE') |
| 28 | + parser.add_argument( |
| 29 | + '--unstrippedsofile', |
| 30 | + required=True, |
| 31 | + help='Unstripped shared object file produced by linking command', |
| 32 | + metavar='FILE') |
| 33 | + args = parser.parse_args() |
| 34 | + |
| 35 | + copy_cmd = ["cp", args.sofile, args.output] |
| 36 | + result = subprocess.call(copy_cmd) |
| 37 | + |
| 38 | + objcopy_cmd = [args.objcopy] |
| 39 | + objcopy_cmd.append('--only-keep-debug') |
| 40 | + objcopy_cmd.append(args.unstrippedsofile) |
| 41 | + objcopy_cmd.append(args.output + '.debug') |
| 42 | + result = subprocess.call(objcopy_cmd) |
| 43 | + |
| 44 | + nm_cmd = subprocess.Popen([args.nm, args.unstrippedsofile, '--format=posix', '--defined-only'], |
| 45 | + stdout=subprocess.PIPE) |
| 46 | + |
| 47 | + awk_cmd = subprocess.Popen(['awk', '{ print $1}'], stdin=nm_cmd.stdout, stdout=subprocess.PIPE) |
| 48 | + |
| 49 | + dynsym_out = open(args.output + '.dynsyms', 'w') |
| 50 | + sort_cmd = subprocess.Popen(['sort'], stdin=awk_cmd.stdout, stdout=dynsym_out) |
| 51 | + dynsym_out.close() |
| 52 | + |
| 53 | + nm_cmd = subprocess.Popen([args.nm, args.unstrippedsofile, '--format=posix', '--defined-only'], |
| 54 | + stdout=subprocess.PIPE) |
| 55 | + |
| 56 | + awk_cmd = subprocess.Popen( |
| 57 | + ['awk', '{ if ($2 == "T" || $2 == "t" ||' + ' $2 == "D") print $1 }'], |
| 58 | + stdin=nm_cmd.stdout, |
| 59 | + stdout=subprocess.PIPE) |
| 60 | + |
| 61 | + funcsyms_out = open(args.output + '.funcsyms', 'w') |
| 62 | + sort_cmd = subprocess.Popen(['sort'], stdin=awk_cmd.stdout, stdout=funcsyms_out) |
| 63 | + funcsyms_out.close() |
| 64 | + |
| 65 | + keep_symbols = open(args.output + '.keep_symbols', 'w') |
| 66 | + sort_cmd = subprocess.Popen( |
| 67 | + ['comm', '-13', args.output + '.dynsyms', args.output + '.funcsyms'], |
| 68 | + stdin=awk_cmd.stdout, |
| 69 | + stdout=keep_symbols) |
| 70 | + |
| 71 | + # Ensure that the keep_symbols file is not empty. |
| 72 | + keep_symbols.write("\n") |
| 73 | + keep_symbols.close() |
| 74 | + |
| 75 | + objcopy_cmd = [ |
| 76 | + args.objcopy, '--rename-section', '.debug_frame=saved_debug_frame', args.output + '.debug', |
| 77 | + args.output + ".mini_debuginfo" |
| 78 | + ] |
| 79 | + subprocess.check_call(objcopy_cmd) |
| 80 | + |
| 81 | + objcopy_cmd = [ |
| 82 | + args.objcopy, '-S', '--remove-section', '.gdb_index', '--remove-section', '.comment', |
| 83 | + '--keep-symbols=' + args.output + '.keep_symbols', args.output + '.mini_debuginfo' |
| 84 | + ] |
| 85 | + subprocess.check_call(objcopy_cmd) |
| 86 | + |
| 87 | + objcopy_cmd = [ |
| 88 | + args.objcopy, '--rename-section', '.saved_debug_frame=.debug_frame', |
| 89 | + args.output + ".mini_debuginfo" |
| 90 | + ] |
| 91 | + subprocess.check_call(objcopy_cmd) |
| 92 | + |
| 93 | + xz_cmd = ['xz', args.output + '.mini_debuginfo'] |
| 94 | + subprocess.check_call(xz_cmd) |
| 95 | + |
| 96 | + objcopy_cmd = [ |
| 97 | + args.objcopy, '--add-section', '.gnu_debugdata=' + args.output + '.mini_debuginfo.xz', |
| 98 | + args.output |
| 99 | + ] |
| 100 | + subprocess.check_call(objcopy_cmd) |
| 101 | + |
| 102 | + # Clean out scratch files |
| 103 | + rm_cmd = [ |
| 104 | + 'rm', '-f', args.output + '.dynsyms', args.output + '.funcsyms', |
| 105 | + args.output + '.keep_symbols', args.output + '.debug', args.output + '.mini_debuginfo', |
| 106 | + args.output + '.mini_debuginfo.xz' |
| 107 | + ] |
| 108 | + result = subprocess.call(rm_cmd) |
| 109 | + |
| 110 | + return result |
125 | 111 |
|
126 | 112 |
|
127 | 113 | if __name__ == "__main__":
|
128 |
| - sys.exit(main()) |
| 114 | + sys.exit(main()) |
0 commit comments