1# This awk script expects to get command-line files that are each
2# the output of 'readelf -l' on a single shared object.
3# But the first file should contain just "execstack-no" or "execstack-yes",
4# indicating what the default is in the absence of PT_GNU_STACK.
5# It exits successfully (0) if none indicated executable stack.
6# It fails (1) if any did indicate executable stack.
7# It fails (2) if the input did not take the expected form.
8
9BEGIN {
10  result = sanity = 0; default_exec = -1;
11  split(xfail, xfails, " ");
12  for (x in xfails)
13    expected_fails[xfails[x] ".phdr"] = 1;
14}
15
16/^execstack-no$/ { default_exec = 0; next }
17/^execstack-yes$/ { default_exec = 1; next }
18
19function check_one(name) {
20  if (default_exec == -1) {
21    print "*** missing execstack-default file?";
22    result = 2;
23  }
24
25  n = split(name, parts, "/");
26  basename = parts[n];
27  expected_fail = basename in expected_fails;
28
29  if (!sanity) {
30    print name ": *** input did not look like readelf -l output";
31    result = 2;
32  } else if (stack_line) {
33    if (stack_line ~ /^.*RW .*$/) {
34      print name ": OK";
35    } else if (stack_line ~ /^.*E.*$/) {
36      if (expected_fail) {
37	print name ": *** executable stack signaled, expected";
38      } else {
39	print name ": *** executable stack signaled";
40	result = result ? result : 1;
41      }
42    }
43  } else if (default_exec) {
44    if (expected_fail) {
45      print name ": *** no PT_GNU_STACK entry, expected";
46    } else {
47      print name ": *** no PT_GNU_STACK entry";
48      result = result ? result : 1;
49    }
50  } else {
51    print name ": no PT_GNU_STACK but default is OK";
52  }
53
54  sanity = 0;
55}
56
57FILENAME != lastfile {
58  if (lastfile)
59    check_one(lastfile);
60  lastfile = FILENAME;
61}
62
63$1 == "Type" && $7 == "Flg" { sanity = 1; stack_line = "" }
64$1 == "GNU_STACK" { stack_line = $0 }
65
66END {
67  check_one(lastfile);
68  exit(result);
69}
70