Skip to content

Commit f58bb0d

Browse files
AaronDotopsiff
authored andcommitted
kallsyms: Avoid weak references for kallsyms symbols
commit 951bcae upstream. 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 <[email protected]> # Boot Reviewed-by: Nick Desaulniers <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Link: https://lkml.kernel.org/r/20230504174320.3930345-1-ardb%40kernel.org Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]> Signed-off-by: Binbin Zhou <[email protected]>
1 parent 79f68e0 commit f58bb0d

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
@@ -446,11 +446,30 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
446446
#endif
447447
#endif
448448

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

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)