Skip to contentSkip to conversation

mksysmap: update comment about __crc_*

PATCH New Masahiro Yamada 1 participant 5 messages Sep 26, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 mksysmap: update comment about __crc_*

Since commit 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS"), __crc_* symbols never become absolute.

Keep ignoring __crc_*, but update the comment.

---

 scripts/mksysmap | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
scripts/mksysmap +4 -4
index ad8bbc52267d..bc5396e255d8 100755
@@ -37,8 +37,8 @@
 
 # readprofile starts reading symbols when _stext is found, and
 # continue until it finds a symbol which is not either of 'T', 't',
-# 'W' or 'w'. __crc_ are 'A' and placed in the middle
-# so we just ignore them to let readprofile continue to work.
-# (At least sparc64 has __crc_ in the middle).
-
+# 'W' or 'w'.
+#
+# Ignored prefixes:
+#  __crc_               - modversions
 $NM -n $1 | grep -v '\( [aNUw] \)\|\(__crc_\)\|\( \$[adt]\)\|\( \.L\)\|\( L0\)' > $2
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/5 kbuild: reuse mksysmap output for kallsyms

scripts/mksysmap internally runs ${NM} (dropping some symbols).

When CONFIG_KALLSYMS=y, mksysmap creates .tmp_System.map, but it is almost the same as the output from the ${NM} invocation in kallsyms(). It is true scripts/mksysmap drops some symbols, but scripts/kallsyms.c ignores more anyway.

Keep the mksysmap output as *.syms, and reuse it for kallsyms and 'cmp -s'. It saves one ${NM} invocation.

---

 scripts/link-vmlinux.sh | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
scripts/link-vmlinux.sh +5 -6
index 23ac13fd9d89..6492c0862657 100755
@@ -157,7 +157,7 @@ kallsyms()
 	fi
 
 	info KSYMS ${2}
-	${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2}
+	cat ${1} | scripts/kallsyms ${kallsymopt} > ${2}
 }
 
 # Perform one step in kallsyms generation, including temporary linking of
@@ -170,7 +170,8 @@ kallsyms_step()
 	kallsyms_S=${kallsyms_vmlinux}.S
 
 	vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
-	kallsyms ${kallsyms_vmlinux} ${kallsyms_S}
+	mksysmap ${kallsyms_vmlinux} ${kallsyms_vmlinux}.syms
+	kallsyms ${kallsyms_vmlinux}.syms ${kallsyms_S}
 
 	info AS ${kallsyms_S}
 	${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
@@ -182,6 +183,7 @@ kallsyms_step()
 # See mksymap for additional details
 mksysmap()
 {
+	info NM ${2}
 	${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
 }
 
@@ -283,7 +285,6 @@ if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then
 	${RESOLVE_BTFIDS} vmlinux
 fi
 
-info SYSMAP System.map
 mksysmap vmlinux System.map
 
 if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
@@ -296,9 +297,7 @@ fi
 
 # step a (see comment above)
 if is_enabled CONFIG_KALLSYMS; then
-	mksysmap ${kallsyms_vmlinux} .tmp_System.map
-
-	if ! cmp -s System.map .tmp_System.map; then
+	if ! cmp -s System.map ${kallsyms_vmlinux}.syms; then
 		echo >&2 Inconsistent kallsyms data
 		echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
 		exit 1
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/5 kallsyms: drop duplicated ignore patterns from kallsyms.c

In reply to Masahiro Yamada

Now that kallsyms.c parses the output from mksysmap, some symbols have already been dropped.

Move comments to scripts/mksysmap. Also, make the grep command readable.

---

 scripts/kallsyms.c |  3 ---
 scripts/mksysmap   | 14 +++++++++++++-
 2 files changed, 13 insertions(+), 4 deletions(-)
scripts/kallsyms.c +0 -3
index f18e6dfc68c5..313cc8161123 100644
@@ -107,9 +107,6 @@ static bool is_ignored_symbol(const char *name, char type)
 
 	/* Symbol names that begin with the following are ignored.*/
 	static const char * const ignored_prefixes[] = {
-		"$",			/* local symbols for ARM, MIPS, etc. */
-		".L",			/* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */
-		"__crc_",		/* modversions */
 		"__efistub_",		/* arm64 EFI stub namespace */
 		"__kvm_nvhe_$",		/* arm64 local symbols in non-VHE KVM namespace */
 		"__kvm_nvhe_.L",	/* arm64 local symbols in non-VHE KVM namespace */
scripts/mksysmap +13 -1
index bc5396e255d8..75f3dfd1c156 100755
@@ -40,5 +40,17 @@
 # 'W' or 'w'.
 #
 # Ignored prefixes:
+#  $                    - local symbols for ARM, MIPS, etc.
+#  .L                   - local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc.
 #  __crc_               - modversions
-$NM -n $1 | grep -v '\( [aNUw] \)\|\(__crc_\)\|\( \$[adt]\)\|\( \.L\)\|\( L0\)' > $2
+#
+# Ignored symbols:
+#  L0                   - for LoongArch?
+
+$NM -n $1 | grep -v		\
+	-e ' [aNUw] '		\
+	-e ' \$'		\
+	-e ' \.L'		\
+	-e ' __crc_'		\
+	-e ' L0$'		\
+> $2
Signature · 1 line
2.34.1
#3 lore raw

PATCH 4/5 kallsyms: take the input file instead of reading stdin

In reply to Masahiro Yamada

This gets rid of the pipe operator connected with 'cat'.

Also use getopt_long() to parse the command line.

---

 scripts/kallsyms.c      | 51 ++++++++++++++++++++++++++---------------
 scripts/link-vmlinux.sh |  2 +-
 2 files changed, 34 insertions(+), 19 deletions(-)
scripts/kallsyms.c +33 -18
index 313cc8161123..5b091625d4c5 100644
@@ -18,6 +18,7 @@
  *
  */
 
+#include <getopt.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -71,7 +72,7 @@ static unsigned char best_table_len[256];
 static void usage(void)
 {
 	fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
-			"[--base-relative] < in.map > out.S\n");
+			"[--base-relative] in.map > out.S\n");
 	exit(1);
 }
 
@@ -310,12 +311,19 @@ static void shrink_table(void)
 	}
 }
 
