verify_builtin_ranges.awk 9.1 KB

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