[PATCH 14/15] kallsyms: scripts/kallsyms: Reduce the output file size

From: David Laight

Date: Sun Oct 18 2026 - 11:54:54 EST


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@xxxxxxxxx>
---
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