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 v8 04/31] kallsyms: support "big" kernel symbols
Date: Tue,  2 Aug 2022 03:49:51 +0200	[thread overview]
Message-ID: <20220802015052.10452-5-ojeda@kernel.org> (raw)
In-Reply-To: <20220802015052.10452-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, therefore
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), 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>
---
 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 fbdf8d3279ac..87e2b1638115 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -70,12 +70,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
@@ -128,7 +136,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,
@@ -142,8 +150,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 f543b1c4f99f..9da3b7767e9d 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -481,12 +481,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.37.1


  parent reply	other threads:[~2022-08-02  1:51 UTC|newest]

Thread overview: 129+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02  1:49 [PATCH v8 00/31] Rust support Miguel Ojeda
2022-08-02  1:49 ` Miguel Ojeda
2022-08-02  1:49 ` Miguel Ojeda
2022-08-02  1:49 ` Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 01/31] kallsyms: use `sizeof` instead of hardcoded size Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 02/31] kallsyms: avoid hardcoding buffer size Miguel Ojeda
2022-08-02  8:29   ` David Laight
2022-08-02  9:45     ` Rasmus Villemoes
2022-08-02  1:49 ` [PATCH v8 03/31] kallsyms: add static relationship between `KSYM_NAME_LEN{,_BUFFER}` Miguel Ojeda
2022-08-02  1:49 ` Miguel Ojeda [this message]
2022-08-02  1:49 ` [PATCH v8 05/31] kallsyms: increase maximum kernel symbol length to 512 Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 06/31] workqueue: introduce `__INIT_WORK_WITH_KEY` Miguel Ojeda
2022-08-15 21:14   ` Tejun Heo
2022-08-15 21:53     ` Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 07/31] locking/spinlock: introduce `__spin_lock_init` Miguel Ojeda
2022-08-03 20:58   ` Boqun Feng
2022-08-02  1:49 ` [PATCH v8 08/31] locking/spinlock: introduce `_raw_spin_lock_init` Miguel Ojeda
2022-08-03 21:00   ` Boqun Feng
2022-08-02  1:49 ` [PATCH v8 09/31] rust: add C helpers Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 10/31] rust: add `compiler_builtins` crate Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 11/31] rust: import upstream `alloc` crate Miguel Ojeda
2022-08-02  1:49 ` [PATCH v8 12/31] rust: adapt `alloc` crate to the kernel Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 13/31] rust: add `build_error` crate Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 14/31] rust: add `macros` crate Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 15/31] rust: add `bindings` crate Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 16/31] rust: add `kernel` crate's `sync` module Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 17/31] rust: add `kernel` crate Miguel Ojeda
2022-08-02 13:34   ` Greg Kroah-Hartman
2022-08-02 14:33     ` Miguel Ojeda
2022-08-02 14:36       ` Greg Kroah-Hartman
2022-08-02 14:53         ` Miguel Ojeda
2022-08-03  7:30           ` Greg Kroah-Hartman
2022-08-02  1:50 ` [PATCH v8 18/31] rust: export generated symbols Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 19/31] vsprintf: add new `%pA` format specifier Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 20/31] scripts: checkpatch: diagnose uses of `%pA` in the C side as errors Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 21/31] scripts: checkpatch: enable language-independent checks for Rust Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 22/31] scripts: add `rustdoc_test_{builder,gen}.py` scripts Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 23/31] scripts: add `generate_rust_analyzer.py` scripts Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 24/31] scripts: decode_stacktrace: demangle Rust symbols Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 25/31] configs: add `rust` config Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 26/31] docs: add Rust documentation Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 27/31] Kbuild: add Rust support Miguel Ojeda
2022-08-02  1:50   ` Miguel Ojeda
2022-08-02  1:50   ` Miguel Ojeda
2022-08-02  1:50   ` Miguel Ojeda
2022-08-02  1:50   ` Miguel Ojeda
2022-08-17 14:39   ` Arnd Bergmann
2022-08-17 14:39     ` Arnd Bergmann
2022-08-17 14:39     ` Arnd Bergmann
2022-08-17 14:39     ` Arnd Bergmann
2022-08-17 14:39     ` Arnd Bergmann
2022-08-17 15:13     ` Miguel Ojeda
2022-08-17 15:13       ` Miguel Ojeda
2022-08-17 15:13       ` Miguel Ojeda
2022-08-17 15:13       ` Miguel Ojeda
2022-08-17 15:24       ` Arnd Bergmann
2022-08-17 15:24         ` Arnd Bergmann
2022-08-17 15:24         ` Arnd Bergmann
2022-08-17 23:13         ` Miguel Ojeda
2022-08-17 23:13           ` Miguel Ojeda
2022-08-17 23:13           ` Miguel Ojeda
2022-08-17 23:13           ` Miguel Ojeda
2022-08-17 16:11       ` Björn Roy Baron
2022-08-17 16:11         ` Björn Roy Baron
2022-08-17 16:11         ` Björn Roy Baron
2022-08-17 16:11         ` Björn Roy Baron
2022-08-17 22:42         ` Miguel Ojeda
2022-08-17 22:42           ` Miguel Ojeda
2022-08-17 22:42           ` Miguel Ojeda
2022-08-17 22:42           ` Miguel Ojeda
2022-09-06 18:08   ` Masahiro Yamada
2022-09-06 18:08     ` Masahiro Yamada
2022-09-06 18:08     ` Masahiro Yamada
2022-09-06 18:08     ` Masahiro Yamada
2022-09-06 18:08     ` Masahiro Yamada
2022-09-06 23:34     ` Michael Ellerman
2022-09-06 23:34       ` Michael Ellerman
2022-09-06 23:34       ` Michael Ellerman
2022-09-06 23:34       ` Michael Ellerman
2022-09-06 23:34       ` Michael Ellerman
2022-08-02  1:50 ` [PATCH v8 28/31] samples: add Rust examples Miguel Ojeda
2022-08-02 14:07   ` Konstantin Shelekhin
2022-08-02 20:04     ` Wei Liu
2022-08-03  9:23       ` Konstantin Shelekhin
2022-08-04 20:31     ` Miguel Ojeda
2022-08-05 11:40       ` Konstantin Shelekhin
2022-08-06 11:58         ` Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 29/31] MAINTAINERS: Rust Miguel Ojeda
2022-08-02  2:25   ` Boqun Feng
2022-08-02  3:41     ` Miguel Ojeda
2022-08-02 14:53     ` Gary Guo
2022-08-04 10:15     ` bjorn3
2022-08-02  1:50 ` [PATCH v8 30/31] [RFC] drivers: gpio: PrimeCell PL061 in Rust Miguel Ojeda
2022-08-02  1:50 ` [PATCH v8 31/31] [RFC] drivers: android: Binder IPC " Miguel Ojeda
2022-08-02 12:26 ` [PATCH v8 00/31] Rust support Matthew Wilcox
2022-08-02 12:26   ` Matthew Wilcox
2022-08-02 12:26   ` Matthew Wilcox
2022-08-02 12:26   ` Matthew Wilcox
2022-08-02 12:26   ` Matthew Wilcox
2022-08-02 13:45   ` Miguel Ojeda
2022-08-02 13:45     ` Miguel Ojeda
2022-08-02 13:45     ` Miguel Ojeda
2022-08-02 13:45     ` Miguel Ojeda
2022-08-02 13:45     ` Miguel Ojeda
2022-08-02 13:48     ` Christoph Hellwig
2022-08-02 13:48       ` Christoph Hellwig
2022-08-02 13:48       ` Christoph Hellwig
2022-08-02 13:48       ` Christoph Hellwig
2022-08-02 13:48       ` Christoph Hellwig
2022-08-02 14:16       ` Miguel Ojeda
2022-08-02 14:16         ` Miguel Ojeda
2022-08-02 14:16         ` Miguel Ojeda
2022-08-02 14:16         ` Miguel Ojeda
2022-08-02 14:16         ` Miguel Ojeda
2022-08-02 14:01     ` Matthew Wilcox
2022-08-02 14:01       ` Matthew Wilcox
2022-08-02 14:01       ` Matthew Wilcox
2022-08-02 14:01       ` Matthew Wilcox
2022-08-02 14:01       ` Matthew Wilcox
2022-08-02 15:09       ` Miguel Ojeda
2022-08-02 15:09         ` Miguel Ojeda
2022-08-02 15:09         ` Miguel Ojeda
2022-08-02 15:09         ` Miguel Ojeda
2022-08-02 15:09         ` Miguel Ojeda
2022-08-02 17:46         ` Miguel Ojeda
2022-08-02 17:46           ` Miguel Ojeda
2022-08-02 17:46           ` Miguel Ojeda
2022-08-02 17:46           ` Miguel Ojeda
2022-08-02 17:46           ` 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=20220802015052.10452-5-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.