Newer
Older
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
#!/bin/bash
if [ $# -eq 0 ]; then
echo "No arguments supplied"
exit 1
fi
arch=$1
basepath=$(pwd)
workdir=$basepath/workdir-$arch
ghidra_sources=$workdir/ghidra-sources
ghidra_headers=$workdir/ghidra-headers
clang_dir=$basepath/scripting/ghidra-extraction-scripts/clang-parser
metrics_dir=$workdir/ghidra-cyclomatic
if [ ! -d $metrics_dir ]; then
mkdir $metrics_dir;
fi
cd $ghidra_sources
for filename in *
do
filename=${filename%".c"}
echo "Parsing with clang input: $filename"
# Copy the source and the header file in the parsing directory.
cp $filename.c $clang_dir/
cp $ghidra_headers/$filename.h $clang_dir/
cd $clang_dir
# Remove newlines inhside commands, this will make easier our fixed-point purgin.
clang-format -style='{ColumnLimit: 5000}' -i $filename.c
# Invoke the normalizer pass for the Ghidra decompiled output.
./ghidra-normalizer-wrapper.sh $filename.c
# Parse the source with clang
clang-7 -std=c11 -S -emit-llvm -w $filename.c.normalized.c
# Invoke the analys passes which collect the metrics we are interested in.
revng opt -cyclomatic -cyclomatic-output=$metrics_dir/$filename.c.csv -S -o /dev/null $filename.c.normalized.ll
# Go back to the source directory after cleaning the workdir
rm $filename.c $filename.h $filename.c.normalized.c $filename.c.normalized.ll
# Return to the directory with all the decompiled sources.
cd $ghidra_sources
done;