summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/configure/check_debug_ranges.py
blob: f82624c14fa410cb318373a3268b41c02ad7f8ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This script returns the number of items for the DW_AT_ranges corresponding
# to a given compilation unit. This is used as a helper to find a bug in some
# versions of GNU ld.

import re
import subprocess
import sys


def get_range_for(compilation_unit, debug_info):
    """Returns the range offset for a given compilation unit
    in a given debug_info."""
    name = ranges = ""
    search_cu = False
    for nfo in debug_info.splitlines():
        if "DW_TAG_compile_unit" in nfo:
            search_cu = True
        elif "DW_TAG_" in nfo or not nfo.strip():
            if name == compilation_unit and ranges != "":
                return int(ranges, 16)
            name = ranges = ""
            search_cu = False
        if search_cu:
            if "DW_AT_name" in nfo:
                name = nfo.rsplit(None, 1)[1]
            elif "DW_AT_ranges" in nfo:
                ranges = nfo.rsplit(None, 1)[1]
    return None


def get_range_length(range, debug_ranges):
    """Returns the number of items in the range starting at the
    given offset."""
    length = 0
    for line in debug_ranges.splitlines():
        m = re.match("\s*([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)", line)
        if m and int(m.group(1), 16) == range:
            length += 1
    return length


def main(bin, compilation_unit):
    p = subprocess.Popen(
        ["objdump", "-W", bin],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
    )
    (out, err) = p.communicate()
    sections = re.split("\n(Contents of the|The section) ", out)
    debug_info = [s for s in sections if s.startswith(".debug_info")]
    debug_ranges = [s for s in sections if s.startswith(".debug_ranges")]
    if not debug_ranges or not debug_info:
        return 0

    range = get_range_for(compilation_unit, debug_info[0])
    if range is not None:
        return get_range_length(range, debug_ranges[0])

    return -1


if __name__ == "__main__":
    print(main(*sys.argv[1:]))