1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2017, Linaro Limited
5#
6
7import sys
8import re
9
10
11def usage():
12    print("Usage: {0} <section reg exp match> [<skip section>...]".format(
13        sys.argv[0]))
14    sys.exit(1)
15
16
17def main():
18    if len(sys.argv) < 2:
19        usage()
20
21    in_shdr = False
22    section_headers = re.compile("Section Headers:")
23    key_to_flags = re.compile("Key to Flags:")
24    match_rule = re.compile(sys.argv[1])
25    skip_sections = sys.argv[2:]
26
27    for line in sys.stdin:
28        if section_headers.match(line):
29            in_shdr = True
30            continue
31        if key_to_flags.match(line):
32            in_shdr = False
33            continue
34
35        if not in_shdr:
36            continue
37
38        words = line.split()
39
40        if len(words) < 3:
41            continue
42
43        if words[0] == "[":
44            name_offs = 2
45        else:
46            name_offs = 1
47
48        sect_name = words[name_offs]
49        sect_type = words[name_offs + 1]
50
51        if sect_type != "PROGBITS":
52            continue
53
54        if not match_rule.match(sect_name):
55            continue
56
57        if sect_name in skip_sections:
58            continue
59
60        print('\t*({0})'.format(sect_name))
61
62
63if __name__ == "__main__":
64    main()
65