Simplify kallsyms offset table generation
Subsystems (4)
v2 cover Simplify kallsyms offset table generation
From: Ard Biesheuvel <[email protected]>
v2:
- implement minimal PC32 support for MIPS so it can tolerate the symbol references emitted by kallsyms
- use signed decimal to represent the offset, so that symbols appearing before _text can be represented correctly.
- add Kees's ack
Ard Biesheuvel (2):
mips: Add support for PC32 relocations in vmlinux
kallsyms: Get rid of kallsyms relative base
arch/mips/boot/tools/relocs.c | 2 + arch/mips/include/asm/elf.h | 2 + kernel/kallsyms.c | 6 +- kernel/kallsyms_internal.h | 1 - kernel/vmcore_info.c | 1 - scripts/kallsyms.c | 64 ++++++-------------- scripts/link-vmlinux.sh | 4 ++ tools/perf/tests/vmlinux-kallsyms.c | 1 - 8 files changed, 29 insertions(+), 52 deletions(-)
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v2 1/2 mips: Add support for PC32 relocations in vmlinux
From: Ard Biesheuvel <[email protected]>
MIPS supports PC32 relocations like most other architectures, which will be used by kallsyms to make its symbol references visible to the linker.
Given that these are place-relative, they can be ignored by the 'relocs' tool, just like other PC type relocations.
--- arch/mips/boot/tools/relocs.c | 2 ++ arch/mips/include/asm/elf.h | 2 ++ 2 files changed, 4 insertions(+)
arch/mips/boot/tools/relocs.c +2 -0
@@ -79,6 +79,7 @@ static const char *rel_type(unsigned type)REL_TYPE (R_MIPS_HIGHEST ),REL_TYPE (R_MIPS_PC21_S2 ),REL_TYPE (R_MIPS_PC26_S2 ), +REL_TYPE (R_MIPS_PC32 ), #undef REL_TYPE };const char *name ="unknown type rel type name" ;
@@ -522,6 +523,7 @@ static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,case R_MIPS_PC16 :case R_MIPS_PC21_S2 :case R_MIPS_PC26_S2 : +case R_MIPS_PC32 :/* * NONE can be ignored and PC relative relocations don't * need to be adjusted.
arch/mips/include/asm/elf.h +2 -0
@@ -123,6 +123,8 @@#define R_MIPS_LOVENDOR 100#define R_MIPS_HIVENDOR 127 +#define R_MIPS_PC32 248 +#define SHN_MIPS_ACCOMON 0xff00/* Allocated common symbols */ #define SHN_MIPS_TEXT 0xff01/* Allocated test symbols. */ #define SHN_MIPS_DATA 0xff02/* Allocated data symbols. */
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v2 2/2 kallsyms: Get rid of kallsyms relative base
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
When the kallsyms relative base was introduced, per-CPU variable references on x86_64 SMP were implemented as offsets into the respective per-CPU region, rather than offsets relative to the location of the variable's template in the kernel image, which is how other architectures implement it.
This required kallsyms to reason about the difference between the two, and the sign of the value in the kallsyms_offsets[] array was used to distinguish them. This meant that negative offsets were not permitted for ordinary variables, and so it was crucial that the relative base was chosen such that all offsets were positive numbers.
This is no longer needed: instead, the offsets can simply be encoded as values in the range -/+ 2 GiB, which is precisely what PC32 relocations provide on most architectures. So it is possible to simplify the logic, and just use _text as the anchor directly, and let the linker calculate the final value based on the location of the entry itself.
Some architectures (nios2, extensa) do not support place-relative relocations at all, but these are all 32-bit and non-relocatable, and so there is no need for place-relative relocations in the first place, and the actual symbol values can just be stored directly.
This makes all entries in the kallsyms_offsets[] array visible as place-relative references in the ELF metadata, which will be important when implementing ELF-based fg-kaslr.
--- kernel/kallsyms.c | 6 +- kernel/kallsyms_internal.h | 1 - kernel/vmcore_info.c | 1 - scripts/kallsyms.c | 64 ++++++-------------- scripts/link-vmlinux.sh | 4 ++ tools/perf/tests/vmlinux-kallsyms.c | 1 - 6 files changed, 25 insertions(+), 52 deletions(-)
kernel/kallsyms.c +4 -2
@@ -151,8 +151,10 @@ static unsigned int get_symbol_offset(unsigned long pos)unsigned long kallsyms_sym_address (int idx) { -/* values are unsigned offsets */ -return kallsyms_relative_base + (u32 )kallsyms_offsets[idx]; +/* non-relocatable 32-bit kernels just embed the value directly */ +if (!IS_ENABLED (CONFIG_64BIT ) && !IS_ENABLED (CONFIG_RELOCATABLE )) +return (u32 )kallsyms_offsets[idx]; +return (unsigned long )offset_to_ptr (kallsyms_offsets + idx); }static unsigned int get_symbol_seq (int index)
kernel/kallsyms_internal.h +0 -1
@@ -8,7 +8,6 @@ extern const int kallsyms_offsets[];extern const u8 kallsyms_names[];extern const unsigned int kallsyms_num_syms; -extern const unsigned long kallsyms_relative_base;extern const char kallsyms_token_table[];extern const u16 kallsyms_token_index[];
kernel/vmcore_info.c +0 -1
@@ -238,7 +238,6 @@ static int __init crash_save_vmcoreinfo_init(void)VMCOREINFO_SYMBOL (kallsyms_token_table);VMCOREINFO_SYMBOL (kallsyms_token_index);VMCOREINFO_SYMBOL (kallsyms_offsets); -VMCOREINFO_SYMBOL (kallsyms_relative_base);#endif /* CONFIG_KALLSYMS */ arch_crash_save_vmcoreinfo ();
scripts/kallsyms.c +17 -47
115 lines not on this page · raw message
scripts/link-vmlinux.sh +4 -0
@@ -143,6 +143,10 @@ kallsyms() kallsymopt="${kallsymopt} --all-symbols" fi + if is_enabled CONFIG_64BIT || is_enabled CONFIG_RELOCATABLE; then + kallsymopt="${kallsymopt} --pc-relative" + fi + info KSYMS "${2}.S" scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
tools/perf/tests/vmlinux-kallsyms.c +0 -1
@@ -27,7 +27,6 @@ static bool is_ignored_symbol(const char *name, char type)* stable symbol list. */ "kallsyms_offsets" , -"kallsyms_relative_base" ,"kallsyms_num_syms" ,"kallsyms_names" , "kallsyms_markers",
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Nathan Chancellor
maintainerQuoted 10 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
v2:
- implement minimal PC32 support for MIPS so it can tolerate the symbol references emitted by kallsyms
- use signed decimal to represent the offset, so that symbols appearing before_textcan be represented correctly.
- add Kees's ack[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next
Thanks!
[1/2] mips: Add support for PC32 relocations in vmlinux
(no commit info)
[2/2] kallsyms: Get rid of kallsyms relative base
(no commit info)
Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted. Patches applied to an "unstable" branch are accepted pending wider testing in -next and any post-commit review; they will generally be moved to the main branch in a week if no issues are found.
Best regards,
Signature · 1 line
Nathan Chancellor <[email protected]>