BPF List
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Petr Mladek <pmladek@suse.com>, Kees Cook <kees@kernel.org>,
	David Laight <david.laight.linux@gmail.com>,
	linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	bpf@vger.kernel.org, Jim Cromie <jim.cromie@gmail.com>,
	Lorenzo Stoakes <ljs@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
	Andrey Grodzovsky <andrey.grodzovsky@crowdstrike.com>,
	Steven Rostedt <rostedt@goodmis.org>
Subject: [PATCH 14/15] kallsyms: scripts/kallsyms: Reduce the output file size
Date: Sun, 18 Oct 2026 16:52:05 +0100	[thread overview]
Message-ID: <20261018155206.132411-15-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20261018155206.132411-1-david.laight.linux@gmail.com>

The output file isn't normally looked at by anyone; so doesn't need
redundant information or formatting.
Reduce the size somewhat:
- Use "%u" instead of " 0x%02x" for byte values.
  Remove some other spaces.
- Only include the symbol name once.
  Put the offset into the compressed symbol names in all three per-symbol
  tables so that the lines can be cross-referenced if necessary.
- Output the kallsyms_token_index table and kallsyms_markers with
  multiple values per line.
- Add the missing .size for kallsyms_token_index.
- Output the token table first.
There is still enough information to help debug the code.

All the printfs are slow - but find_token() dominates everything.

Reduces the file size by about 25%.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 scripts/kallsyms.c | 93 +++++++++++++++++++++++++++-------------------
 1 file changed, 55 insertions(+), 38 deletions(-)

diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 189fb50ec8c5..8576fe6ad90b 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -350,6 +350,30 @@ static void write_src(void)
 	struct sym_entry *se;
 
 	printf("\t.section .rodata, \"a\"\n");
+	printf("\n");
+
+	output_label("kallsyms_token_table");
+	off = 0;
+	for (i = 0; i < 256; i++) {
+		best_idx[i] = off;
+		off += token_table[i].len + 1;
+		printf("\t.asciz\t\"%s\"\t/* %u: %u */\n",
+		       token_table[i].str, i, token_table[i].refs);
+	}
+	printf(".size kallsyms_token_table, . - kallsyms_token_table\n");
+	printf("\n");
+
+	output_label("kallsyms_token_index");
+	for (i = 0; i < 256; i++) {
+		if (i & 15)
+			printf(",");
+		else
+			printf("\n\t.short\t" + !i);
+		printf("%u", best_idx[i]);
+	}
+	printf("\n");
+	printf(".size kallsyms_token_index, . - kallsyms_token_index\n");
+	printf("\n");
 
 	output_value("kallsyms_num_syms", symbol_cnt);
 
@@ -359,35 +383,37 @@ static void write_src(void)
 		se = symbol[i];
 		se->seq = off;
 
-		/* There cannot be any symbol of length zero. */
-		if (se->len == 0)
-			errx(1, "unexpected zero symbol length");
+		/* The length must be less than that of the original symbol. */
+		if (!se->len || se->len > KSYM_NAME_LEN)
+			errx(1, "symbol %d has corrupt compressed length %d\n",
+			     i, se->len);
 
-		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
-		if (se->len > 0x3FFF)
-			errx(1, "unexpected huge symbol length");
+		/*
+		 * Output the number that identifies the symbol at the start
+		 * of the line so that they line up nicely.
+		 */
+		printf("\t/* n:%u */\t.byte\t", off);
 
 		/* Encode length with ULEB128. */
 		if (se->len <= 0x7F) {
 			/* Most symbols use a single byte for the length. */
-			printf("\t.byte 0x%02x", se->len);
+			printf("%u", se->len);
 			off += se->len + 1;
 		} else {
 			/* "Big" symbols use two bytes. */
-			printf("\t.byte 0x%02x, 0x%02x",
-				(se->len & 0x7F) | 0x80, (se->len >> 7) & 0x7F);
+			printf("%u,%u", (se->len & 0x7F) | 0x80, (se->len >> 7) & 0x7F);
 			off += se->len + 2;
 		}
 		for (k = 0; k < se->len; k++)
-			printf(", 0x%02x", se->sym[k]);
+			printf(",%u", se->sym[k]);
+		printf("\n");
 
 		/*
 		 * Now that we wrote out the compressed symbol name, restore the
-		 * original name and print it in the comment.
+		 * original name.
 		 */
 		se->len = expand_symbol(se->sym, se->len, buf);
 		memcpy(se->sym, buf, se->len + 1);
-		printf("\t/* %s */\n", se->sym);
 	}
 	table_size = off;
 	printf(".size kallsyms_names, . - kallsyms_names\n");
@@ -395,25 +421,15 @@ static void write_src(void)
 
 	output_value("kallsyms_marker_shift", marker_shift);
 	output_label("kallsyms_markers");
