Skip to content

Commit e22a4fd

Browse files
committed
lld/mach-o: Make tool scripts from 2124ca1 py2.7-compatible
1 parent 70409b2 commit e22a4fd

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

lld/test/MachO/tools/generate-cfi-funcs.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
The purpose is to produce object-file test inputs to lld with a
55
variety of compact unwind encodings.
66
"""
7+
from __future__ import print_function
78
import random
89
import argparse
910
import string
@@ -18,7 +19,7 @@
1819
saved_regs_combined = list(list(permutations(saved_regs, i))
1920
for i in range(0,6))
2021

21-
def print_function(name: str):
22+
def print_function(name):
2223
global lsda_odds
2324
have_lsda = (random.random() < lsda_odds)
2425
frame_size = random.randint(4, 64) * 16
@@ -32,41 +33,42 @@ def print_function(name: str):
3233
if func_size_high % 0x10 == 0:
3334
func_size_low += 1
3435

35-
print(f"""\
36-
### {name} regs={reg_count} frame={frame_size} lsda={have_lsda} size={func_size}
36+
print("""\
37+
### %s regs=%d frame=%d lsda=%s size=%d
3738
.section __TEXT,__text,regular,pure_instructions
3839
.p2align 4, 0x90
39-
.globl {name}
40-
{name}:
41-
.cfi_startproc""")
40+
.globl %s
41+
%s:
42+
.cfi_startproc""" % (
43+
name, reg_count, frame_size, have_lsda, func_size, name, name))
4244
if have_lsda:
4345
global lsda_n
4446
lsda_n += 1
45-
print(f"""\
47+
print("""\
4648
.cfi_personality 155, ___gxx_personality_v0
47-
.cfi_lsda 16, Lexception{lsda_n}""")
48-
print(f"""\
49-
pushq %rbp
50-
.cfi_def_cfa_offset {frame_size}
51-
.cfi_offset %rbp, {frame_offset+(6*8)}
52-
movq %rsp, %rbp
53-
.cfi_def_cfa_register %rbp""")
49+
.cfi_lsda 16, Lexception%d""" % lsda_n)
50+
print("""\
51+
pushq %%rbp
52+
.cfi_def_cfa_offset %d
53+
.cfi_offset %%rbp, %d
54+
movq %%rsp, %%rbp
55+
.cfi_def_cfa_register %%rbp""" % (frame_size, frame_offset + 6*8))
5456
for i in range(reg_count):
55-
print(f".cfi_offset {regs_saved[i]}, {frame_offset+(i*8)}")
56-
print(f"""\
57-
.fill {func_size - 6}
58-
popq %rbp
57+
print(".cfi_offset %s, %d" % (regs_saved[i], frame_offset+(i*8)))
58+
print("""\
59+
.fill %d
60+
popq %%rbp
5961
retq
6062
.cfi_endproc
61-
""")
63+
""" % (func_size - 6))
6264

6365
if have_lsda:
64-
print(f"""\
66+
print("""\
6567
.section __TEXT,__gcc_except_tab
6668
.p2align 2
67-
Lexception{lsda_n}:
69+
Lexception%d:
6870
.space 0x10
69-
""")
71+
""" % lsda_n)
7072
return func_size
7173

7274
def random_seed():
@@ -103,22 +105,22 @@ def main():
103105
global lsda_odds
104106
lsda_odds = args.lsda / 100.0
105107

106-
print(f"""\
107-
### seed={args.seed} lsda={lsda_odds} p2align={p2align}
108+
print("""\
109+
### seed=%s lsda=%f p2align=%d
108110
.section __TEXT,__text,regular,pure_instructions
109-
.p2align {p2align}, 0x90
110-
""")
111+
.p2align %d, 0x90
112+
""" % (args.seed, lsda_odds, p2align, p2align))
111113

112114
size = 0
113115
base = (1 << p2align)
114116
if args.functions:
115117
for n in range(args.functions):
116-
size += print_function(f"x{size+base:08x}")
118+
size += print_function("x%08x" % (size+base))
117119
else:
118120
while size < (args.pages << 24):
119-
size += print_function(f"x{size+base:08x}")
121+
size += print_function("x%08x" % (size+base))
120122

121-
print(f"""\
123+
print("""\
122124
.section __TEXT,__text,regular,pure_instructions
123125
.globl _main
124126
.p2align 4, 0x90

lld/test/MachO/tools/validate-unwind-info.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Validate compact unwind info by cross checking the llvm-objdump
44
reports of the input object file vs final linked output.
55
"""
6+
from __future__ import print_function
67
import sys
78
import argparse
89
import re
@@ -25,11 +26,11 @@ def main():
2526

2627
object_encodings_list = [(symbol, encoding, personality, lsda)
2728
for symbol, encoding, personality, lsda in
28-
re.findall(rf"start:\s+0x{hex}+\s+(\w+)\s+" +
29-
rf"length:\s+0x{hex}+\s+" +
30-
rf"compact encoding:\s+0x({hex}+)(?:\s+" +
31-
rf"personality function:\s+0x({hex}+)\s+\w+\s+" +
32-
rf"LSDA:\s+0x({hex}+)\s+\w+(?: \+ 0x{hex}+)?)?",
29+
re.findall(r"start:\s+0x%s+\s+(\w+)\s+" % hex +
30+
r"length:\s+0x%s+\s+" % hex +
31+
r"compact encoding:\s+0x(%s+)(?:\s+" % hex +
32+
r"personality function:\s+0x(%s+)\s+\w+\s+" % hex +
33+
r"LSDA:\s+0x(%s+)\s+\w+(?: \+ 0x%s+)?)?" % (hex, hex),
3334
objdump_string, re.DOTALL)]
3435
object_encodings_map = {symbol:encoding
3536
for symbol, encoding, _, _ in object_encodings_list}
@@ -38,21 +39,21 @@ def main():
3839

3940
program_symbols_map = {address:symbol
4041
for address, symbol in
41-
re.findall(rf"^{hex8}({hex8}) g\s+F __TEXT,__text (x\1)$",
42+
re.findall(r"^%s(%s) g\s+F __TEXT,__text (x\1)$" % (hex8, hex8),
4243
objdump_string, re.MULTILINE)}
4344
if not program_symbols_map:
4445
sys.exit("no program symbols found in input")
4546

4647
program_common_encodings = (
47-
re.findall(rf"^\s+encoding\[\d+\]: 0x({hex}+)$",
48+
re.findall(r"^\s+encoding\[\d+\]: 0x(%s+)$" % hex,
4849
objdump_string, re.MULTILINE))
4950
if not program_common_encodings:
5051
sys.exit("no common encodings found in input")
5152

5253
program_encodings_map = {program_symbols_map[address]:encoding
5354
for address, encoding in
54-
re.findall(rf"^\s+\[\d+\]: function offset=0x({hex}+), " +
55-
rf"encoding\[\d+\]=0x({hex}+)$",
55+
re.findall(r"^\s+\[\d+\]: function offset=0x(%s+), " % hex +
56+
r"encoding\[\d+\]=0x(%s+)$" % hex,
5657
objdump_string, re.MULTILINE)}
5758
if not object_encodings_map:
5859
sys.exit("no program encodings found in input")
@@ -66,13 +67,14 @@ def main():
6667
if fold:
6768
del object_encodings_map[symbol]
6869
if args.debug:
69-
print(f"{'delete' if fold else 'retain'} {symbol} with {encoding}")
70+
print("%s %s with %s" % (
71+
'delete' if fold else 'retain', symbol, encoding))
7072
encoding0 = encoding
7173

7274
if program_encodings_map != object_encodings_map:
7375
if args.debug:
74-
pprint(f"program encodings map:\n{program_encodings_map}")
75-
pprint(f"object encodings map:\n{object_encodings_map}")
76+
pprint("program encodings map:\n" + program_encodings_map)
77+
pprint("object encodings map:\n" + object_encodings_map)
7678
sys.exit("encoding maps differ")
7779

7880
# Count frequency of object-file folded encodings
@@ -87,8 +89,8 @@ def main():
8789

8890
if program_common_encodings != encoding_frequencies:
8991
if args.debug:
90-
pprint(f"program common encodings:\n{program_common_encodings}")
91-
pprint(f"object encoding frequencies:\n{encoding_frequencies}")
92+
pprint("program common encodings:\n" + program_common_encodings)
93+
pprint("object encoding frequencies:\n" + encoding_frequencies)
9294
sys.exit("encoding frequencies differ")
9395

9496

0 commit comments

Comments
 (0)