Joshua Hood | 65ecceb | 2022-03-14 16:18:50 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Archive all source files that are references in binary debug info. |
| 8 | |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 9 | Invoked by libfuzzer buildbots. Executes dwarfdump to parse debug info. |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import argparse |
| 15 | import os |
| 16 | import re |
| 17 | import subprocess |
| 18 | import zipfile |
| 19 | |
| 20 | compile_unit_re = re.compile('.*DW_TAG_compile_unit.*') |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 21 | at_name_re = re.compile('.*DW_AT_name.*"(.*)".*') |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def main(): |
| 25 | parser = argparse.ArgumentParser(description="Zip binary sources.") |
| 26 | parser.add_argument('--binary', required=True, |
| 27 | help='binary file to read') |
| 28 | parser.add_argument('--workdir', required=True, |
| 29 | help='working directory to use to resolve relative paths') |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 30 | parser.add_argument('--srcdir', required=True, |
| 31 | help='sources root directory to calculate zip entry names') |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 32 | parser.add_argument('--output', required=True, |
| 33 | help='output zip file name') |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 34 | parser.add_argument('--dwarfdump', required=False, |
| 35 | default='dwarfdump', help='path to dwarfdump utility') |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 36 | args = parser.parse_args() |
| 37 | |
| 38 | # Dump .debug_info section. |
| 39 | out = subprocess.check_output( |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 40 | [args.dwarfdump, '-i', args.binary]) |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 41 | |
| 42 | looking_for_unit = True |
| 43 | compile_units = set() |
| 44 | |
| 45 | # Look for DW_AT_name within DW_TAG_compile_unit |
| 46 | for line in out.splitlines(): |
| 47 | if looking_for_unit and compile_unit_re.match(line): |
| 48 | looking_for_unit = False |
| 49 | elif not looking_for_unit: |
| 50 | match = at_name_re.match(line) |
| 51 | if match: |
| 52 | compile_units.add(match.group(1)) |
| 53 | looking_for_unit = True |
| 54 | |
| 55 | # Zip sources. |
| 56 | with zipfile.ZipFile(args.output, 'w') as z: |
| 57 | for compile_unit in sorted(compile_units): |
| 58 | src_file = os.path.abspath(os.path.join(args.workdir, compile_unit)) |
| 59 | print(src_file) |
aizatsky | 57c14fe | 2016-07-07 22:12:16 | [diff] [blame] | 60 | z.write(src_file, os.path.relpath(src_file, args.srcdir)) |
aizatsky | d892de03 | 2016-06-02 05:49:18 | [diff] [blame] | 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | main() |