All of lore.kernel.org
 help / color / mirror / Atom feed
From: Miguel Ojeda <ojeda@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jarkko Sakkinen <jarkko@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Kees Cook <keescook@chromium.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Wedson Almeida Filho <wedsonaf@google.com>,
	Gary Guo <gary@garyguo.net>, Boqun Feng <boqun.feng@gmail.com>,
	Matthew Wilcox <willy@infradead.org>
Subject: [PATCH v7 02/25] kallsyms: support "big" kernel symbols
Date: Mon, 23 May 2022 04:01:15 +0200	[thread overview]
Message-ID: <20220523020209.11810-3-ojeda@kernel.org> (raw)
In-Reply-To: <20220523020209.11810-1-ojeda@kernel.org>

Rust symbols can become quite long due to namespacing introduced
by modules, types, traits, generics, etc.

Increasing to 255 is not enough in some cases, and therefore
we need to introduce longer lengths to the symbol table.

In order to avoid increasing all lengths to 2 bytes (since most
of them are small, including many Rust ones), we use ULEB128 to
keep smaller symbols in 1 byte, with the rest in 2 bytes.

Reviewed-by: Kees Cook <keescook@chromium.org>
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Co-developed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Gary Guo <gary@garyguo.net>
Co-developed-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Co-developed-by: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
This is a prerequisite patch, independently submitted at:

    https://lore.kernel.org/lkml/20220506203443.24721-3-ojeda@kernel.org/

 kernel/kallsyms.c  | 26 ++++++++++++++++++++++----
 scripts/kallsyms.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index 79f2eb617a62..e8d2262ef2d2 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -69,12 +69,20 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
 	data = &kallsyms_names[off];
 	len = *data;
 	data++;
+	off++;
+
+	/* If MSB is 1, it is a "big" symbol, so needs an additional byte. */
+	if ((len & 0x80) != 0) {
+		len = (len & 0x7F) | (*data << 7);
+		data++;
+		off++;
+	}
 
 	/*
 	 * Update the offset to return the offset for the next symbol on
 	 * the compressed stream.
 	 */
-	off += len + 1;
+	off += len;
 
 	/*
 	 * For every byte on the compressed symbol data, copy the table
@@ -127,7 +135,7 @@ static char kallsyms_get_symbol_type(unsigned int off)
 static unsigned int get_symbol_offset(unsigned long pos)
 {
 	const u8 *name;
-	int i;
+	int i, len;
 
 	/*
 	 * Use the closest marker we have. We have markers every 256 positions,
@@ -141,8 +149,18 @@ static unsigned int get_symbol_offset(unsigned long pos)
 	 * so we just need to add the len to the current pointer for every
 	 * symbol we wish to skip.
 	 */
-	for (i = 0; i < (pos & 0xFF); i++)
-		name = name + (*name) + 1;
+	for (i = 0; i < (pos & 0xFF); i++) {
+		len = *name;
+
+		/*
+		 * If MSB is 1, it is a "big" symbol, so we need to look into
+		 * the next byte (and skip it, too).
+		 */
+		if ((len & 0x80) != 0)
+			len = ((len & 0x7F) | (name[1] << 7)) + 1;
+
+		name = name + len + 1;
+	}
 
 	return name - kallsyms_names;
 }
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 82d6508bdf29..7e99799aa7b9 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -480,12 +480,35 @@ static void write_src(void)
 		if ((i & 0xFF) == 0)
 			markers[i >> 8] = off;
 
-		printf("\t.byte 0x%02x", table[i]->len);
+		/* There cannot be any symbol of length zero. */
+		if (table[i]->len == 0) {
+			fprintf(stderr, "kallsyms failure: "
+				"unexpected zero symbol length\n");
+			exit(EXIT_FAILURE);
+		}
+
+		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
+		if (table[i]->len > 0x3FFF) {
+			fprintf(stderr, "kallsyms failure: "
+				"unexpected huge symbol length\n");
+			exit(EXIT_FAILURE);
+		}
+
+		/* Encode length with ULEB128. */
+		if (table[i]->len <= 0x7F) {
+			/* Most symbols use a single byte for the length. */
+			printf("\t.byte 0x%02x", table[i]->len);
+			off += table[i]->len + 1;
+		} else {
+			/* "Big" symbols use two bytes. */
+			printf("\t.byte 0x%02x, 0x%02x",
+				(table[i]->len & 0x7F) | 0x80,
+				(table[i]->len >> 7) & 0x7F);
+			off += table[i]->len + 2;
+		}
 		for (k = 0; k < table[i]->len; k++)
 			printf(", 0x%02x", table[i]->sym[k]);
 		printf("\n");
-
-		off += table[i]->len + 1;
 	}
 	printf("\n");
 
