|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This tool helps matching dwarf dumps |
| 4 | +# (= the output from running llvm-objdump --dwarf=frames), |
| 5 | +# by address to function names (which are parsed from a normal objdump). |
| 6 | +# The script is used for checking if .cfi_negate_ra_state CFIs |
| 7 | +# are generated by BOLT the same way they are generated by LLVM. |
| 8 | +# The script is called twice in unittests: once with the objdumps of |
| 9 | +# the BOLT input binary, and once with the output binary from BOLT. |
| 10 | +# We output the offsets of .cfi_negate_ra_state instructions from the |
| 11 | +# function's start address to see that BOLT can generate them to the same |
| 12 | +# locations. |
| 13 | +# Because we check the location, this is only useful for testing without |
| 14 | +# optimization flags, so `llvm-bolt input.exe -o output.exe` |
| 15 | + |
| 16 | + |
| 17 | +import argparse |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +import re |
| 21 | + |
| 22 | + |
| 23 | +class NameDwarfPair(object): |
| 24 | + def __init__(self, name, body): |
| 25 | + self.name = name |
| 26 | + self.body = body |
| 27 | + self.finalized = False |
| 28 | + |
| 29 | + def append(self, body_line): |
| 30 | + # only store elements into the body until the first whitespace line is encountered. |
| 31 | + if body_line.isspace(): |
| 32 | + self.finalized = True |
| 33 | + if not self.finalized: |
| 34 | + self.body += body_line |
| 35 | + |
| 36 | + def print(self): |
| 37 | + print(self.name) |
| 38 | + print(self.body) |
| 39 | + |
| 40 | + def parse_negate_offsets(self): |
| 41 | + """ |
| 42 | + Create a list of locations/offsets of the negate_ra_state CFIs in the |
| 43 | + dwarf entry. To find offsets for each, we match the DW_CFA_advance_loc |
| 44 | + entries, and sum up their values. |
| 45 | + """ |
| 46 | + negate_offsets = [] |
| 47 | + loc = 0 |
| 48 | + # TODO: make sure this is not printed in hex |
| 49 | + re_advloc = r"DW_CFA_advance_loc: (\d+)" |
| 50 | + |
| 51 | + for line in self.body.splitlines(): |
| 52 | + # if line matches advance_loc int |
| 53 | + match = re.search(re_advloc, line) |
| 54 | + if match: |
| 55 | + loc += int(match.group(1)) |
| 56 | + if "DW_CFA_AARCH64_negate_ra_state" in line: |
| 57 | + negate_offsets.append(loc) |
| 58 | + |
| 59 | + self.negate_offsets = negate_offsets |
| 60 | + |
| 61 | + def __eq__(self, other): |
| 62 | + return self.name == other.name and self.negate_offsets == other.negate_offsets |
| 63 | + |
| 64 | + |
| 65 | +def extract_function_addresses(objdump): |
| 66 | + """ |
| 67 | + Parse and return address-to-name dictionary from objdump file. |
| 68 | + Function names in the objdump look like this: |
| 69 | + 000123abc <foo>: |
| 70 | + We create a dict from the addr (000123abc), to the name (foo). |
| 71 | + """ |
| 72 | + addr_name_dict = dict() |
| 73 | + re_function = re.compile(r"^([0-9a-fA-F]+)\s<(.*)>:$") |
| 74 | + with open(objdump, "r") as f: |
| 75 | + for line in f.readlines(): |
| 76 | + match = re_function.match(line) |
| 77 | + if not match: |
| 78 | + continue |
| 79 | + m_addr = match.groups()[0] |
| 80 | + m_name = match.groups()[1] |
| 81 | + addr_name_dict[int(m_addr, 16)] = m_name |
| 82 | + |
| 83 | + return addr_name_dict |
| 84 | + |
| 85 | + |
| 86 | +def match_dwarf_to_name(dwarfdump, addr_name_dict): |
| 87 | + """ |
| 88 | + Parse dwarf dump, and match names to blocks using the dict from the objdump. |
| 89 | + Return a list of NameDwarfPairs. |
| 90 | + The matched lines look like this: |
| 91 | + 000123 000456 000789 FDE cie=000000 pc=0123abc...0456def |
| 92 | + We do not have the function name for this, only the PC range it applies to. |
| 93 | + We match the pc=0123abc (the start address), and find the matching name from |
| 94 | + the addr_name_dict. |
| 95 | + The resultint NameDwarfPair will hold the lines this header applied to, and |
| 96 | + instead of the header with the addresses, it will just have the function name. |
| 97 | + """ |
| 98 | + re_address_line = re.compile(r".*pc=([0-9a-fA-F]+)\.\.\.([0-9a-fA-F]+)") |
| 99 | + with open(dwarfdump, "r") as dw: |
| 100 | + functions = [] |
| 101 | + for line in dw.readlines(): |
| 102 | + match = re_address_line.match(line) |
| 103 | + if not match: |
| 104 | + if len(functions) > 0: |
| 105 | + functions[-1].append(line) |
| 106 | + continue |
| 107 | + pc_start_address = match.groups()[0] |
| 108 | + name = addr_name_dict.get(int(pc_start_address, 16)) |
| 109 | + functions.append(NameDwarfPair(name, "")) |
| 110 | + |
| 111 | + return functions |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + parser = argparse.ArgumentParser() |
| 116 | + parser.add_argument("objdump", help="Objdump file") |
| 117 | + parser.add_argument( |
| 118 | + "dwarfdump", help="dwarf dump file created with 'llvm-objdump --dwarf=frames'" |
| 119 | + ) |
| 120 | + parser.add_argument("function", help="Function to search CFIs in.") |
| 121 | + |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + addr_name_dict = extract_function_addresses(args.objdump) |
| 125 | + functions = match_dwarf_to_name(args.dwarfdump, addr_name_dict) |
| 126 | + |
| 127 | + for f in functions: |
| 128 | + if f.name == args.function: |
| 129 | + f.parse_negate_offsets() |
| 130 | + print(f.negate_offsets) |
| 131 | + break |
| 132 | + else: |
| 133 | + print(f"{args.function} not found") |
| 134 | + exit(-1) |
| 135 | + |
| 136 | + |
| 137 | +main() |
0 commit comments