generate_builtin_ranges.awk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #!/usr/bin/gawk -f
  2. # SPDX-License-Identifier: GPL-2.0
  3. # generate_builtin_ranges.awk: Generate address range data for builtin modules
  4. # Written by Kris Van Hees <kris.van.hees@oracle.com>
  5. #
  6. # Usage: generate_builtin_ranges.awk modules.builtin vmlinux.map \
  7. # vmlinux.o.map > modules.builtin.ranges
  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. }
  29. close(fn);
  30. # A single module (common case) also reflects objects that are not part
  31. # of a module. Some of those objects have names that are also a module
  32. # name (e.g. core). We check the associated module file name, and if
  33. # they do not match, the object is not part of a module.
  34. if (mod !~ / /) {
  35. if (!(mod in mods))
  36. mod = "";
  37. }
  38. gsub(/([^/ ]*\/)+/, "", mod);
  39. gsub(/-/, "_", mod);
  40. # At this point, mod is a single (valid) module name, or a list of
  41. # module names (that do not need validation).
  42. omod[obj] = mod;
  43. return mod;
  44. }
  45. # Update the ranges entry for the given module 'mod' in section 'osect'.
  46. #
  47. # We use a modified absolute start address (soff + base) as index because we
  48. # may need to insert an anchor record later that must be at the start of the
  49. # section data, and the first module may very well start at the same address.
  50. # So, we use (addr << 1) + 1 to allow a possible anchor record to be placed at
  51. # (addr << 1). This is safe because the index is only used to sort the entries
  52. # before writing them out.
  53. #
  54. function update_entry(osect, mod, soff, eoff, sect, idx) {
  55. sect = sect_in[osect];
  56. idx = sprintf("%016x", (soff + sect_base[osect]) * 2 + 1);
  57. entries[idx] = sprintf("%s %08x-%08x %s", sect, soff, eoff, mod);
  58. count[sect]++;
  59. }
  60. # (1) Build a lookup map of built-in module names.
  61. #
  62. # The first file argument is used as input (modules.builtin).
  63. #
  64. # Lines will be like:
  65. # kernel/crypto/lzo-rle.ko
  66. # and we record the object name "crypto/lzo-rle".
  67. #
  68. ARGIND == 1 {
  69. sub(/kernel\//, ""); # strip off "kernel/" prefix
  70. sub(/\.ko$/, ""); # strip off .ko suffix
  71. mods[$1] = 1;
  72. next;
  73. }
  74. # (2) Collect address information for each section.
  75. #
  76. # The second file argument is used as input (vmlinux.map).
  77. #
  78. # We collect the base address of the section in order to convert all addresses
  79. # in the section into offset values.
  80. #
  81. # We collect the address of the anchor (or first symbol in the section if there
  82. # is no explicit anchor) to allow users of the range data to calculate address
  83. # ranges based on the actual load address of the section in the running kernel.
  84. #
  85. # We collect the start address of any sub-section (section included in the top
  86. # level section being processed). This is needed when the final linking was
  87. # done using vmlinux.a because then the list of objects contained in each
  88. # section is to be obtained from vmlinux.o.map. The offset of the sub-section
  89. # is recorded here, to be used as an addend when processing vmlinux.o.map
  90. # later.
  91. #
  92. # Both GNU ld and LLVM lld linker map format are supported by converting LLVM
  93. # lld linker map records into equivalent GNU ld linker map records.
  94. #
  95. # The first record of the vmlinux.map file provides enough information to know
  96. # which format we are dealing with.
  97. #
  98. ARGIND == 2 && FNR == 1 && NF == 7 && $1 == "VMA" && $7 == "Symbol" {
  99. map_is_lld = 1;
  100. if (dbg)
  101. printf "NOTE: %s uses LLVM lld linker map format\n", FILENAME >"/dev/stderr";
  102. next;
  103. }
  104. # (LLD) Convert a section record fronm lld format to ld format.
  105. #
  106. # lld: ffffffff82c00000 2c00000 2493c0 8192 .data
  107. # ->
  108. # ld: .data 0xffffffff82c00000 0x2493c0 load address 0x0000000002c00000
  109. #
  110. ARGIND == 2 && map_is_lld && NF == 5 && /[0-9] [^ ]+$/ {
  111. $0 = $5 " 0x"$1 " 0x"$3 " load address 0x"$2;
  112. }
  113. # (LLD) Convert an anchor record from lld format to ld format.
  114. #
  115. # lld: ffffffff81000000 1000000 0 1 _text = .
  116. # ->
  117. # ld: 0xffffffff81000000 _text = .
  118. #
  119. ARGIND == 2 && map_is_lld && !anchor && NF == 7 && raw_addr == "0x"$1 && $6 == "=" && $7 == "." {
  120. $0 = " 0x"$1 " " $5 " = .";
  121. }
  122. # (LLD) Convert an object record from lld format to ld format.
  123. #
  124. # lld: 11480 11480 1f07 16 vmlinux.a(arch/x86/events/amd/uncore.o):(.text)
  125. # ->
  126. # ld: .text 0x0000000000011480 0x1f07 arch/x86/events/amd/uncore.o
  127. #
  128. ARGIND == 2 && map_is_lld && NF == 5 && $5 ~ /:\(/ {
  129. gsub(/\)/, "");
  130. sub(/ vmlinux\.a\(/, " ");
  131. sub(/:\(/, " ");
  132. $0 = " "$6 " 0x"$1 " 0x"$3 " " $5;
  133. }
  134. # (LLD) Convert a symbol record from lld format to ld format.
  135. #
  136. # We only care about these while processing a section for which no anchor has
  137. # been determined yet.
  138. #
  139. # lld: ffffffff82a859a4 2a859a4 0 1 btf_ksym_iter_id
  140. # ->
  141. # ld: 0xffffffff82a859a4 btf_ksym_iter_id
  142. #
  143. ARGIND == 2 && map_is_lld && sect && !anchor && NF == 5 && $5 ~ /^[_A-Za-z][_A-Za-z0-9]*$/ {
  144. $0 = " 0x"$1 " " $5;
  145. }
  146. # (LLD) We do not need any other ldd linker map records.
  147. #
  148. ARGIND == 2 && map_is_lld && /^[0-9a-f]{16} / {
  149. next;
  150. }
  151. # (LD) Section records with just the section name at the start of the line
  152. # need to have the next line pulled in to determine whether it is a
  153. # loadable section. If it is, the next line will contains a hex value
  154. # as first and second items.
  155. #
  156. ARGIND == 2 && !map_is_lld && NF == 1 && /^[^ ]/ {
  157. s = $0;
  158. getline;
  159. if ($1 !~ /^0x/ || $2 !~ /^0x/)
  160. next;
  161. $0 = s " " $0;
  162. }
  163. # (LD) Object records with just the section name denote records with a long
  164. # section name for which the remainder of the record can be found on the
  165. # next line.
  166. #
  167. # (This is also needed for vmlinux.o.map, when used.)
  168. #
  169. ARGIND >= 2 && !map_is_lld && NF == 1 && /^ [^ \*]/ {
  170. s = $0;
  171. getline;
  172. $0 = s " " $0;
  173. }
  174. # Beginning a new section - done with the previous one (if any).
  175. #
  176. ARGIND == 2 && /^[^ ]/ {
  177. sect = 0;
  178. }
  179. # Process a loadable section (we only care about .-sections).
  180. #
  181. # Record the section name and its base address.
  182. # We also record the raw (non-stripped) address of the section because it can
  183. # be used to identify an anchor record.
  184. #
  185. # Note:
  186. # Since some AWK implementations cannot handle large integers, we strip off the
  187. # first 4 hex digits from the address. This is safe because the kernel space
  188. # is not large enough for addresses to extend into those digits. The portion
  189. # to strip off is stored in addr_prefix as a regexp, so further clauses can
  190. # perform a simple substitution to do the address stripping.
  191. #
  192. ARGIND == 2 && /^\./ {
  193. # Explicitly ignore a few sections that are not relevant here.
  194. if ($1 ~ /^\.orc_/ || $1 ~ /_sites$/ || $1 ~ /\.percpu/)
  195. next;
  196. # Sections with a 0-address can be ignored as well.
  197. if ($2 ~ /^0x0+$/)
  198. next;
  199. raw_addr = $2;
  200. addr_prefix = "^" substr($2, 1, 6);
  201. base = $2;
  202. sub(addr_prefix, "0x", base);
  203. base = strtonum(base);
  204. sect = $1;
  205. anchor = 0;
  206. sect_base[sect] = base;
  207. sect_size[sect] = strtonum($3);
  208. if (dbg)
  209. printf "[%s] BASE %016x\n", sect, base >"/dev/stderr";
  210. next;
  211. }
  212. # If we are not in a section we care about, we ignore the record.
  213. #
  214. ARGIND == 2 && !sect {
  215. next;
  216. }
  217. # Record the first anchor symbol for the current section.
  218. #
  219. # An anchor record for the section bears the same raw address as the section
  220. # record.
  221. #
  222. ARGIND == 2 && !anchor && NF == 4 && raw_addr == $1 && $3 == "=" && $4 == "." {
  223. anchor = sprintf("%s %08x-%08x = %s", sect, 0, 0, $2);
  224. sect_anchor[sect] = anchor;
  225. if (dbg)
  226. printf "[%s] ANCHOR %016x = %s (.)\n", sect, 0, $2 >"/dev/stderr";
  227. next;
  228. }
  229. # If no anchor record was found for the current section, use the first symbol
  230. # in the section as anchor.
  231. #
  232. ARGIND == 2 && !anchor && NF == 2 && $1 ~ /^0x/ && $2 !~ /^0x/ {
  233. addr = $1;
  234. sub(addr_prefix, "0x", addr);
  235. addr = strtonum(addr) - base;
  236. anchor = sprintf("%s %08x-%08x = %s", sect, addr, addr, $2);
  237. sect_anchor[sect] = anchor;
  238. if (dbg)
  239. printf "[%s] ANCHOR %016x = %s\n", sect, addr, $2 >"/dev/stderr";
  240. next;
  241. }
  242. # The first occurrence of a section name in an object record establishes the
  243. # addend (often 0) for that section. This information is needed to handle
  244. # sections that get combined in the final linking of vmlinux (e.g. .head.text
  245. # getting included at the start of .text).
  246. #
  247. # If the section does not have a base yet, use the base of the encapsulating
  248. # section.
  249. #
  250. ARGIND == 2 && sect && NF == 4 && /^ [^ \*]/ && !($1 in sect_addend) {
  251. # There are a few sections with constant data (without symbols) that
  252. # can get resized during linking, so it is best to ignore them.
  253. if ($1 ~ /^\.rodata\.(cst|str)[0-9]/)
  254. next;
  255. if (!($1 in sect_base)) {
  256. sect_base[$1] = base;
  257. if (dbg)
  258. printf "[%s] BASE %016x\n", $1, base >"/dev/stderr";
  259. }
  260. addr = $2;
  261. sub(addr_prefix, "0x", addr);
  262. addr = strtonum(addr);
  263. sect_addend[$1] = addr - sect_base[$1];
  264. sect_in[$1] = sect;
  265. if (dbg)
  266. printf "[%s] ADDEND %016x - %016x = %016x\n", $1, addr, base, sect_addend[$1] >"/dev/stderr";
  267. # If the object is vmlinux.o then we will need vmlinux.o.map to get the
  268. # actual offsets of objects.
  269. if ($4 == "vmlinux.o")
  270. need_o_map = 1;
  271. }
  272. # (3) Collect offset ranges (relative to the section base address) for built-in
  273. # modules.
  274. #
  275. # If the final link was done using the actual objects, vmlinux.map contains all
  276. # the information we need (see section (3a)).
  277. # If linking was done using vmlinux.a as intermediary, we will need to process
  278. # vmlinux.o.map (see section (3b)).
  279. # (3a) Determine offset range info using vmlinux.map.
  280. #
  281. # Since we are already processing vmlinux.map, the top level section that is
  282. # being processed is already known. If we do not have a base address for it,
  283. # we do not need to process records for it.
  284. #
  285. # Given the object name, we determine the module(s) (if any) that the current
  286. # object is associated with.
  287. #
  288. # If we were already processing objects for a (list of) module(s):
  289. # - If the current object belongs to the same module(s), update the range data
  290. # to include the current object.
  291. # - Otherwise, ensure that the end offset of the range is valid.
  292. #
  293. # If the current object does not belong to a built-in module, ignore it.
  294. #
  295. # If it does, we add a new built-in module offset range record.
  296. #
  297. ARGIND == 2 && !need_o_map && /^ [^ ]/ && NF == 4 && $3 != "0x0" {
  298. if (!(sect in sect_base))
  299. next;
  300. # Turn the address into an offset from the section base.
  301. soff = $2;
  302. sub(addr_prefix, "0x", soff);
  303. soff = strtonum(soff) - sect_base[sect];
  304. eoff = soff + strtonum($3);
  305. # Determine which (if any) built-in modules the object belongs to.
  306. mod = get_module_info($4);
  307. # If we are processing a built-in module:
  308. # - If the current object is within the same module, we update its
  309. # entry by extending the range and move on
  310. # - Otherwise:
  311. # + If we are still processing within the same main section, we
  312. # validate the end offset against the start offset of the
  313. # current object (e.g. .rodata.str1.[18] objects are often
  314. # listed with an incorrect size in the linker map)
  315. # + Otherwise, we validate the end offset against the section
  316. # size
  317. if (mod_name) {
  318. if (mod == mod_name) {
  319. mod_eoff = eoff;
  320. update_entry(mod_sect, mod_name, mod_soff, eoff);
  321. next;
  322. } else if (sect == sect_in[mod_sect]) {
  323. if (mod_eoff > soff)
  324. update_entry(mod_sect, mod_name, mod_soff, soff);
  325. } else {
  326. v = sect_size[sect_in[mod_sect]];
  327. if (mod_eoff > v)
  328. update_entry(mod_sect, mod_name, mod_soff, v);
  329. }
  330. }
  331. mod_name = mod;
  332. # If we encountered an object that is not part of a built-in module, we
  333. # do not need to record any data.
  334. if (!mod)
  335. next;
  336. # At this point, we encountered the start of a new built-in module.
  337. mod_name = mod;
  338. mod_soff = soff;
  339. mod_eoff = eoff;
  340. mod_sect = $1;
  341. update_entry($1, mod, soff, mod_eoff);
  342. next;
  343. }
  344. # If we do not need to parse the vmlinux.o.map file, we are done.
  345. #
  346. ARGIND == 3 && !need_o_map {
  347. if (dbg)
  348. printf "Note: %s is not needed.\n", FILENAME >"/dev/stderr";
  349. exit;
  350. }
  351. # (3) Collect offset ranges (relative to the section base address) for built-in
  352. # modules.
  353. #
  354. # (LLD) Convert an object record from lld format to ld format.
  355. #
  356. ARGIND == 3 && map_is_lld && NF == 5 && $5 ~ /:\(/ {
  357. gsub(/\)/, "");
  358. sub(/:\(/, " ");
  359. sect = $6;
  360. if (!(sect in sect_addend))
  361. next;
  362. sub(/ vmlinux\.a\(/, " ");
  363. $0 = " "sect " 0x"$1 " 0x"$3 " " $5;
  364. }
  365. # (3b) Determine offset range info using vmlinux.o.map.
  366. #
  367. # If we do not know an addend for the object's section, we are interested in
  368. # anything within that section.
  369. #
  370. # Determine the top-level section that the object's section was included in
  371. # during the final link. This is the section name offset range data will be
  372. # associated with for this object.
  373. #
  374. # The remainder of the processing of the current object record follows the
  375. # procedure outlined in (3a).
  376. #
  377. ARGIND == 3 && /^ [^ ]/ && NF == 4 && $3 != "0x0" {
  378. osect = $1;
  379. if (!(osect in sect_addend))
  380. next;
  381. # We need to work with the main section.
  382. sect = sect_in[osect];
  383. # Turn the address into an offset from the section base.
  384. soff = $2;
  385. sub(addr_prefix, "0x", soff);
  386. soff = strtonum(soff) + sect_addend[osect];
  387. eoff = soff + strtonum($3);
  388. # Determine which (if any) built-in modules the object belongs to.
  389. mod = get_module_info($4);
  390. # If we are processing a built-in module:
  391. # - If the current object is within the same module, we update its
  392. # entry by extending the range and move on
  393. # - Otherwise:
  394. # + If we are still processing within the same main section, we
  395. # validate the end offset against the start offset of the
  396. # current object (e.g. .rodata.str1.[18] objects are often
  397. # listed with an incorrect size in the linker map)
  398. # + Otherwise, we validate the end offset against the section
  399. # size
  400. if (mod_name) {
  401. if (mod == mod_name) {
  402. mod_eoff = eoff;
  403. update_entry(mod_sect, mod_name, mod_soff, eoff);
  404. next;
  405. } else if (sect == sect_in[mod_sect]) {
  406. if (mod_eoff > soff)
  407. update_entry(mod_sect, mod_name, mod_soff, soff);
  408. } else {
  409. v = sect_size[sect_in[mod_sect]];
  410. if (mod_eoff > v)
  411. update_entry(mod_sect, mod_name, mod_soff, v);
  412. }
  413. }
  414. mod_name = mod;
  415. # If we encountered an object that is not part of a built-in module, we
  416. # do not need to record any data.
  417. if (!mod)
  418. next;
  419. # At this point, we encountered the start of a new built-in module.
  420. mod_name = mod;
  421. mod_soff = soff;
  422. mod_eoff = eoff;
  423. mod_sect = osect;
  424. update_entry(osect, mod, soff, mod_eoff);
  425. next;
  426. }
  427. # (4) Generate the output.
  428. #
  429. # Anchor records are added for each section that contains offset range data
  430. # records. They are added at an adjusted section base address (base << 1) to
  431. # ensure they come first in the second records (see update_entry() above for
  432. # more information).
  433. #
  434. # All entries are sorted by (adjusted) address to ensure that the output can be
  435. # parsed in strict ascending address order.
  436. #
  437. END {
  438. for (sect in count) {
  439. if (sect in sect_anchor) {
  440. idx = sprintf("%016x", sect_base[sect] * 2);
  441. entries[idx] = sect_anchor[sect];
  442. }
  443. }
  444. n = asorti(entries, indices);
  445. for (i = 1; i <= n; i++)
  446. print entries[indices[i]];
  447. }