-	for (i = 0; i < symbol_cnt; i += 1u << marker_shift)
-		printf("\t.long\t%u\n", symbol[i]->seq);
-	printf(".size kallsyms_markers, . - kallsyms_markers\n");
-	printf("\n");
-
-	output_label("kallsyms_token_table");
-	off = 0;
-	for (i = 0; i < 256; i++) {
-		best_idx[i] = off;
-		off += token_table[i].len + 1;
-		printf("\t.asciz\t\"%s\"\t/* %u: %u */\n",
-		       token_table[i].str, i, token_table[i].refs);
+	for (i = 0, k = 0; i < symbol_cnt; i += 1u << marker_shift, k++) {
+		if (k & 7)
+			printf(",");
+		else
+			printf("\n\t.long\t" + !k);
+		printf("%u", symbol[i]->seq);
 	}
-	printf(".size kallsyms_token_table, . - kallsyms_token_table\n");
 	printf("\n");
-
-	output_label("kallsyms_token_index");
-	for (i = 0; i < 256; i++)
-		printf("\t.short\t%d\n", best_idx[i]);
+	printf(".size kallsyms_markers, . - kallsyms_markers\n");
 	printf("\n");
 
 	output_label("kallsyms_offsets");
@@ -424,13 +440,13 @@ static void write_src(void)
 			long long offset = se->addr - _text;
 
 			if (offset < INT_MIN || offset > INT_MAX)
-				errx(1, "relative symbol value %#llx out of range",
-				     se->addr);
-			printf("\t.long\t_text - . + (%d)\t/* %s */\n",
-			       (int)offset, se->sym);
+				errx(1, "relative symbol %s value %#llx out of range\n",
+					se->sym + 1, se->addr);
+			printf("\t.long\t_text-.+%d\t/* n:%u %c %s */\n",
+			       (int)offset, se->seq, se->sym[0], se->sym + 1);
 		} else {
-			printf("\t.long\t%#x\t/* %s */\n",
-			       (unsigned int)se->addr, se->sym);
+			printf("\t.long\t%#x\t/* n:%u %c %s */\n",
+			       (unsigned int)se->addr, se->seq, se->sym[0], se->sym + 1);
 		}
 	}
 	printf(".size kallsyms_offsets, . - kallsyms_offsets\n");
@@ -451,10 +467,11 @@ static void write_src(void)
 #ifdef CONFIG_CPU_BIG_ENDIAN
 			off = __builtin_bswap32(off) >> 8;
 #endif
-			printf("\t.byte\t0x%02x, 0x%02x, 0x%02x\t/* %s */\n",
-				off & 0xff, (off >> 8) & 0xff, off >> 16, se->sym);
+			/* The three spaces before the tab line up the comments */
+			printf("\t.byte\t%u,%u,%u   \t/* n:%u */\n",
+				off & 0xff, (off >> 8) & 0xff, off >> 16, se->seq);
 		} else {
-			printf("\t.long\t%#04x\t/* %s */\n", off, se->sym);
+			printf("\t.long\t%#04x\t/* n:%u */\n", off, se->seq);
 		}
 	}
 	/* Terminator without the 'duplicate' flag. */
-- 
2.39.5


  parent reply	other threads:[~2026-10-18 15:53 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 15:51 [PATCH v2 next 00/15] kallsyms: optimise symbol search by name David Laight
2026-10-18 15:51 ` [PATCH v2 next 01/15] kallsyms: scripts/kallsyms.c: Avoid overrunning on-stack buffer David Laight
2026-10-18 15:51 ` [PATCH v2 next 02/15] kallsyms: Fix selftest startup delay David Laight
2026-10-18 15:51 ` [PATCH v2 next 03/15] kallsyms: Improve selftest test_perf_kallsyms_lookup_name() output David Laight
2026-10-18 16:03   ` sashiko-bot
2026-10-19 16:07   ` kernel test robot
2026-10-19 17:08     ` David Laight
2026-10-18 15:51 ` [PATCH v2 next 04/15] kallsyms: scripts/kallsyms: Check for output file write errors David Laight
2026-10-18 15:51 ` [PATCH v2 next 05/15] kallsyms: Match compressed tokens on the fly during binary search David Laight
2026-10-18 15:51 ` [PATCH v2 next 06/15] kallsyms: Optimise symbol name search David Laight
2026-10-18 16:15   ` sashiko-bot
2026-10-18 17:04     ` David Laight
2026-10-18 15:51 ` [PATCH v2 next 07/15] kallsyms: Optimise detection of duplicate symbol names David Laight
2026-10-18 15:51 ` [PATCH v2 next 08/15] kallsyms: Support changing the density of the kallsyms_markers[] array David Laight
2026-10-18 15:52 ` [PATCH v2 next 09/15] kallsyms: Add a kernel parameter for the marker shift David Laight
2026-10-18 15:52 ` [PATCH v2 next 10/15] kallsyms: scripts/kallsyms: Rename some variables for clarity David Laight
2026-10-18 15:52 ` [PATCH v2 next 11/15] kallsyms: scripts/kallsyms: Keep full expansion of tokens David Laight
2026-10-18 16:00   ` sashiko-bot
2026-10-18 16:37     ` David Laight
2026-10-18 15:52 ` [PATCH v2 next 12/15] kallsyms: scripts/kallsyms: Optimise compress_symbols() David Laight
2026-10-18 15:52 ` [PATCH v2 next 13/15] kallsyms: scripts/kallsyms: Reuse tokens that have no references David Laight
2026-10-18 15:52 ` David Laight [this message]
2026-10-18 16:05   ` [PATCH 14/15] kallsyms: scripts/kallsyms: Reduce the output file size sashiko-bot
2026-10-18 16:47     ` David Laight
2026-10-18 15:52 ` [PATCH 15/15] kallsyms: scripts/kallsyms: Improve compression by about 2% David Laight
2026-10-18 16:05   ` sashiko-bot
2026-10-18 17:28     ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261018155206.132411-15-david.laight.linux@gmail.com \
    --to=david.laight.linux@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrey.grodzovsky@crowdstrike.com \
    --cc=bpf@vger.kernel.org \
    --cc=jim.cromie@gmail.com \
    --cc=kees@kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox