1 #!/usr/bin/gawk -f 1 #!/usr/bin/gawk -f 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 # verify_builtin_ranges.awk: Verify address ra 3 # verify_builtin_ranges.awk: Verify address range data for builtin modules 4 # Written by Kris Van Hees <kris.van.hees@oracl 4 # Written by Kris Van Hees <kris.van.hees@oracle.com> 5 # 5 # 6 # Usage: verify_builtin_ranges.awk modules.bui 6 # Usage: verify_builtin_ranges.awk modules.builtin.ranges System.map \ 7 # modules.bui 7 # modules.builtin vmlinux.map vmlinux.o.map 8 # 8 # 9 9 10 # Return the module name(s) (if any) associate 10 # Return the module name(s) (if any) associated with the given object. 11 # 11 # 12 # If we have seen this object before, return i 12 # If we have seen this object before, return information from the cache. 13 # Otherwise, retrieve it from the correspondin 13 # Otherwise, retrieve it from the corresponding .cmd file. 14 # 14 # 15 function get_module_info(fn, mod, obj, s) { 15 function get_module_info(fn, mod, obj, s) { 16 if (fn in omod) 16 if (fn in omod) 17 return omod[fn]; 17 return omod[fn]; 18 18 19 if (match(fn, /\/[^/]+$/) == 0) 19 if (match(fn, /\/[^/]+$/) == 0) 20 return ""; 20 return ""; 21 21 22 obj = fn; 22 obj = fn; 23 mod = ""; 23 mod = ""; 24 fn = substr(fn, 1, RSTART) "." substr( 24 fn = substr(fn, 1, RSTART) "." substr(fn, RSTART + 1) ".cmd"; 25 if (getline s <fn == 1) { 25 if (getline s <fn == 1) { 26 if (match(s, /DKBUILD_MODFILE= 26 if (match(s, /DKBUILD_MODFILE=['"]+[^'"]+/) > 0) { 27 mod = substr(s, RSTART 27 mod = substr(s, RSTART + 16, RLENGTH - 16); 28 gsub(/['"]/, "", mod); 28 gsub(/['"]/, "", mod); 29 } else if (match(s, /RUST_MODF 29 } else if (match(s, /RUST_MODFILE=[^ ]+/) > 0) 30 mod = substr(s, RSTART 30 mod = substr(s, RSTART + 13, RLENGTH - 13); 31 } else { 31 } else { 32 print "ERROR: Failed to read: 32 print "ERROR: Failed to read: " fn "\n\n" \ 33 " For kernels built wit 33 " For kernels built with O=<objdir>, cd to <objdir>\n" \ 34 " and execute this scri 34 " and execute this script as ./source/scripts/..." \ 35 >"/dev/stderr"; 35 >"/dev/stderr"; 36 close(fn); 36 close(fn); 37 total = 0; 37 total = 0; 38 exit(1); 38 exit(1); 39 } 39 } 40 close(fn); 40 close(fn); 41 41 42 # A single module (common case) also r 42 # A single module (common case) also reflects objects that are not part 43 # of a module. Some of those objects 43 # of a module. Some of those objects have names that are also a module 44 # name (e.g. core). We check the asso 44 # name (e.g. core). We check the associated module file name, and if 45 # they do not match, the object is not 45 # they do not match, the object is not part of a module. 46 if (mod !~ / /) { 46 if (mod !~ / /) { 47 if (!(mod in mods)) 47 if (!(mod in mods)) 48 mod = ""; 48 mod = ""; 49 } 49 } 50 50 51 gsub(/([^/ ]*\/)+/, "", mod); 51 gsub(/([^/ ]*\/)+/, "", mod); 52 gsub(/-/, "_", mod); 52 gsub(/-/, "_", mod); 53 53 54 # At this point, mod is a single (vali 54 # At this point, mod is a single (valid) module name, or a list of 55 # module names (that do not need valid 55 # module names (that do not need validation). 56 omod[obj] = mod; 56 omod[obj] = mod; 57 57 58 return mod; 58 return mod; 59 } 59 } 60 60 61 # Return a representative integer value for a 61 # Return a representative integer value for a given hexadecimal address. 62 # 62 # 63 # Since all kernel addresses fall within the s 63 # Since all kernel addresses fall within the same memory region, we can safely 64 # strip off the first 6 hex digits before perf 64 # strip off the first 6 hex digits before performing the hex-to-dec conversion, 65 # thereby avoiding integer overflows. 65 # thereby avoiding integer overflows. 66 # 66 # 67 function addr2val(val) { 67 function addr2val(val) { 68 sub(/^0x/, "", val); 68 sub(/^0x/, "", val); 69 if (length(val) == 16) 69 if (length(val) == 16) 70 val = substr(val, 5); 70 val = substr(val, 5); 71 return strtonum("0x" val); 71 return strtonum("0x" val); 72 } 72 } 73 73 74 # Determine the kernel build directory to use 74 # Determine the kernel build directory to use (default is .). 75 # 75 # 76 BEGIN { 76 BEGIN { 77 if (ARGC < 6) { 77 if (ARGC < 6) { 78 print "Syntax: verify_builtin_ 78 print "Syntax: verify_builtin_ranges.awk <ranges-file> <system-map>\n" \ 79 " <builtin-file 79 " <builtin-file> <vmlinux-map> <vmlinux-o-map>\n" \ 80 >"/dev/stderr"; 80 >"/dev/stderr"; 81 total = 0; 81 total = 0; 82 exit(1); 82 exit(1); 83 } 83 } 84 } 84 } 85 85 86 # (1) Load the built-in module address range d 86 # (1) Load the built-in module address range data. 87 # 87 # 88 ARGIND == 1 { 88 ARGIND == 1 { 89 ranges[FNR] = $0; 89 ranges[FNR] = $0; 90 rcnt++; 90 rcnt++; 91 next; 91 next; 92 } 92 } 93 93 94 # (2) Annotate System.map symbols with module 94 # (2) Annotate System.map symbols with module names. 95 # 95 # 96 ARGIND == 2 { 96 ARGIND == 2 { 97 addr = addr2val($1); 97 addr = addr2val($1); 98 name = $3; 98 name = $3; 99 99 100 while (addr >= mod_eaddr) { 100 while (addr >= mod_eaddr) { 101 if (sect_symb) { 101 if (sect_symb) { 102 if (sect_symb != name) 102 if (sect_symb != name) 103 next; 103 next; 104 104 105 sect_base = addr - sec 105 sect_base = addr - sect_off; 106 if (dbg) 106 if (dbg) 107 printf "[%s] B 107 printf "[%s] BASE (%s) %016x - %016x = %016x\n", sect_name, sect_symb, addr, sect_off, sect_base >"/dev/stderr"; 108 sect_symb = 0; 108 sect_symb = 0; 109 } 109 } 110 110 111 if (++ridx > rcnt) 111 if (++ridx > rcnt) 112 break; 112 break; 113 113 114 $0 = ranges[ridx]; 114 $0 = ranges[ridx]; 115 sub(/-/, " "); 115 sub(/-/, " "); 116 if ($4 != "=") { 116 if ($4 != "=") { 117 sub(/-/, " "); 117 sub(/-/, " "); 118 mod_saddr = strtonum(" 118 mod_saddr = strtonum("0x" $2) + sect_base; 119 mod_eaddr = strtonum(" 119 mod_eaddr = strtonum("0x" $3) + sect_base; 120 $1 = $2 = $3 = ""; 120 $1 = $2 = $3 = ""; 121 sub(/^ +/, ""); 121 sub(/^ +/, ""); 122 mod_name = $0; 122 mod_name = $0; 123 123 124 if (dbg) 124 if (dbg) 125 printf "[%s] % 125 printf "[%s] %s from %016x to %016x\n", sect_name, mod_name, mod_saddr, mod_eaddr >"/dev/stderr"; 126 } else { 126 } else { 127 sect_name = $1; 127 sect_name = $1; 128 sect_off = strtonum("0 128 sect_off = strtonum("0x" $2); 129 sect_symb = $5; 129 sect_symb = $5; 130 } 130 } 131 } 131 } 132 132 133 idx = addr"-"name; 133 idx = addr"-"name; 134 if (addr >= mod_saddr && addr < mod_ea 134 if (addr >= mod_saddr && addr < mod_eaddr) 135 sym2mod[idx] = mod_name; 135 sym2mod[idx] = mod_name; 136 136 137 next; 137 next; 138 } 138 } 139 139 140 # Once we are done annotating the System.map, 140 # Once we are done annotating the System.map, we no longer need the ranges data. 141 # 141 # 142 FNR == 1 && ARGIND == 3 { 142 FNR == 1 && ARGIND == 3 { 143 delete ranges; 143 delete ranges; 144 } 144 } 145 145 146 # (3) Build a lookup map of built-in module na 146 # (3) Build a lookup map of built-in module names. 147 # 147 # 148 # Lines from modules.builtin will be like: 148 # Lines from modules.builtin will be like: 149 # kernel/crypto/lzo-rle.ko 149 # kernel/crypto/lzo-rle.ko 150 # and we record the object name "crypto/lzo-rl 150 # and we record the object name "crypto/lzo-rle". 151 # 151 # 152 ARGIND == 3 { 152 ARGIND == 3 { 153 sub(/kernel\//, ""); 153 sub(/kernel\//, ""); # strip off "kernel/" prefix 154 sub(/\.ko$/, ""); 154 sub(/\.ko$/, ""); # strip off .ko suffix 155 155 156 mods[$1] = 1; 156 mods[$1] = 1; 157 next; 157 next; 158 } 158 } 159 159 160 # (4) Get a list of symbols (per object). 160 # (4) Get a list of symbols (per object). 161 # 161 # 162 # Symbols by object are read from vmlinux.map, 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 163 # if vmlinux is found to have inked in vmlinux.o. 164 # 164 # 165 165 166 # If we were able to get the data we need from 166 # If we were able to get the data we need from vmlinux.map, there is no need to 167 # process vmlinux.o.map. 167 # process vmlinux.o.map. 168 # 168 # 169 FNR == 1 && ARGIND == 5 && total > 0 { 169 FNR == 1 && ARGIND == 5 && total > 0 { 170 if (dbg) 170 if (dbg) 171 printf "Note: %s is not needed 171 printf "Note: %s is not needed.\n", FILENAME >"/dev/stderr"; 172 exit; 172 exit; 173 } 173 } 174 174 175 # First determine whether we are dealing with 175 # First determine whether we are dealing with a GNU ld or LLVM lld linker map. 176 # 176 # 177 ARGIND >= 4 && FNR == 1 && NF == 7 && $1 == "V 177 ARGIND >= 4 && FNR == 1 && NF == 7 && $1 == "VMA" && $7 == "Symbol" { 178 map_is_lld = 1; 178 map_is_lld = 1; 179 next; 179 next; 180 } 180 } 181 181 182 # (LLD) Convert a section record fronm lld for 182 # (LLD) Convert a section record fronm lld format to ld format. 183 # 183 # 184 ARGIND >= 4 && map_is_lld && NF == 5 && /[0-9] 184 ARGIND >= 4 && map_is_lld && NF == 5 && /[0-9] [^ ]+$/ { 185 $0 = $5 " 0x"$1 " 0x"$3 " load address 185 $0 = $5 " 0x"$1 " 0x"$3 " load address 0x"$2; 186 } 186 } 187 187 188 # (LLD) Convert an object record from lld form 188 # (LLD) Convert an object record from lld format to ld format. 189 # 189 # 190 ARGIND >= 4 && map_is_lld && NF == 5 && $5 ~ / 190 ARGIND >= 4 && map_is_lld && NF == 5 && $5 ~ /:\(/ { 191 if (/\.a\(/ && !/ vmlinux\.a\(/) 191 if (/\.a\(/ && !/ vmlinux\.a\(/) 192 next; 192 next; 193 193 194 gsub(/\)/, ""); 194 gsub(/\)/, ""); 195 sub(/:\(/, " "); 195 sub(/:\(/, " "); 196 sub(/ vmlinux\.a\(/, " "); 196 sub(/ vmlinux\.a\(/, " "); 197 $0 = " "$6 " 0x"$1 " 0x"$3 " " $5; 197 $0 = " "$6 " 0x"$1 " 0x"$3 " " $5; 198 } 198 } 199 199 200 # (LLD) Convert a symbol record from lld forma 200 # (LLD) Convert a symbol record from lld format to ld format. 201 # 201 # 202 ARGIND >= 4 && map_is_lld && NF == 5 && $5 ~ / 202 ARGIND >= 4 && map_is_lld && NF == 5 && $5 ~ /^[A-Za-z_][A-Za-z0-9_]*$/ { 203 $0 = " 0x" $1 " " $5; 203 $0 = " 0x" $1 " " $5; 204 } 204 } 205 205 206 # (LLD) We do not need any other ldd linker ma 206 # (LLD) We do not need any other ldd linker map records. 207 # 207 # 208 ARGIND >= 4 && map_is_lld && /^[0-9a-f]{16} / 208 ARGIND >= 4 && map_is_lld && /^[0-9a-f]{16} / { 209 next; 209 next; 210 } 210 } 211 211 212 # Handle section records with long section nam 212 # Handle section records with long section names (spilling onto a 2nd line). 213 # 213 # 214 ARGIND >= 4 && !map_is_lld && NF == 1 && /^[^ 214 ARGIND >= 4 && !map_is_lld && NF == 1 && /^[^ ]/ { 215 s = $0; 215 s = $0; 216 getline; 216 getline; 217 $0 = s " " $0; 217 $0 = s " " $0; 218 } 218 } 219 219 220 # Next section - previous one is done. 220 # Next section - previous one is done. 221 # 221 # 222 ARGIND >= 4 && /^[^ ]/ { 222 ARGIND >= 4 && /^[^ ]/ { 223 sect = 0; 223 sect = 0; 224 } 224 } 225 225 226 # Get the (top level) section name. 226 # Get the (top level) section name. 227 # 227 # 228 ARGIND >= 4 && /^\./ { 228 ARGIND >= 4 && /^\./ { 229 # Explicitly ignore a few sections tha 229 # Explicitly ignore a few sections that are not relevant here. 230 if ($1 ~ /^\.orc_/ || $1 ~ /_sites$/ | 230 if ($1 ~ /^\.orc_/ || $1 ~ /_sites$/ || $1 ~ /\.percpu/) 231 next; 231 next; 232 232 233 # Sections with a 0-address can be ign 233 # Sections with a 0-address can be ignored as well (in vmlinux.map). 234 if (ARGIND == 4 && $2 ~ /^0x0+$/) 234 if (ARGIND == 4 && $2 ~ /^0x0+$/) 235 next; 235 next; 236 236 237 sect = $1; 237 sect = $1; 238 238 239 next; 239 next; 240 } 240 } 241 241 242 # If we are not currently in a section we care 242 # If we are not currently in a section we care about, ignore records. 243 # 243 # 244 !sect { 244 !sect { 245 next; 245 next; 246 } 246 } 247 247 248 # Handle object records with long section name 248 # Handle object records with long section names (spilling onto a 2nd line). 249 # 249 # 250 ARGIND >= 4 && /^ [^ \*]/ && NF == 1 { 250 ARGIND >= 4 && /^ [^ \*]/ && NF == 1 { 251 # If the section name is long, the rem 251 # If the section name is long, the remainder of the entry is found on 252 # the next line. 252 # the next line. 253 s = $0; 253 s = $0; 254 getline; 254 getline; 255 $0 = s " " $0; 255 $0 = s " " $0; 256 } 256 } 257 257 258 # Objects linked in from static libraries are 258 # Objects linked in from static libraries are ignored. 259 # If the object is vmlinux.o, we need to consu 259 # If the object is vmlinux.o, we need to consult vmlinux.o.map for per-object 260 # symbol information 260 # symbol information 261 # 261 # 262 ARGIND == 4 && /^ [^ ]/ && NF == 4 { 262 ARGIND == 4 && /^ [^ ]/ && NF == 4 { 263 if ($4 ~ /\.a\(/) 263 if ($4 ~ /\.a\(/) 264 next; 264 next; 265 265 266 idx = sect":"$1; 266 idx = sect":"$1; 267 if (!(idx in sect_addend)) { 267 if (!(idx in sect_addend)) { 268 sect_addend[idx] = addr2val($2 268 sect_addend[idx] = addr2val($2); 269 if (dbg) 269 if (dbg) 270 printf "ADDEND %s = %0 270 printf "ADDEND %s = %016x\n", idx, sect_addend[idx] >"/dev/stderr"; 271 } 271 } 272 if ($4 == "vmlinux.o") { 272 if ($4 == "vmlinux.o") { 273 need_o_map = 1; 273 need_o_map = 1; 274 next; 274 next; 275 } 275 } 276 } 276 } 277 277 278 # If data from vmlinux.o.map is needed, we onl 278 # If data from vmlinux.o.map is needed, we only process section and object 279 # records from vmlinux.map to determine which 279 # records from vmlinux.map to determine which section we need to pay attention 280 # to in vmlinux.o.map. So skip everything els 280 # to in vmlinux.o.map. So skip everything else from vmlinux.map. 281 # 281 # 282 ARGIND == 4 && need_o_map { 282 ARGIND == 4 && need_o_map { 283 next; 283 next; 284 } 284 } 285 285 286 # Get module information for the current objec 286 # Get module information for the current object. 287 # 287 # 288 ARGIND >= 4 && /^ [^ ]/ && NF == 4 { 288 ARGIND >= 4 && /^ [^ ]/ && NF == 4 { 289 msect = $1; 289 msect = $1; 290 mod_name = get_module_info($4); 290 mod_name = get_module_info($4); 291 mod_eaddr = addr2val($2) + addr2val($3 291 mod_eaddr = addr2val($2) + addr2val($3); 292 292 293 next; 293 next; 294 } 294 } 295 295 296 # Process a symbol record. 296 # Process a symbol record. 297 # 297 # 298 # Evaluate the module information obtained fro 298 # Evaluate the module information obtained from vmlinux.map (or vmlinux.o.map) 299 # as follows: 299 # as follows: 300 # - For all symbols in a given object: 300 # - For all symbols in a given object: 301 # - If the symbol is annotated with the sa 301 # - If the symbol is annotated with the same module name(s) that the object 302 # belongs to, count it as a match. 302 # belongs to, count it as a match. 303 # - Otherwise: 303 # - Otherwise: 304 # - If the symbol is known to have dupl 304 # - If the symbol is known to have duplicates of which at least one is 305 # in a built-in module, disregard it. 305 # in a built-in module, disregard it. 306 # - If the symbol us not annotated with 306 # - If the symbol us not annotated with any module name(s) AND the 307 # object belongs to built-in modules, 307 # object belongs to built-in modules, count it as missing. 308 # - Otherwise, count it as a mismatch. 308 # - Otherwise, count it as a mismatch. 309 # 309 # 310 ARGIND >= 4 && /^ / && NF == 2 && $1 ~ /^0x/ { 310 ARGIND >= 4 && /^ / && NF == 2 && $1 ~ /^0x/ { 311 idx = sect":"msect; 311 idx = sect":"msect; 312 if (!(idx in sect_addend)) 312 if (!(idx in sect_addend)) 313 next; 313 next; 314 314 315 addr = addr2val($1); 315 addr = addr2val($1); 316 316 317 # Handle the rare but annoying case wh 317 # Handle the rare but annoying case where a 0-size symbol is placed at 318 # the byte *after* the module range. 318 # the byte *after* the module range. Based on vmlinux.map it will be 319 # considered part of the current objec 319 # considered part of the current object, but it falls just beyond the 320 # module address range. Unfortunately 320 # module address range. Unfortunately, its address could be at the 321 # start of another built-in module, so 321 # start of another built-in module, so the only safe thing to do is to 322 # ignore it. 322 # ignore it. 323 if (mod_name && addr == mod_eaddr) 323 if (mod_name && addr == mod_eaddr) 324 next; 324 next; 325 325 326 # If we are processing vmlinux.o.map, 326 # If we are processing vmlinux.o.map, we need to apply the base address 327 # of the section to the relative addre 327 # of the section to the relative address on the record. 328 # 328 # 329 if (ARGIND == 5) 329 if (ARGIND == 5) 330 addr += sect_addend[idx]; 330 addr += sect_addend[idx]; 331 331 332 idx = addr"-"$2; 332 idx = addr"-"$2; 333 mod = ""; 333 mod = ""; 334 if (idx in sym2mod) { 334 if (idx in sym2mod) { 335 mod = sym2mod[idx]; 335 mod = sym2mod[idx]; 336 if (sym2mod[idx] == mod_name) 336 if (sym2mod[idx] == mod_name) { 337 mod_matches++; 337 mod_matches++; 338 matches++; 338 matches++; 339 } else if (mod_name == "") { 339 } else if (mod_name == "") { 340 print $2 " in " mod " 340 print $2 " in " mod " (should NOT be)"; 341 mismatches++; 341 mismatches++; 342 } else { 342 } else { 343 print $2 " in " mod " 343 print $2 " in " mod " (should be " mod_name ")"; 344 mismatches++; 344 mismatches++; 345 } 345 } 346 } else if (mod_name != "") { 346 } else if (mod_name != "") { 347 print $2 " should be in " mod_ 347 print $2 " should be in " mod_name; 348 missing++; 348 missing++; 349 } else 349 } else 350 matches++; 350 matches++; 351 351 352 total++; 352 total++; 353 353 354 next; 354 next; 355 } 355 } 356 356 357 # Issue the comparison report. 357 # Issue the comparison report. 358 # 358 # 359 END { 359 END { 360 if (total) { 360 if (total) { 361 printf "Verification of %s:\n" 361 printf "Verification of %s:\n", ARGV[1]; 362 printf " Correct matches: %6 362 printf " Correct matches: %6d (%d%% of total)\n", matches, 100 * matches / total; 363 printf " Module matches: %6 363 printf " Module matches: %6d (%d%% of matches)\n", mod_matches, 100 * mod_matches / matches; 364 printf " Mismatches: %6 364 printf " Mismatches: %6d (%d%% of total)\n", mismatches, 100 * mismatches / total; 365 printf " Missing: %6 365 printf " Missing: %6d (%d%% of total)\n", missing, 100 * missing / total; 366 366 367 if (mismatches || missing) 367 if (mismatches || missing) 368 exit(1); 368 exit(1); 369 } 369 } 370 } 370 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.