Skip to content

Commit 951bcae

Browse files
ardbiesheuvelmasahir0y
authored andcommitted
kallsyms: Avoid weak references for kallsyms symbols
kallsyms is a directory of all the symbols in the vmlinux binary, and so creating it is somewhat of a chicken-and-egg problem, as its non-zero size affects the layout of the binary, and therefore the values of the symbols. For this reason, the kernel is linked more than once, and the first pass does not include any kallsyms data at all. For the linker to accept this, the symbol declarations describing the kallsyms metadata are emitted as having weak linkage, so they can remain unsatisfied. During the subsequent passes, the weak references are satisfied by the kallsyms metadata that was constructed based on information gathered from the preceding passes. Weak references lead to somewhat worse codegen, because taking their address may need to produce NULL (if the reference was unsatisfied), and this is not usually supported by RIP or PC relative symbol references. Given that these references are ultimately always satisfied in the final link, let's drop the weak annotation, and instead, provide fallback definitions in the linker script that are only emitted if an unsatisfied reference exists. While at it, drop the FRV specific annotation that these symbols reside in .rodata - FRV is long gone. Tested-by: Nick Desaulniers <ndesaulniers@google.com> # Boot Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> Acked-by: Arnd Bergmann <arnd@arndb.de> Link: https://lkml.kernel.org/r/20230504174320.3930345-1-ardb%40kernel.org Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
1 parent c3f7bed commit 951bcae

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

include/asm-generic/vmlinux.lds.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,30 @@
448448
#endif
449449
#endif
450450

451+
/*
452+
* Some symbol definitions will not exist yet during the first pass of the
453+
* link, but are guaranteed to exist in the final link. Provide preliminary
454+
* definitions that will be superseded in the final link to avoid having to
455+
* rely on weak external linkage, which requires a GOT when used in position
456+
* independent code.
457+
*/
458+
#define PRELIMINARY_SYMBOL_DEFINITIONS \
459+
PROVIDE(kallsyms_addresses = .); \
460+
PROVIDE(kallsyms_offsets = .); \
461+
PROVIDE(kallsyms_names = .); \
462+
PROVIDE(kallsyms_num_syms = .); \
463+
PROVIDE(kallsyms_relative_base = .); \
464+
PROVIDE(kallsyms_token_table = .); \
465+
PROVIDE(kallsyms_token_index = .); \
466+
PROVIDE(kallsyms_markers = .); \
467+
PROVIDE(kallsyms_seqs_of_names = .);
468+
451469
/*
452470
* Read only Data
453471
*/
454472
#define RO_DATA(align) \
455473
. = ALIGN((align)); \
474+
PRELIMINARY_SYMBOL_DEFINITIONS \
456475
.rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \
457476
__start_rodata = .; \
458477
*(.rodata) *(.rodata.*) \

kernel/kallsyms.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,6 @@ static unsigned long get_symbol_pos(unsigned long addr,
325325
unsigned long symbol_start = 0, symbol_end = 0;
326326
unsigned long i, low, high, mid;
327327

328-
/* This kernel should never had been booted. */
329-
if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
330-
BUG_ON(!kallsyms_addresses);
331-
else
332-
BUG_ON(!kallsyms_offsets);
333-
334328
/* Do a binary search on the sorted kallsyms_addresses array. */
335329
low = 0;
336330
high = kallsyms_num_syms;

kernel/kallsyms_internal.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,21 @@
55
#include <linux/types.h>
66

77
/*
8-
* These will be re-linked against their real values
9-
* during the second link stage.
8+
* These will be re-linked against their real values during the second link
9+
* stage. Preliminary values must be provided in the linker script using the
10+
* PROVIDE() directive so that the first link stage can complete successfully.
1011
*/
11-
extern const unsigned long kallsyms_addresses[] __weak;
12-
extern const int kallsyms_offsets[] __weak;
13-
extern const u8 kallsyms_names[] __weak;
12+
extern const unsigned long kallsyms_addresses[];
13+
extern const int kallsyms_offsets[];
14+
extern const u8 kallsyms_names[];
1415

15-
/*
16-
* Tell the compiler that the count isn't in the small data section if the arch
17-
* has one (eg: FRV).
18-
*/
19-
extern const unsigned int kallsyms_num_syms
20-
__section(".rodata") __attribute__((weak));
21-
22-
extern const unsigned long kallsyms_relative_base
23-
__section(".rodata") __attribute__((weak));
16+
extern const unsigned int kallsyms_num_syms;
17+
extern const unsigned long kallsyms_relative_base;
2418

25-
extern const char kallsyms_token_table[] __weak;
26-
extern const u16 kallsyms_token_index[] __weak;
19+
extern const char kallsyms_token_table[];
20+
extern const u16 kallsyms_token_index[];
2721

28-
extern const unsigned int kallsyms_markers[] __weak;
29-
extern const u8 kallsyms_seqs_of_names[] __weak;
22+
extern const unsigned int kallsyms_markers[];
23+
extern const u8 kallsyms_seqs_of_names[];
3024

3125
#endif // LINUX_KALLSYMS_INTERNAL_H_

0 commit comments

Comments
 (0)