[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / scripts / localplt.awk
1 # This awk script expects to get command-line files that are each
2 # the output of 'readelf -WSdr' on a single shared object, and named
3 # .../NAME.jmprel where NAME is the unadorned file name of the shared object.
4 # It writes "NAME: SYMBOL" for each PLT entry in NAME that refers to a
5 # symbol defined in the same object.
6
7 BEGIN {
8   result = 0;
9   pltrelsize = -1;
10 }
11
12 FILENAME != lastfile {
13   if (lastfile && jmprel_offset == 0 && rela_offset == 0 && rel_offset == 0) {
14     print FILENAME ": *** failed to find expected output (readelf -WSdr)";
15     result = 2;
16   }
17   if (pltrelsz > 0 && jmprel_offset == -1) {
18     print FILENAME ": Could not find section for DT_JMPREL";
19     result = 2;
20   }
21   lastfile = FILENAME;
22   jmprel_offset = 0;
23   rela_offset = 0;
24   rel_offset = 0;
25   pltrelsz = -1;
26   delete section_offset_by_address;
27 }
28
29 /^Section Headers:/ { in_shdrs = 1; next }
30 in_shdrs && !/^ +\[/ { in_shdrs = 0 }
31
32 in_shdrs && /^ +\[/ { sub(/\[ +/, "[") }
33 in_shdrs {
34   address = strtonum("0x" $4);
35   offset = strtonum("0x" $5);
36   section_offset_by_address[address] = offset;
37 }
38
39 in_shdrs { next }
40
41 $1 == "Offset" && $2 == "Info" { in_relocs = 1; next }
42 NF == 0 { in_relocs = 0 }
43
44 in_relocs && relocs_offset == jmprel_offset && NF >= 5 {
45   # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
46   # value, but rather as the resolver symbol followed by ().
47   if ($4 ~ /\(\)/) {
48     print whatfile, gensub(/@.*/, "", "g", $5)
49   } else {
50     symval = strtonum("0x" $4);
51     if (symval != 0)
52       print whatfile, gensub(/@.*/, "", "g", $5)
53   }
54 }
55
56 in_relocs && relocs_offset == rela_offset && NF >= 5 {
57   # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
58   # value, but rather as the resolver symbol followed by ().
59   if ($4 ~ /\(\)/) {
60     print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
61   } else {
62     symval = strtonum("0x" $4);
63     if (symval != 0)
64       print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
65   }
66 }
67
68 in_relocs && relocs_offset == rel_offset && NF >= 5 {
69   # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
70   # value, but rather as the resolver symbol followed by ().
71   if ($4 ~ /\(\)/) {
72     print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
73   } else {
74     symval = strtonum("0x" $4);
75     if (symval != 0)
76       print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
77   }
78 }
79
80 in_relocs { next }
81
82 $1 == "Relocation" && $2 == "section" && $5 == "offset" {
83   relocs_offset = strtonum($6);
84   whatfile = gensub(/^.*\/([^/]+)\.jmprel$/, "\\1:", 1, FILENAME);
85   next
86 }
87
88 $2 == "(JMPREL)" {
89   jmprel_addr = strtonum($3);
90   if (jmprel_addr in section_offset_by_address) {
91     jmprel_offset = section_offset_by_address[jmprel_addr];
92   } else {
93     jmprel_offset = -1
94   }
95   next
96 }
97
98 $2 == "(PLTRELSZ)" {
99   pltrelsz = strtonum($3);
100   next
101 }
102
103 $2 == "(RELA)" {
104   rela_addr = strtonum($3);
105   if (rela_addr in section_offset_by_address) {
106     rela_offset = section_offset_by_address[rela_addr];
107   } else {
108     print FILENAME ": *** DT_RELA does not match any section's address";
109     result = 2;
110   }
111   next
112 }
113
114 $2 == "(REL)" {
115   rel_addr = strtonum($3);
116   if (rel_addr in section_offset_by_address) {
117     rel_offset = section_offset_by_address[rel_addr];
118   } else {
119     print FILENAME ": *** DT_REL does not match any section's address";
120     result = 2;
121   }
122   next
123 }
124 END { exit(result) }