2 # SPDX-License-Identifier: GPL-2.0
3 # verify_builtin_ranges.awk: Verify address range data for builtin modules
4 # Written by Kris Van Hees <kris.van.hees@oracle.com>
6 # Usage: verify_builtin_ranges.awk modules.builtin.ranges System.map \
7 # modules.builtin vmlinux.map vmlinux.o.map
10 # Return the module name(s) (if any) associated with the given object.
12 # If we have seen this object before, return information from the cache.
13 # Otherwise, retrieve it from the corresponding .cmd file.
15 function get_module_info
(fn
, mod
, obj
, s
) {
19 if (match(fn
, /\
/[^
/]+$
/) ==
0)
24 fn =
substr(fn
, 1, RSTART) "." substr(fn
, RSTART + 1) ".cmd";
25 if (getline s
<fn ==
1) {
26 if (match(s
, /DKBUILD_MODFILE=
['"]+[^'"]+/) > 0) {
27 mod = substr(s, RSTART + 16, RLENGTH - 16);
28 gsub(/['"]/, "", mod
);
29 } else if (match(s
, /RUST_MODFILE=
[^
]+/) > 0)
30 mod =
substr(s
, RSTART + 13, RLENGTH - 13);
32 print "ERROR: Failed to read: " fn
"\n\n" \
33 " For kernels built with O=<objdir>, cd to <objdir>\n" \
34 " and execute this script as ./source/scripts/..." \
42 # A single module (common case) also reflects objects that are not part
43 # of a module. Some of those objects have names that are also a module
44 # name (e.g. core). We check the associated module file name, and if
45 # they do not match, the object is not part of a module.
51 gsub(/([^
/ ]*\
/)+/, "", mod
);
54 # At this point, mod is a single (valid) module name, or a list of
55 # module names (that do not need validation).
61 # Return a representative integer value for a given hexadecimal address.
63 # Since all kernel addresses fall within the same memory region, we can safely
64 # strip off the first 6 hex digits before performing the hex-to-dec conversion,
65 # thereby avoiding integer overflows.
67 function addr2val
(val
) {
69 if (length(val
) ==
16)
71 return strtonum
("0x" val
);
74 # Determine the kernel build directory to use (default is .).
78 print "Syntax: verify_builtin_ranges.awk <ranges-file> <system-map>\n" \
79 " <builtin-file> <vmlinux-map> <vmlinux-o-map>\n" \
86 # (1) Load the built-in module address range data.
94 # (2) Annotate System.map symbols with module names.
100 while (addr
>= mod_eaddr
) {
102 if (sect_symb
!= name
)
105 sect_base = addr
- sect_off
;
107 printf "[%s] BASE (%s) %016x - %016x = %016x\n", sect_name
, sect_symb
, addr
, sect_off
, sect_base
>"/dev/stderr";
118 mod_saddr = strtonum
("0x" $
2) + sect_base
;
119 mod_eaddr = strtonum
("0x" $
3) + sect_base
;
125 printf "[%s] %s from %016x to %016x\n", sect_name
, mod_name
, mod_saddr
, mod_eaddr
>"/dev/stderr";
128 sect_off = strtonum
("0x" $
2);
134 if (addr
>= mod_saddr
&& addr
< mod_eaddr
)
135 sym2mod
[idx
] = mod_name
;
140 # Once we are done annotating the System.map, we no longer need the ranges data.
142 FNR ==
1 && ARGIND ==
3 {
146 # (3) Build a lookup map of built-in module names.
148 # Lines from modules.builtin will be like:
149 # kernel/crypto/lzo-rle.ko
150 # and we record the object name "crypto/lzo-rle".
153 sub(/kernel\
//, ""); # strip off "kernel/" prefix
154 sub(/\.ko$
/, ""); # strip off .ko suffix
160 # (4) Get a list of symbols (per object).
162 # Symbols by object are read from vmlinux.map, with fallback to vmlinux.o.map
163 # if vmlinux is found to have inked in vmlinux.o.
166 # If we were able to get the data we need from vmlinux.map, there is no need to
167 # process vmlinux.o.map.
169 FNR ==
1 && ARGIND ==
5 && total
> 0 {
171 printf "Note: %s is not needed.\n", FILENAME >"/dev/stderr";
175 # First determine whether we are dealing with a GNU ld or LLVM lld linker map.
177 ARGIND >=
4 && FNR ==
1 && NF ==
7 && $
1 ==
"VMA" && $
7 ==
"Symbol" {
182 # (LLD) Convert a section record fronm lld format to ld format.
184 ARGIND >=
4 && map_is_lld
&& NF ==
5 && /[0-9] [^
]+$
/ {
185 $
0 = $
5 " 0x"$
1 " 0x"$
3 " load address 0x"$
2;
188 # (LLD) Convert an object record from lld format to ld format.
190 ARGIND >=
4 && map_is_lld
&& NF ==
5 && $
5 ~
/:\
(/ {
191 if (/\.a\
(/ && !
/ vmlinux\.a\
(/)
196 sub(/ vmlinux\.a\
(/, " ");
197 $
0 =
" "$
6 " 0x"$
1 " 0x"$
3 " " $
5;
200 # (LLD) Convert a symbol record from lld format to ld format.
202 ARGIND >=
4 && map_is_lld
&& NF ==
5 && $
5 ~
/^
[A
-Za
-z_
][A
-Za
-z0
-9_
]*$
/ {
203 $
0 =
" 0x" $
1 " " $
5;
206 # (LLD) We do not need any other ldd linker map records.
208 ARGIND >=
4 && map_is_lld
&& /^
[0-9a
-f
]{16} / {
212 # Handle section records with long section names (spilling onto a 2nd line).
214 ARGIND >=
4 && !map_is_lld
&& NF ==
1 && /^
[^
]/ {
220 # Next section - previous one is done.
222 ARGIND >=
4 && /^
[^
]/ {
226 # Get the (top level) section name.
228 ARGIND >=
4 && /^\.
/ {
229 # Explicitly ignore a few sections that are not relevant here.
230 if ($
1 ~
/^\.orc_
/ || $
1 ~
/_sites$
/ || $
1 ~
/\.percpu
/)
233 # Sections with a 0-address can be ignored as well (in vmlinux.map).
234 if (ARGIND ==
4 && $
2 ~
/^
0x0+$
/)
242 # If we are not currently in a section we care about, ignore records.
248 # Handle object records with long section names (spilling onto a 2nd line).
250 ARGIND >=
4 && /^
[^ \
*]/ && NF ==
1 {
251 # If the section name is long, the remainder of the entry is found on
258 # Objects linked in from static libraries are ignored.
259 # If the object is vmlinux.o, we need to consult vmlinux.o.map for per-object
262 ARGIND ==
4 && /^
[^
]/ && NF ==
4 {
267 if (!
(idx in sect_addend
)) {
268 sect_addend
[idx
] = addr2val
($
2);
270 printf "ADDEND %s = %016x\n", idx
, sect_addend
[idx
] >"/dev/stderr";
272 if ($
4 ==
"vmlinux.o") {
278 # If data from vmlinux.o.map is needed, we only process section and object
279 # records from vmlinux.map to determine which section we need to pay attention
280 # to in vmlinux.o.map. So skip everything else from vmlinux.map.
282 ARGIND ==
4 && need_o_map
{
286 # Get module information for the current object.
288 ARGIND >=
4 && /^
[^
]/ && NF ==
4 {
290 mod_name = get_module_info
($
4);
291 mod_eaddr = addr2val
($
2) + addr2val
($
3);
296 # Process a symbol record.
298 # Evaluate the module information obtained from vmlinux.map (or vmlinux.o.map)
300 # - For all symbols in a given object:
301 # - If the symbol is annotated with the same module name(s) that the object
302 # belongs to, count it as a match.
304 # - If the symbol is known to have duplicates of which at least one is
305 # in a built-in module, disregard it.
306 # - If the symbol us not annotated with any module name(s) AND the
307 # object belongs to built-in modules, count it as missing.
308 # - Otherwise, count it as a mismatch.
310 ARGIND >=
4 && /^
/ && NF ==
2 && $
1 ~
/^
0x
/ {
312 if (!
(idx in sect_addend
))
317 # Handle the rare but annoying case where a 0-size symbol is placed at
318 # the byte *after* the module range. Based on vmlinux.map it will be
319 # considered part of the current object, but it falls just beyond the
320 # module address range. Unfortunately, its address could be at the
321 # start of another built-in module, so the only safe thing to do is to
323 if (mod_name
&& addr == mod_eaddr
)
326 # If we are processing vmlinux.o.map, we need to apply the base address
327 # of the section to the relative address on the record.
330 addr
+= sect_addend
[idx
];
334 if (idx in sym2mod
) {
336 if (sym2mod
[idx
] == mod_name
) {
339 } else if (mod_name ==
"") {
340 print $
2 " in " mod
" (should NOT be)";
343 print $
2 " in " mod
" (should be " mod_name
")";
346 } else if (mod_name
!= "") {
347 print $
2 " should be in " mod_name
;
357 # Issue the comparison report.
361 printf "Verification of %s:\n", ARGV[1];
362 printf " Correct matches: %6d (%d%% of total)\n", matches
, 100 * matches
/ total
;
363 printf " Module matches: %6d (%d%% of matches)\n", mod_matches
, 100 * mod_matches
/ matches
;
364 printf " Mismatches: %6d (%d%% of total)\n", mismatches
, 100 * mismatches
/ total
;
365 printf " Missing: %6d (%d%% of total)\n", missing
, 100 * missing
/ total
;
367 if (mismatches
|| missing
)