-static void read_map(FILE *in)
+static void read_map(const char *in)
 {
+	FILE *fp;
 	struct sym_entry *sym;
 
-	while (!feof(in)) {
-		sym = read_symbol(in);
+	fp = fopen(in, "r");
+	if (!fp) {
+		perror(in);
+		exit(1);
+	}
+
+	while (!feof(fp)) {
+		sym = read_symbol(fp);
 		if (!sym)
 			continue;
 
@@ -326,12 +334,15 @@ static void read_map(FILE *in)
 			table = realloc(table, sizeof(*table) * table_size);
 			if (!table) {
 				fprintf(stderr, "out of memory\n");
+				fclose(fp);
 				exit (1);
 			}
 		}
 
 		table[table_cnt++] = sym;
 	}
+
+	fclose(fp);
 }
 
 static void output_label(const char *label)
@@ -762,22 +773,26 @@ static void record_relative_base(void)
 
 int main(int argc, char **argv)
 {
-	if (argc >= 2) {
-		int i;
-		for (i = 1; i < argc; i++) {
-			if(strcmp(argv[i], "--all-symbols") == 0)
-				all_symbols = 1;
-			else if (strcmp(argv[i], "--absolute-percpu") == 0)
-				absolute_percpu = 1;
-			else if (strcmp(argv[i], "--base-relative") == 0)
-				base_relative = 1;
-			else
-				usage();
-		}
-	} else if (argc != 1)
+	while (1) {
+		static struct option long_options[] = {
+			{"all-symbols",     no_argument, &all_symbols,     1},
+			{"absolute-percpu", no_argument, &absolute_percpu, 1},
+			{"base-relative",   no_argument, &base_relative,   1},
+			{},
+		};
+
+		int c = getopt_long(argc, argv, "", long_options, NULL);
+
+		if (c == -1)
+			break;
+		if (c != 0)
+			usage();
+	}
+
+	if (optind >= argc)
 		usage();
 
-	read_map(stdin);
+	read_map(argv[optind]);
 	shrink_table();
 	if (absolute_percpu)
 		make_percpus_absolute();
scripts/link-vmlinux.sh +1 -1
index 6492c0862657..2782c5d1518b 100755
@@ -157,7 +157,7 @@ kallsyms()
 	fi
 
 	info KSYMS ${2}
-	cat ${1} | scripts/kallsyms ${kallsymopt} > ${2}
+	scripts/kallsyms ${kallsymopt} ${1} > ${2}
 }
 
 # Perform one step in kallsyms generation, including temporary linking of
Signature · 1 line
2.34.1
#4 lore raw

PATCH 5/5 kallsyms: ignore __kstrtab_* and __kstrtabns_* symbols

In reply to Masahiro Yamada

Every EXPORT_SYMBOL creates __kstrtab_* and __kstrtabns_*, which consumes 15-20% of the kallsyms entries.

For x86_64 defconfig,

  $ cat /proc/kallsyms | wc
     129527    388581   5685465
  $ cat /proc/kallsyms | grep __kstrtab | wc
      23489     70467   1187932

We already ignore __crc_* symbols populated by EXPORT_SYMBOL, so it should be fine to ignore __kstrtab_* and __kstrtabns_* as well.

This makes vmlinux a bit smaller.

  $ size vmlinux.before vmlinux.after
     text    data     bss     dec     hex filename
  22785374        8559694 1413328 32758396        1f3da7c vmlinux.before
  22785374        8137806 1413328 32336508        1ed6a7c vmlinux.after

---

 scripts/mksysmap | 4 ++++
 1 file changed, 4 insertions(+)
scripts/mksysmap +4 -0
index 75f3dfd1c156..16a08b8ef2f8 100755
@@ -43,6 +43,8 @@
 #  $                    - local symbols for ARM, MIPS, etc.
 #  .L                   - local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc.
 #  __crc_               - modversions
+#  __kstrtab_           - EXPORT_SYMBOL (symbol name)
+#  __kstrtabns_         - EXPORT_SYMBOL (namespace)
 #
 # Ignored symbols:
 #  L0                   - for LoongArch?
@@ -52,5 +54,7 @@ $NM -n $1 | grep -v		\
 	-e ' \$'		\
 	-e ' \.L'		\
 	-e ' __crc_'		\
+	-e ' __kstrtab_'	\
+	-e ' __kstrtabns_'	\
 	-e ' L0$'		\
 > $2
Signature · 1 line
2.34.1