xref: /aosp_15_r20/external/toolchain-utils/debug_info_test/check_cus.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1# -*- coding: utf-8 -*-
2# Copyright 2018 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""check compile units."""
7
8
9import os
10import subprocess
11
12import check_ngcc
13
14
15cu_checks = [check_ngcc.not_by_gcc]
16
17
18def check_compile_unit(dso_path, producer, comp_path):
19    """check all compiler flags used to build the compile unit.
20
21    Args:
22      dso_path: path to the elf/dso.
23      producer: DW_AT_producer contains the compiler command line.
24      comp_path: DW_AT_comp_dir + DW_AT_name.
25
26    Returns:
27      A set of failed tests.
28    """
29    failed = set()
30    for c in cu_checks:
31        if not c(dso_path, producer, comp_path):
32            failed.add(c.__module__)
33
34    return failed
35
36
37def check_compile_units(dso_path):
38    """check all compile units in the given dso.
39
40    Args:
41      dso_path: path to the dso.
42
43    Returns:
44      True if everything looks fine otherwise False.
45    """
46
47    failed = set()
48    producer = ""
49    comp_path = ""
50
51    readelf = subprocess.Popen(
52        ["llvm-dwarfdump", "--recurse-depth=0", dso_path],
53        stdout=subprocess.PIPE,
54        stderr=open(os.devnull, "w"),
55        encoding="utf-8",
56    )
57    for l in readelf.stdout:
58        if "DW_TAG_compile_unit" in l:
59            if producer:
60                failed = failed.union(
61                    check_compile_unit(dso_path, producer, comp_path)
62                )
63            producer = ""
64            comp_path = ""
65        elif "DW_AT_producer" in l:
66            producer = l
67        elif "DW_AT_name" in l:
68            comp_path = os.path.join(comp_path, l.split(":")[-1].strip())
69        elif "DW_AT_comp_dir" in l:
70            comp_path = os.path.join(l.split(":")[-1].strip(), comp_path)
71    if producer:
72        failed = failed.union(check_compile_unit(dso_path, producer, comp_path))
73
74    if failed:
75        print("%s failed check: %s" % (dso_path, " ".join(failed)))
76        return False
77
78    return True
79