generic: 5.15: backport patch supporting "big" kernel symbols
[openwrt/openwrt.git] / target / linux / generic / backport-5.15 / 202-v6.1-kallsyms-support-big-kernel-symbols.patch
1 From 73bbb94466fd3f8b313eeb0b0467314a262dddb3 Mon Sep 17 00:00:00 2001
2 From: Miguel Ojeda <ojeda@kernel.org>
3 Date: Mon, 5 Apr 2021 04:58:39 +0200
4 Subject: [PATCH] kallsyms: support "big" kernel symbols
5
6 Rust symbols can become quite long due to namespacing introduced
7 by modules, types, traits, generics, etc.
8
9 Increasing to 255 is not enough in some cases, therefore
10 introduce longer lengths to the symbol table.
11
12 In order to avoid increasing all lengths to 2 bytes (since most
13 of them are small, including many Rust ones), use ULEB128 to
14 keep smaller symbols in 1 byte, with the rest in 2 bytes.
15
16 Reviewed-by: Kees Cook <keescook@chromium.org>
17 Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
18 Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
19 Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
20 Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
21 Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
22 Co-developed-by: Gary Guo <gary@garyguo.net>
23 Signed-off-by: Gary Guo <gary@garyguo.net>
24 Co-developed-by: Boqun Feng <boqun.feng@gmail.com>
25 Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
26 Co-developed-by: Matthew Wilcox <willy@infradead.org>
27 Signed-off-by: Matthew Wilcox <willy@infradead.org>
28 Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
29 ---
30 kernel/kallsyms.c | 26 ++++++++++++++++++++++----
31 scripts/kallsyms.c | 29 ++++++++++++++++++++++++++---
32 2 files changed, 48 insertions(+), 7 deletions(-)
33
34 --- a/kernel/kallsyms.c
35 +++ b/kernel/kallsyms.c
36 @@ -69,12 +69,20 @@ static unsigned int kallsyms_expand_symb
37 data = &kallsyms_names[off];
38 len = *data;
39 data++;
40 + off++;
41 +
42 + /* If MSB is 1, it is a "big" symbol, so needs an additional byte. */
43 + if ((len & 0x80) != 0) {
44 + len = (len & 0x7F) | (*data << 7);
45 + data++;
46 + off++;
47 + }
48
49 /*
50 * Update the offset to return the offset for the next symbol on
51 * the compressed stream.
52 */
53 - off += len + 1;
54 + off += len;
55
56 /*
57 * For every byte on the compressed symbol data, copy the table
58 @@ -127,7 +135,7 @@ static char kallsyms_get_symbol_type(uns
59 static unsigned int get_symbol_offset(unsigned long pos)
60 {
61 const u8 *name;
62 - int i;
63 + int i, len;
64
65 /*
66 * Use the closest marker we have. We have markers every 256 positions,
67 @@ -141,8 +149,18 @@ static unsigned int get_symbol_offset(un
68 * so we just need to add the len to the current pointer for every
69 * symbol we wish to skip.
70 */
71 - for (i = 0; i < (pos & 0xFF); i++)
72 - name = name + (*name) + 1;
73 + for (i = 0; i < (pos & 0xFF); i++) {
74 + len = *name;
75 +
76 + /*
77 + * If MSB is 1, it is a "big" symbol, so we need to look into
78 + * the next byte (and skip it, too).
79 + */
80 + if ((len & 0x80) != 0)
81 + len = ((len & 0x7F) | (name[1] << 7)) + 1;
82 +
83 + name = name + len + 1;
84 + }
85
86 return name - kallsyms_names;
87 }
88 --- a/scripts/kallsyms.c
89 +++ b/scripts/kallsyms.c
90 @@ -470,12 +470,35 @@ static void write_src(void)
91 if ((i & 0xFF) == 0)
92 markers[i >> 8] = off;
93
94 - printf("\t.byte 0x%02x", table[i]->len);
95 + /* There cannot be any symbol of length zero. */
96 + if (table[i]->len == 0) {
97 + fprintf(stderr, "kallsyms failure: "
98 + "unexpected zero symbol length\n");
99 + exit(EXIT_FAILURE);
100 + }
101 +
102 + /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
103 + if (table[i]->len > 0x3FFF) {
104 + fprintf(stderr, "kallsyms failure: "
105 + "unexpected huge symbol length\n");
106 + exit(EXIT_FAILURE);
107 + }
108 +
109 + /* Encode length with ULEB128. */
110 + if (table[i]->len <= 0x7F) {
111 + /* Most symbols use a single byte for the length. */
112 + printf("\t.byte 0x%02x", table[i]->len);
113 + off += table[i]->len + 1;
114 + } else {
115 + /* "Big" symbols use two bytes. */
116 + printf("\t.byte 0x%02x, 0x%02x",
117 + (table[i]->len & 0x7F) | 0x80,
118 + (table[i]->len >> 7) & 0x7F);
119 + off += table[i]->len + 2;
120 + }
121 for (k = 0; k < table[i]->len; k++)
122 printf(", 0x%02x", table[i]->sym[k]);
123 printf("\n");
124 -
125 - off += table[i]->len + 1;
126 }
127 printf("\n");
128