Skip to content

Commit 5d3c541

Browse files
authored
Merge pull request #110 from daverodgman/mtest-misc
Misc improvements to mtest
2 parents 00e3662 + c97b6d4 commit 5d3c541

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

tools/bin/mtest

+42-21
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ def clean():
133133
def toolchain(target):
134134
# basic CFLAGS setup
135135
c_flags = f"-I{ROOT}/include"
136-
if "-O" not in target.cflags: c_flags += " -Os"
136+
if "-O" not in target.cflags:
137+
if args.size_tfm and target.compiler in { "clang", "armclang" }:
138+
c_flags += " -Oz"
139+
else:
140+
c_flags += " -Os"
137141
if "-std" not in target.cflags: c_flags += " -std=c99"
138142
if args.no_warnings:
139143
c_flags += " -Wno-everything"
@@ -375,18 +379,26 @@ def disassemble(target):
375379
objdump = toolchain(target)["OBJDUMP"]
376380
objfiles = ["library/libmbedcrypto.a", "library/libmbedtls.a", "library/libmbedx509.a" ] + args.build_targets
377381
objfiles = [f"{ROOT}/{f}" for f in objfiles if os.path.exists(f"{ROOT}/{f}")]
378-
output, _ = subrun([objdump] + objfiles + [f"--disassemble={args.disassemble}", "--visualize-jumps=extended-color"], silent=True)
379-
# extract the relevant bit of output
380-
found = False
381-
diss = []
382-
for l in output.splitlines():
383-
if l.strip().endswith(f"{args.disassemble}>:"):
384-
found = True
385-
if found and re.search(r"[\w_]+.o:\s+file format", l): break
386-
if found and l.strip().startswith("Disassembly of section"): break
387-
if found: diss.append(l)
388-
while diss[-1] == "": diss = diss[:-1]
389-
return "\n".join(diss)
382+
if args.disassemble == True:
383+
disarg = "--disassemble"
384+
else:
385+
disarg = f"--disassemble={args.disassemble}"
386+
output, _ = subrun([objdump] + objfiles + [disarg, "--visualize-jumps=extended-color"], silent=True)
387+
if args.disassemble == True:
388+
# return complete output
389+
return output
390+
else:
391+
# extract the relevant bit of output
392+
found = False
393+
diss = []
394+
for l in output.splitlines():
395+
if l.strip().endswith(f"{args.disassemble}>:"):
396+
found = True
397+
if found and re.search(r"[\w_]+.o:\s+file format", l): break
398+
if found and l.strip().startswith("Disassembly of section"): break
399+
if found: diss.append(l)
400+
while len(diss) > 0 and diss[-1] == "": diss = diss[:-1]
401+
return "\n".join(diss)
390402

391403

392404
def build(target, tests):
@@ -437,7 +449,12 @@ def size(target):
437449
size_target = "library/libmbedcrypto.a"
438450
log(f'{t["SIZE"]} {size_target}')
439451
size = subrun([t["SIZE"], size_target], silent=True)[0]
440-
log(size)
452+
lines = size.splitlines()
453+
# sort modules by text+data size
454+
lines = lines[:1] + sorted(lines[1:], key=lambda l:int(l.split()[0]) + int(l.split()[1]))
455+
for l in lines:
456+
# skip modules with zero size
457+
if not re.search(r"^\s*0\s+0\s+0\s+0\s+0\s", l): log(l)
441458
size_data = [x.split()[0:3] for x in size.splitlines()[1:]]
442459
text_sum = sum(int(x[0]) for x in size_data)
443460
data_sum = sum(int(x[1]) for x in size_data)
@@ -507,13 +524,17 @@ mtest base64 armv6-thumb1 aarch64 x86_64
507524
Run alignment test suite on key targets:
508525
mtest -k alignment
509526
510-
Report library size, as built with TF-M configuration for armv8 thumb2:
527+
Report library size, as built by armclang -Oz with TF-M configuration for armv8 thumb2:
511528
mtest -S
529+
mtest -S clang # as above, but use clang instead of the default armclang
512530
513531
Build aes.o multiple times, with every combination of the given options:
514532
mtest aes.o MBEDTLS_AES_SETKEY_ENC_ALT,MBEDTLS_AES_DECRYPT_ALT,MBEDTLS_AES_ROM_TABLES,MBEDTLS_AES_ENCRYPT_ALT,MBEDTLS_AES_SETKEY_DEC_ALT,MBEDTLS_AES_FEWER_TABLES,MBEDTLS_PADLOCK_C
515533
516-
Disassemble mbedtls_xor for arm, built with clang -Os
534+
Disassemble cmac.o
535+
mtest cmac.o -d
536+
537+
Disassemble mbedtls_xor, built with clang -Os for arm
517538
mtest -d mbedtls_xor -Os clang arm
518539
'''
519540

@@ -536,9 +557,9 @@ Build targets: build the library by default, or files (e.g. aes.o), programs (e.
536557
parser.add_argument('-A', '--asan', action="store_true", default=False, help="make an ASAN build")
537558
parser.add_argument('-W', '--no-warnings', action="store_true", default=False, help="disable warnings")
538559
parser.add_argument('-C', '--no-clean', action="store_true", default=False, help="don't run make clean (if building for a single target)")
539-
parser.add_argument('-d', '--disassemble', metavar='function', action="store", default=None, help="disassemble specified function")
560+
parser.add_argument('-d', '--disassemble', metavar='function', action="store", nargs='?', const=True, default=None, help="disassemble specified function (or everything if no function specified)")
540561
parser.add_argument('-s', '--size', action="store_true", default=False, help="display size of library / given build target")
541-
parser.add_argument('-S', '--size-tfm', action="store_true", default=False, help=f"as --size, but use the TF-M config file and build with {SIZE_TFM_COMPILER} -Os for Cortex-M33")
562+
parser.add_argument('-S', '--size-tfm', action="store_true", default=False, help=f"as --size, but use the TF-M config file and build with {SIZE_TFM_COMPILER} -Oz for Cortex-M33")
542563
parser.add_argument('-j', '--jobs', nargs='?', const=-1, metavar='N', action="store", type=int, help="enable parallel builds, like 'make -j'. Alternatively, set the MAKEFLAGS environment variable")
543564
parser.add_argument('rest', metavar='args', nargs='*', help=REST_HELP)
544565
# handle -MBEDTLS_OPTION style args manually because they confuse argparse w.r.t. -M for --memsan
@@ -585,18 +606,18 @@ Build targets: build the library by default, or files (e.g. aes.o), programs (e.
585606
if args.size_tfm:
586607
config.append("tfm_mbedcrypto_config_profile_medium.h")
587608
config.append("crypto_config_profile_medium.h")
588-
for a in args.rest:
609+
for a in list(args.rest):
589610
if a.endswith(".h") and os.path.exists(a):
590611
# arbitrary .h file
591612
config.append(a)
592613
args.rest.remove(a)
593-
break
614+
continue
594615
x = a.split("/")[-1] if "configs/" in a else a
595616
if x in supported_configs or f"{x}.h" in supported_configs:
596617
# file in configs directory, or a known argument to set_config.py
597618
config.append(f"{x}.h" if f"{x}.h" in supported_configs else x)
598619
args.rest.remove(a)
599-
break
620+
continue
600621

601622
# filter out non-config options, and non-flags
602623
args.rest = [x for x in args.rest if not x.startswith("-") and not x.startswith("MBEDTLS_") and not x.startswith("PSA_WANT_")]

0 commit comments

Comments
 (0)