-- 
2.36.1


  parent reply	other threads:[~2022-05-23  2:03 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-23  2:01 [PATCH v7 00/25] Rust support Miguel Ojeda
2022-05-23  2:01 ` Miguel Ojeda
2022-05-23  2:01 ` Miguel Ojeda
2022-05-23  2:01 ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size Miguel Ojeda
2022-05-23 19:45   ` Jarkko Sakkinen
2022-05-23 19:55     ` Jarkko Sakkinen
2022-05-24 16:21     ` Miguel Ojeda
2022-05-26  4:54       ` Jarkko Sakkinen
2022-05-23  2:01 ` Miguel Ojeda [this message]
2022-05-23 20:30   ` [PATCH v7 02/25] kallsyms: support "big" kernel symbols Jarkko Sakkinen
2022-05-23  2:01 ` [PATCH v7 03/25] kallsyms: increase maximum kernel symbol length to 512 Miguel Ojeda
2022-05-23 20:31   ` Jarkko Sakkinen
2022-05-24 18:07     ` Miguel Ojeda
2022-05-27 16:25       ` Jarkko Sakkinen
2022-05-30 13:01         ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 04/25] kunit: take `kunit_assert` as `const` Miguel Ojeda
2022-05-23 17:15   ` Daniel Latypov
2022-05-23 18:14     ` Shuah Khan
2022-05-24 12:37       ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 05/25] rust: add C helpers Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 06/25] rust: add `compiler_builtins` crate Miguel Ojeda
2022-05-23 18:37   ` Nick Desaulniers
2022-05-23 23:41     ` Gary Guo
2022-05-25 21:29       ` Nick Desaulniers
2022-05-25 21:29         ` Nick Desaulniers
2022-05-24 12:29     ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 07/25] rust: import upstream `alloc` crate Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 08/25] rust: adapt `alloc` crate to the kernel Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 09/25] rust: add `build_error` crate Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 10/25] rust: add `macros` crate Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 11/25] rust: add `kernel` crate's `sync` module Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 12/25] rust: add `kernel` crate Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 13/25] rust: export generated symbols Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 14/25] vsprintf: add new `%pA` format specifier Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 15/25] scripts: checkpatch: diagnose uses of `%pA` in the C side Miguel Ojeda
2022-05-23  2:17   ` Joe Perches
2022-05-24 16:35     ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 16/25] scripts: checkpatch: enable language-independent checks for Rust Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 17/25] scripts: add `rustdoc_test_{builder,gen}.py` scripts Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 18/25] scripts: add `generate_rust_analyzer.py` scripts Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 19/25] scripts: decode_stacktrace: demangle Rust symbols Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 20/25] docs: add Rust documentation Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 21/25] Kbuild: add Rust support Miguel Ojeda
2022-05-23  2:01   ` Miguel Ojeda
2022-05-23  2:01   ` Miguel Ojeda
2022-05-23  2:01   ` Miguel Ojeda
2022-05-23 18:44   ` Nick Desaulniers
2022-05-23 18:44     ` Nick Desaulniers
2022-05-23 18:44     ` Nick Desaulniers
2022-05-23 18:44     ` Nick Desaulniers
2022-05-23 18:44     ` Nick Desaulniers
2022-05-24 15:12     ` Miguel Ojeda
2022-05-24 15:12       ` Miguel Ojeda
2022-05-24 15:12       ` Miguel Ojeda
2022-05-24 15:12       ` Miguel Ojeda
2022-05-24 15:12       ` Miguel Ojeda
2022-05-25 22:25   ` Nick Desaulniers
2022-05-25 22:25     ` Nick Desaulniers
2022-05-25 22:25     ` Nick Desaulniers
2022-05-25 22:25     ` Nick Desaulniers
2022-05-25 22:25     ` Nick Desaulniers
2022-05-30 13:39     ` Miguel Ojeda
2022-05-30 13:39       ` Miguel Ojeda
2022-05-30 13:39       ` Miguel Ojeda
2022-05-30 13:39       ` Miguel Ojeda
2022-05-30 13:39       ` Miguel Ojeda
2022-07-16  8:21   ` Masahiro Yamada
2022-07-16  8:21     ` Masahiro Yamada
2022-07-16  8:21     ` Masahiro Yamada
2022-07-16  8:21     ` Masahiro Yamada
2022-07-16  8:57     ` Miguel Ojeda
2022-07-16  8:57       ` Miguel Ojeda
2022-07-16  8:57       ` Miguel Ojeda
2022-07-16  8:57       ` Miguel Ojeda
2022-07-16  8:57       ` Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 22/25] samples: add Rust examples Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 23/25] MAINTAINERS: Rust Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 24/25] [RFC] drivers: gpio: PrimeCell PL061 in Rust Miguel Ojeda
2022-05-23  2:01 ` [PATCH v7 25/25] [RFC] drivers: android: Binder IPC " Miguel Ojeda
2022-07-16 12:42 ` [PATCH v7 00/25] Rust support Conor Dooley
2022-07-16 12:42   ` Conor Dooley
2022-07-16 12:42   ` Conor Dooley
2022-07-16 12:42   ` Conor Dooley
2022-07-16 12:42   ` Conor Dooley
2022-07-16 13:36   ` Miguel Ojeda
2022-07-16 13:36     ` Miguel Ojeda
2022-07-16 13:36     ` Miguel Ojeda
2022-07-16 13:36     ` Miguel Ojeda
2022-07-16 13:36     ` Miguel Ojeda
2022-07-16 13:51     ` Conor.Dooley
2022-07-16 13:51       ` Conor.Dooley
2022-07-16 13:51       ` Conor.Dooley
2022-07-16 13:51       ` Conor.Dooley
2022-07-16 13:51       ` Conor.Dooley
2022-07-16 13:56       ` Miguel Ojeda
2022-07-16 13:56         ` Miguel Ojeda
2022-07-16 13:56         ` Miguel Ojeda
2022-07-16 13:56         ` Miguel Ojeda
2022-07-16 13:56         ` Miguel Ojeda

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=20220523020209.11810-3-ojeda@kernel.org \
    --to=ojeda@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jarkko@kernel.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=wedsonaf@google.com \
    --cc=willy@infradead.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.