kallsyms: embed source file:line info in kernel stack traces
Subsystems (4)
v3 cover kallsyms: embed source file:line info in kernel stack traces
This series adds CONFIG_KALLSYMS_LINEINFO, which embeds source file:line information directly in the kernel image so that stack traces annotate every frame with the originating source location - no external tools, no debug symbols at runtime, and safe to use in NMI/panic context.
Motivation
==========
The recent "slowly decommission bugzilla?" thread surfaced a recurring
problem: when users encounter kernel crashes they see stack traces like func+0x1ec/0x240 but have no way to identify which subsystem or maintainer to contact. Richard Weinberger proposed building a database mapping symbols to source files using nm/DWARF. Linus pointed to scripts/decode_stacktrace.sh as the existing solution. But as the discussion progressed, it became clear that decode_stacktrace.sh has significant practical barriers that prevent it from being useful in the common case.
Problems with scripts/decode_stacktrace.sh
==========================================
- Requires debug symbols: the script needs vmlinux with DWARF debug info. Many distros don't retain debug symbols for older or security kernels, and even when available, asking users to obtain matching debuginfo packages is a significant hurdle.
- Requires toolchain: users need addr2line and nm installed.
- Version-matching requirement: debug symbols must exactly match the running kernel binary.
What this series does
=====================
Patch 1: CONFIG_KALLSYMS_LINEINFO
At build time, a host tool (scripts/gen_lineinfo) reads DWARF .debug_line from vmlinux, extracts address-to-file:line mappings, and embeds them as sorted lookup tables in .rodata. At runtime, kallsyms_lookup_lineinfo() binary-searches the table and __sprint_symbol() appends "(file:line)" to each stack frame. NMI/panic- safe (no locks, no allocations), KASLR-compatible.
Patch 2: CONFIG_KALLSYMS_LINEINFO_MODULES
Extends lineinfo to loadable modules. Each .ko gets a .mod_lineinfo section embedded at build time. The module loader picks it up at load time. Same zero-allocation, NMI-safe lookup.
Patch 3: delta compression
Block-indexed delta-encoding with LEB128 varints, implementing the approach suggested by Juergen Gross in the RFC review. Reduces overhead from ~44 MiB to ~11 MiB (~3.7 bytes/entry), addressing the primary size concern from the RFC.
Patch 4: KUnit tests
30 KUnit tests covering the lineinfo lookup paths, delta-decode logic, boundary conditions, and integration with the backtrace formatting APIs.
Example output
==============
[ 11.206749] dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:94) [ 11.207403] vpanic+0x36e/0x620 (kernel/panic.c:650) [ 11.209324] panic+0xc9/0xd0 (kernel/panic.c:787) [ 11.213312] sysrq_handle_crash+0x1a/0x20 (drivers/tty/sysrq.c:154) [ 11.214005] __handle_sysrq.cold+0x66/0x256 (drivers/tty/sysrq.c:611) [ 11.214712] write_sysrq_trigger+0x65/0x80 (drivers/tty/sysrq.c:1221) [ 11.215424] proc_reg_write+0x1bd/0x3c0 (fs/proc/inode.c:330) [ 11.216061] vfs_write+0x1c6/0xff0 (fs/read_write.c:686) [ 11.218848] ksys_write+0xfa/0x200 (fs/read_write.c:740) [ 11.222394] do_syscall_64+0xf3/0x690 (arch/x86/entry/syscall_64.c:63)
Size impact
===========
Measured with a Debian kernel config:
- bzImage: +3.6 MiB (14 MiB -> 18 MiB, +26%)
- Runtime memory: +5.9 MiB (text+data+bss)
- Code overhead: +5.0 KiB (.text, lookup functions only)
- Data overhead: +5.9 MiB (.data, lineinfo tables)
Lineinfo data breakdown:
- lineinfo_data (delta-compressed): 5,728 KiB (97%)
- lineinfo_block_addrs: 99 KiB
- lineinfo_block_offsets: 99 KiB
- lineinfo_filenames: 111 KiB
- lineinfo_file_offsets: 17 KiB
The ~5.9 MiB is after 2.7x delta compression; uncompressed would be ~16 MiB. This is a fraction of the cost of shipping full DWARF debug info (hundreds of MiB), which distros must store and serve for every kernel version.
For distros, maintaining debug symbol repositories is expensive: storage, mirrors, and CDN bandwidth for hundreds of MiB per kernel build add up quickly. A ~5.9 MiB increase in the kernel image itself is a modest cost that eliminates the need for users to find, download, and version-match debuginfo packages just to make a crash report useful.
For developers, the file:line annotations appear immediately in crash traces - no post-processing with decode_stacktrace.sh needed.
Changes since v2
=================
- Replace #ifdef CONFIG_KALLSYMS_LINEINFO with IS_ENABLED() throughout, so the compiler checks the code for syntax errors regardless of configuration. (Suggested by Helge Deller)
- Replace zigzag + ULEB128 encoding of signed deltas with native SLEB128, removing the unnecessary zigzag transform layer.
(Suggested by Vivian Wang)
- Deduplicate the binary search and delta-decode logic: extract shared struct lineinfo_table and lineinfo_search() into mod_lineinfo.h instead of maintaining near-identical copies in kernel/kallsyms.c and kernel/module/kallsyms.c. (Suggested by Vivian Wang)
- Use .uleb128 / .sleb128 assembler directives in gen_lineinfo output instead of encoding varints in C and emitting .byte hex literals. (Suggested by Vivian Wang)
- Redesign module mod_lineinfo_header to use explicit (offset, size) pairs for each sub-array, similar to flattened devicetree layout. This makes bounds validation straightforward: offset + size <= section_size. (Suggested by Vivian Wang)
- Remove dead sym_start parameter from kallsyms_lookup_lineinfo() and module_lookup_lineinfo().
Changes since v1
=================
- Fix path stripping regression on architectures where DWARF comp_dir is a subdirectory (e.g. arch/parisc/kernel) rather than the source tree
root: paths now correctly show "kernel/traps.c:212" instead of bare "traps.c:212". Added kernel_dirs[] fallback scan and bare-filename recovery via comp_dir. (Reported by Helge Deller)
- Fix RST heading: overline/underline must be at least as long as the heading text in kallsyms-lineinfo.rst. (Reported by Randy Dunlap)
- Fix MAINTAINERS alphabetical ordering: move KALLSYMS LINEINFO entry before KASAN. (Reported by Randy Dunlap)
- Fix arch-portability of .debug_line relocation handling: replace hardcoded R_X86_64_32 with r_type_abs32() supporting x86, arm, arm64, riscv, s390, mips, ppc, loongarch, and parisc.
- Fix vmlinux compressed-path data_end for the last block: use lineinfo_data_size instead of UINT_MAX.
- Add file_offsets[] and filenames_size bounds checks in vmlinux lookup path (the module path already had them).
- Add alignment padding for file_offsets[] in module .mod_lineinfo binary format (data[] is variable-length u8, followed by u32[]).
- Remove sym_start cross-validation check that incorrectly rejected valid lineinfo entries for assembly-adjacent functions.
- Add KUnit test suite (new patch 4/4): 30 tests covering vmlinux lookup, module lookup, delta decode, boundary conditions, and backtrace formatting integration.
Changes since RFC
==================
- Added module support (patch 2)
- Added delta compression (patch 3), reducing size from ~44 MiB to ~11 MiB, addressing the primary concern from RFC review
- Added documentation (Documentation/admin-guide/kallsyms-lineinfo.rst)
- Added MAINTAINERS entry
Sasha Levin (4):
kallsyms: embed source file:line info in kernel stack traces
kallsyms: extend lineinfo to loadable modules
kallsyms: delta-compress lineinfo tables for ~2.7x size reduction
kallsyms: add KUnit tests for lineinfo feature
Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 97 ++ MAINTAINERS | 9 + include/linux/kallsyms.h | 30 +- include/linux/mod_lineinfo.h | 243 +++++ include/linux/module.h | 16 + init/Kconfig | 35 + kernel/kallsyms.c | 59 ++ kernel/kallsyms_internal.h | 13 + kernel/module/kallsyms.c | 75 ++ kernel/module/main.c | 4 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 813 +++++++++++++++++ scripts/.gitignore | 1 + scripts/Makefile | 4 + scripts/Makefile.modfinal | 6 + scripts/empty_lineinfo.S | 38 + scripts/gen-mod-lineinfo.sh | 48 + scripts/gen_lineinfo.c | 850 ++++++++++++++++++ scripts/kallsyms.c | 17 + scripts/link-vmlinux.sh | 43 +- 22 files changed, 2411 insertions(+), 4 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 include/linux/mod_lineinfo.h create mode 100644 lib/tests/lineinfo_kunit.c create mode 100644 scripts/empty_lineinfo.S create mode 100755 scripts/gen-mod-lineinfo.sh create mode 100644 scripts/gen_lineinfo.c
Signature · 1 line
2.51.0
PATCH v3 1/4 kallsyms: embed source file:line info in kernel stack traces
Add CONFIG_KALLSYMS_LINEINFO, which embeds a compact address-to-line lookup table in the kernel image so stack traces directly print source file and line number information:
root@localhost:~# echo c > /proc/sysrq-trigger [ 11.201987] sysrq: Trigger a crash [ 11.202831] Kernel panic - not syncing: sysrq triggered crash [ 11.206218] Call Trace: [ 11.206501] <TASK> [ 11.206749] dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:94) [ 11.207403] vpanic+0x36e/0x620 (kernel/panic.c:650) [ 11.208565] ? __lock_acquire+0x465/0x2240 (kernel/locking/lockdep.c:4674) [ 11.209324] panic+0xc9/0xd0 (kernel/panic.c:787) [ 11.211873] ? find_held_lock+0x2b/0x80 (kernel/locking/lockdep.c:5350) [ 11.212597] ? lock_release+0xd3/0x300 (kernel/locking/lockdep.c:5535) [ 11.213312] sysrq_handle_crash+0x1a/0x20 (drivers/tty/sysrq.c:154) [ 11.214005] __handle_sysrq.cold+0x66/0x256 (drivers/tty/sysrq.c:611) [ 11.214712] write_sysrq_trigger+0x65/0x80 (drivers/tty/sysrq.c:1221) [ 11.215424] proc_reg_write+0x1bd/0x3c0 (fs/proc/inode.c:330) [ 11.216061] vfs_write+0x1c6/0xff0 (fs/read_write.c:686) [ 11.218848] ksys_write+0xfa/0x200 (fs/read_write.c:740) [ 11.222394] do_syscall_64+0xf3/0x690 (arch/x86/entry/syscall_64.c:63) [ 11.223942] entry_SYSCALL_64_after_hwframe+0x77/0x7f (arch/x86/entry/entry_64.S:121)
At build time, a new host tool (scripts/gen_lineinfo) reads DWARF .debug_line from vmlinux using libdw (elfutils), extracts all address-to-file:line mappings, and generates an assembly file with sorted parallel arrays (offsets from _text, file IDs, and line numbers). These are linked into vmlinux as .rodata.
At runtime, kallsyms_lookup_lineinfo() does a binary search on the table and __sprint_symbol() appends "(file:line)" to each stack frame. The lookup uses offsets from _text so it works with KASLR, requires no locks or allocations, and is safe in any context including panic.
The feature requires CONFIG_DEBUG_INFO (for DWARF data) and elfutils (libdw-dev) on the build host.
Memory footprint measured with a 1852-option x86_64 config:
Table: 4,597,583 entries from 4,841 source files
lineinfo_addrs[] 4,597,583 x u32 = 17.5 MiB
lineinfo_file_ids[] 4,597,583 x u16 = 8.8 MiB
lineinfo_lines[] 4,597,583 x u32 = 17.5 MiB
file_offsets + filenames ~ 0.1 MiBTotal .rodata increase: ~ 44.0 MiB
vmlinux (stripped): 529 MiB -> 573 MiB (+44 MiB / +8.3%)
Note: this probably won't be something we roll into "production", but it might be useful for the average user given the relatively low memory footprint, in canary deployments for hyperscalers, or by default for folks who run tests/fuzzing/etc.
Disclaimer: this was vibe coded over an afternoon with an AI coding assistant.
The .config used for testing is a simple KVM guest configuration for local development and testing.
--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 72 +++ MAINTAINERS | 6 + include/linux/kallsyms.h | 17 +- init/Kconfig | 20 + kernel/kallsyms.c | 56 ++ kernel/kallsyms_internal.h | 9 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/empty_lineinfo.S | 30 ++ scripts/gen_lineinfo.c | 510 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 43 +- 13 files changed, 780 insertions(+), 4 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 scripts/empty_lineinfo.S create mode 100644 scripts/gen_lineinfo.c
Documentation/admin-guide/index.rst +1 -0
@@ -73,6 +73,7 @@ problems and bugs in particular. ramoops dynamic-debug-howto init + kallsyms-lineinfo kdump/index perf/index pstore-blk
Documentation/admin-guide/kallsyms-lineinfo.rst new file +72 -0
72 lines not on this page · raw message
MAINTAINERS +6 -0
@@ -13728,6 +13728,12 @@ S: Maintained F: Documentation/hwmon/k8temp.rst F: drivers/hwmon/k8temp.c +KALLSYMS LINEINFO +M: Sasha Levin <[email protected]> +S: Maintained +F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: scripts/gen_lineinfo.c + KASAN M: Andrey Ryabinin <[email protected]> R: Alexander Potapenko <[email protected]>
include/linux/kallsyms.h +16 -1
@@ -16,10 +16,15 @@#include <asm/sections.h> #define KSYM_NAME_LEN 512 + +/* Extra space for " (path/to/file.c:12345)" suffix when lineinfo is enabled */ +#define KSYM_LINEINFO_LEN (IS_ENABLED(CONFIG_KALLSYMS_LINEINFO) ? 128 : 0) +#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \ (KSYM_NAME_LEN - 1) + \ 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \ - (BUILD_ID_SIZE_MAX * 2) + 1) + (BUILD_ID_SIZE_MAX * 2) + 1 + \ + KSYM_LINEINFO_LEN)struct cred ;struct module ;
@@ -96,6 +101,9 @@ extern int sprint_backtrace_build_id(char *buffer, unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname); +bool kallsyms_lookup_lineinfo (unsigned long addr, +const char **file,unsigned int *line); +#else /* !CONFIG_KALLSYMS */ static inline unsigned long kallsyms_lookup_name (const char *name)
@@ -164,6 +172,13 @@ static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long) {return -EOPNOTSUPP ; } + +static inline bool kallsyms_lookup_lineinfo (unsigned long addr, +const char **file, +unsigned int *line) +{ +return false; +}#endif /*CONFIG_KALLSYMS*/ static inline void print_ip_sym (const char *loglvl,unsigned long ip)
init/Kconfig +20 -0
@@ -2050,6 +2050,26 @@ config KALLSYMS_ALL Say N unless you really need all symbols, or kernel live patching. +config KALLSYMS_LINEINFO + bool "Embed source file:line information in stack traces" + depends on KALLSYMS && DEBUG_INFO + help + Embeds an address-to-source-line mapping table in the kernel + image so that stack traces directly include file:line information, + similar to what scripts/decode_stacktrace.sh provides but without + needing external tools or a vmlinux with debug info at runtime. + + When enabled, stack traces will look like: + + kmem_cache_alloc_noprof+0x60/0x630 (mm/slub.c:3456) + anon_vma_clone+0x2ed/0xcf0 (mm/rmap.c:412) + + This requires elfutils (libdw-dev/elfutils-devel) on the build host. + Adds approximately 44MB to a typical kernel image (10 bytes per + DWARF line-table entry, ~4.6M entries for a typical config). + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKS
kernel/kallsyms.c +56 -0
68 lines not on this page · raw message
kernel/kallsyms_internal.h +9 -0
@@ -15,4 +15,13 @@ extern const u16 kallsyms_token_index[];extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[]; +extern const u32 lineinfo_num_entries; +extern const u32 lineinfo_addrs[]; +extern const u16 lineinfo_file_ids[]; +extern const u32 lineinfo_lines[]; +extern const u32 lineinfo_num_files; +extern const u32 lineinfo_file_offsets[]; +extern const u32 lineinfo_filenames_size; +extern const char lineinfo_filenames[]; +#endif // LINUX_KALLSYMS_INTERNAL_H_
scripts/.gitignore +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only /asn1_compiler +/gen_lineinfo /gen_packed_field_checks /generate_rust_target /insert-sys-cert
scripts/Makefile +3 -0
@@ -4,6 +4,7 @@ # the kernel for the build process. hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
@@ -36,6 +37,8 @@ HOSTLDLIBS_sorttable = -lpthread HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include HOSTCFLAGS_sign-file.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) HOSTLDLIBS_sign-file = $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) +HOSTCFLAGS_gen_lineinfo.o = $(shell $(HOSTPKG_CONFIG) --cflags libdw 2> /dev/null) +HOSTLDLIBS_gen_lineinfo = $(shell $(HOSTPKG_CONFIG) --libs libdw 2> /dev/null || echo -ldw -lelf -lz) ifdef CONFIG_UNWINDER_ORC ifeq ($(ARCH),x86_64)
scripts/empty_lineinfo.S new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2026 Sasha Levin <[email protected]> + * + * Empty lineinfo stub for the initial vmlinux link. + * The real lineinfo is generated from .tmp_vmlinux1 by gen_lineinfo. + */ + .section .rodata, "a" + .globl lineinfo_num_entries + .balign 4 +lineinfo_num_entries: + .long 0 + .globl lineinfo_num_files + .balign 4 +lineinfo_num_files: + .long 0 + .globl lineinfo_addrs +lineinfo_addrs: + .globl lineinfo_file_ids +lineinfo_file_ids: + .globl lineinfo_lines +lineinfo_lines: + .globl lineinfo_file_offsets +lineinfo_file_offsets: + .globl lineinfo_filenames_size + .balign 4 +lineinfo_filenames_size: + .long 0 + .globl lineinfo_filenames +lineinfo_filenames:
scripts/gen_lineinfo.c new file +510 -0
510 lines not on this page · raw message
scripts/kallsyms.c +16 -0
@@ -78,6 +78,17 @@ static char *sym_name(const struct sym_entry *s)static bool is_ignored_symbol (const char *name,char type) { +/* Ignore lineinfo symbols for kallsyms pass stability */ +static const char *const lineinfo_syms[] = { +"lineinfo_addrs" , +"lineinfo_file_ids" , +"lineinfo_file_offsets" , +"lineinfo_filenames" , +"lineinfo_lines" , +"lineinfo_num_entries" , +"lineinfo_num_files" , + }; +if (type =='u' || type =='n' )return true;
@@ -90,6 +101,11 @@ static bool is_ignored_symbol(const char *name, char type)return true; } +for (size_t i =0 ; i <ARRAY_SIZE (lineinfo_syms); i++) { +if (!strcmp (name, lineinfo_syms[i])) +return true; + } +return false; }
scripts/link-vmlinux.sh +40 -3
81 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 2/4 kallsyms: extend lineinfo to loadable modules
In reply to Sasha LevinAdd CONFIG_KALLSYMS_LINEINFO_MODULES, which extends the CONFIG_KALLSYMS_LINEINFO feature to loadable kernel modules.
At build time, each .ko is post-processed by scripts/gen-mod-lineinfo.sh (modeled on gen-btf.sh) which runs scripts/gen_lineinfo --module on the .ko, generates a .mod_lineinfo section containing a compact binary table of .text-relative offsets, file IDs, line numbers, and filenames, and embeds it back into the .ko via objcopy.
At runtime, module_lookup_lineinfo() performs a binary search on the module's .mod_lineinfo section, and __sprint_symbol() calls it for addresses that fall within a module. The lookup is NMI/panic-safe (no locks, no allocations) — the data lives in read-only module memory and is freed automatically when the module is unloaded.
The gen_lineinfo tool gains --module mode which:
- Uses .text section address as base (ET_REL files have no _text symbol)
- Filters entries to .text-only (excludes .init.text/.exit.text)
- Handles libdw's ET_REL path-doubling quirk in make_relative()
- Outputs a flat binary-format section instead of named global symbols
Per-module overhead is approximately 10 bytes per DWARF line entry.
--- .../admin-guide/kallsyms-lineinfo.rst | 40 +- MAINTAINERS | 2 + include/linux/mod_lineinfo.h | 68 ++++ include/linux/module.h | 5 + init/Kconfig | 13 + kernel/kallsyms.c | 18 +- kernel/module/kallsyms.c | 91 +++++ kernel/module/main.c | 3 + scripts/Makefile | 1 + scripts/Makefile.modfinal | 6 + scripts/gen-mod-lineinfo.sh | 48 +++ scripts/gen_lineinfo.c | 349 ++++++++++++++++-- 12 files changed, 604 insertions(+), 40 deletions(-) create mode 100644 include/linux/mod_lineinfo.h create mode 100755 scripts/gen-mod-lineinfo.sh
Documentation/admin-guide/kallsyms-lineinfo.rst +32 -8
@@ -51,22 +51,46 @@ With ``CONFIG_KALLSYMS_LINEINFO``:: Note that assembly routines (such as ``entry_SYSCALL_64_after_hwframe``) are not annotated because they lack DWARF debug information. +Module Support +============== + +``CONFIG_KALLSYMS_LINEINFO_MODULES`` extends the feature to loadable kernel +modules. When enabled, each ``.ko`` is post-processed at build time to embed +a ``.mod_lineinfo`` section containing the same kind of address-to-source +mapping. + +Enable in addition to the base options:: + + CONFIG_MODULES=y + CONFIG_KALLSYMS_LINEINFO_MODULES=y + +Stack traces from module code will then include annotations:: + + my_driver_func+0x30/0x100 [my_driver] (drivers/foo/bar.c:123) + +The ``.mod_lineinfo`` section is loaded into read-only module memory alongside +the module text. No additional runtime memory allocation is required; the data +is freed when the module is unloaded. + Memory Overhead =============== -The lineinfo tables are stored in ``.rodata`` and typically add approximately -44 MiB to the kernel image for a standard configuration (~4.6 million DWARF -line entries, ~10 bytes per entry after deduplication). +The vmlinux lineinfo tables are stored in ``.rodata`` and typically add +approximately 44 MiB to the kernel image for a standard configuration +(~4.6 million DWARF line entries, ~10 bytes per entry after deduplication). + +Per-module lineinfo adds approximately 10 bytes per DWARF line entry to each +``.ko`` file. Known Limitations ================= -- **vmlinux only**: Only symbols in the core kernel image are annotated. - Module symbols are not covered. -- **4 GiB offset limit**: Address offsets from ``_text`` are stored as 32-bit - values. Entries beyond 4 GiB from ``_text`` are skipped at build time with - a warning. +- **4 GiB offset limit**: Address offsets from ``_text`` (vmlinux) or + ``.text`` base (modules) are stored as 32-bit values. Entries beyond + 4 GiB are skipped at build time with a warning. - **65535 file limit**: Source file IDs are stored as 16-bit values. Builds with more than 65535 unique source files will fail with an error. - **No assembly annotations**: Functions implemented in assembly that lack DWARF ``.debug_line`` data are not annotated. +- **No init text**: For modules, functions in ``.init.text`` are not annotated + because that memory is freed after module initialization.
MAINTAINERS +2 -0
@@ -13732,6 +13732,8 @@ KALLSYMS LINEINFO M: Sasha Levin <[email protected]> S: Maintained F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: include/linux/mod_lineinfo.h +F: scripts/gen-mod-lineinfo.sh F: scripts/gen_lineinfo.c KASAN
include/linux/mod_lineinfo.h new file +68 -0
68 lines not on this page · raw message
include/linux/module.h +5 -0
@@ -508,6 +508,8 @@ struct module {void *btf_data;void *btf_base_data;#endif +void *lineinfo_data;/* .mod_lineinfo section in MOD_RODATA */ +unsigned int lineinfo_data_size;#ifdef CONFIG_JUMP_LABEL struct jump_entry *jump_entries;unsigned int num_jump_entries;
@@ -1021,6 +1023,9 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,#endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ +bool module_lookup_lineinfo (struct module *mod,unsigned long addr, +const char **file,unsigned int *line); +/* Define __free(module_put) macro for struct module *. */ DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))
init/Kconfig +13 -0
@@ -2070,6 +2070,19 @@ config KALLSYMS_LINEINFO If unsure, say N. +config KALLSYMS_LINEINFO_MODULES + bool "Embed source file:line information in module stack traces" + depends on KALLSYMS_LINEINFO && MODULES + help + Extends KALLSYMS_LINEINFO to loadable kernel modules. Each .ko + gets a lineinfo table generated from its DWARF data at build time, + so stack traces from module code include (file.c:123) annotations. + + Requires elfutils (libdw-dev/elfutils-devel) on the build host. + Increases .ko sizes by approximately 10 bytes per DWARF line entry. + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKS
kernel/kallsyms.c +15 -3
@@ -543,12 +543,24 @@ static int __sprint_symbol(char *buffer, unsigned long address, len +=sprintf (buffer + len,"]" ); } -if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) && !modname) { +if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO )) {const char *li_file;unsigned int li_line; +bool found = false; + +if (!modname) + found =kallsyms_lookup_lineinfo (address, + &li_file, &li_line); +else if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) { +struct module *mod =__module_address (address); + +if (mod) + found =module_lookup_lineinfo (mod, address, + &li_file, + &li_line); + } -if (kallsyms_lookup_lineinfo (address, - &li_file, &li_line)) +if (found) len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len," (%s:%u)" , li_file, li_line); }
kernel/module/kallsyms.c +91 -0
94 lines not on this page · raw message
kernel/module/main.c +3 -0
@@ -2648,6 +2648,9 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->btf_base_data =any_section_objs (info,".BTF.base" ,1 , &mod->btf_base_data_size);#endif +if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) + mod->lineinfo_data =any_section_objs (info,".mod_lineinfo" ,1 , + &mod->lineinfo_data_size);#ifdef CONFIG_JUMP_LABEL mod->jump_entries = section_objs(info, "__jump_table", sizeof(*mod->jump_entries),
scripts/Makefile +1 -0
@@ -5,6 +5,7 @@ hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO_MODULES) += gen_lineinfo hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
scripts/Makefile.modfinal +6 -0
@@ -46,6 +46,9 @@ quiet_cmd_btf_ko = BTF [M] $@ $(CONFIG_SHELL) $(srctree)/scripts/gen-btf.sh --btf_base $(objtree)/vmlinux $@; \ fi; +quiet_cmd_lineinfo_ko = LINEINFO [M] $@ + cmd_lineinfo_ko = $(CONFIG_SHELL) $(srctree)/scripts/gen-mod-lineinfo.sh $@ + # Same as newer-prereqs, but allows to exclude specified extra dependencies newer_prereqs_except = $(filter-out $(PHONY) $(1),$?)
@@ -59,6 +62,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) +endif +ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + +$(if $(newer-prereqs),$(call cmd,lineinfo_ko)) endif +$(call cmd,check_tracepoint)
scripts/gen-mod-lineinfo.sh new file +48 -0
48 lines not on this page · raw message
scripts/gen_lineinfo.c +320 -29
415 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 3/4 kallsyms: delta-compress lineinfo tables for ~2.7x size reduction
In reply to Sasha LevinReplace the flat uncompressed parallel arrays (lineinfo_addrs[], lineinfo_file_ids[], lineinfo_lines[]) with a block-indexed, delta-encoded, ULEB128 varint compressed format.
The sorted address array has small deltas between consecutive entries (typically 1-50 bytes), file IDs have high locality (delta often 0, same file), and line numbers change slowly. Delta-encoding followed by ULEB128 varint compression shrinks most values from 4 bytes to 1.
Entries are grouped into blocks of 64. A small uncompressed block index (first addr + byte offset per block) enables O(log(N/64)) binary search, followed by sequential decode of at most 64 varints within the matching block. All decode state lives on the stack -- zero allocations, still safe for NMI/panic context.
Measured on a defconfig+debug x86_64 build (3,017,154 entries, 4,822 source files, 47,144 blocks):
Before (flat arrays):
lineinfo_addrs[] 12,068,616 bytes (u32 x 3.0M)
lineinfo_file_ids[] 6,034,308 bytes (u16 x 3.0M)
lineinfo_lines[] 12,068,616 bytes (u32 x 3.0M)
Total: 30,171,540 bytes (28.8 MiB, 10.0 bytes/entry)After (block-indexed delta + ULEB128):
lineinfo_block_addrs[] 188,576 bytes (184 KiB)
lineinfo_block_offsets[] 188,576 bytes (184 KiB)
lineinfo_data[] 10,926,128 bytes (10.4 MiB)
Total: 11,303,280 bytes (10.8 MiB, 3.7 bytes/entry)Savings: 18.0 MiB (2.7x reduction)
Booted in QEMU and verified with SysRq-l that annotations still work:
default_idle+0x9/0x10 (arch/x86/kernel/process.c:767) default_idle_call+0x6c/0xb0 (kernel/sched/idle.c:122) do_idle+0x335/0x490 (kernel/sched/idle.c:191) cpu_startup_entry+0x4e/0x60 (kernel/sched/idle.c:429) rest_init+0x1aa/0x1b0 (init/main.c:760)
--- .../admin-guide/kallsyms-lineinfo.rst | 7 +- include/linux/mod_lineinfo.h | 227 ++++++++++++++++-- init/Kconfig | 8 +- kernel/kallsyms.c | 46 ++-- kernel/kallsyms_internal.h | 8 +- kernel/module/kallsyms.c | 77 +++--- scripts/empty_lineinfo.S | 20 +- scripts/gen_lineinfo.c | 189 +++++++++------ scripts/kallsyms.c | 7 +- 9 files changed, 400 insertions(+), 189 deletions(-)
Documentation/admin-guide/kallsyms-lineinfo.rst +4 -3
@@ -76,10 +76,11 @@ Memory Overhead =============== The vmlinux lineinfo tables are stored in ``.rodata`` and typically add -approximately 44 MiB to the kernel image for a standard configuration -(~4.6 million DWARF line entries, ~10 bytes per entry after deduplication). +approximately 10-15 MiB to the kernel image for a standard configuration +(~4.6 million DWARF line entries, ~2-3 bytes per entry after delta +compression). -Per-module lineinfo adds approximately 10 bytes per DWARF line entry to each +Per-module lineinfo adds approximately 2-3 bytes per DWARF line entry to each ``.ko`` file. Known Limitations
include/linux/mod_lineinfo.h +201 -26
258 lines not on this page · raw message
init/Kconfig +5 -3
@@ -2065,8 +2065,9 @@ config KALLSYMS_LINEINFO anon_vma_clone+0x2ed/0xcf0 (mm/rmap.c:412) This requires elfutils (libdw-dev/elfutils-devel) on the build host. - Adds approximately 44MB to a typical kernel image (10 bytes per - DWARF line-table entry, ~4.6M entries for a typical config). + Adds approximately 10-15MB to a typical kernel image (~2-3 bytes + per entry after delta compression, ~4.6M entries for a typical + config). If unsure, say N.
@@ -2079,7 +2080,8 @@ config KALLSYMS_LINEINFO_MODULES so stack traces from module code include (file.c:123) annotations. Requires elfutils (libdw-dev/elfutils-devel) on the build host. - Increases .ko sizes by approximately 10 bytes per DWARF line entry. + Increases .ko sizes by approximately 2-3 bytes per DWARF line + entry after delta compression. If unsure, say N.
kernel/kallsyms.c +17 -29
64 lines not on this page · raw message
kernel/kallsyms_internal.h +5 -3
@@ -16,10 +16,12 @@ extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[];extern const u32 lineinfo_num_entries; -extern const u32 lineinfo_addrs[]; -extern const u16 lineinfo_file_ids[]; -extern const u32 lineinfo_lines[];extern const u32 lineinfo_num_files; +extern const u32 lineinfo_num_blocks; +extern const u32 lineinfo_block_addrs[]; +extern const u32 lineinfo_block_offsets[]; +extern const u32 lineinfo_data_size; +extern const u8 lineinfo_data[];extern const u32 lineinfo_file_offsets[];extern const u32 lineinfo_filenames_size;extern const char lineinfo_filenames[];
kernel/module/kallsyms.c +31 -46
105 lines not on this page · raw message
scripts/empty_lineinfo.S +14 -6
@@ -14,12 +14,20 @@ lineinfo_num_entries: .balign 4 lineinfo_num_files: .long 0 - .globl lineinfo_addrs -lineinfo_addrs: - .globl lineinfo_file_ids -lineinfo_file_ids: - .globl lineinfo_lines -lineinfo_lines: + .globl lineinfo_num_blocks + .balign 4 +lineinfo_num_blocks: + .long 0 + .globl lineinfo_block_addrs +lineinfo_block_addrs: + .globl lineinfo_block_offsets +lineinfo_block_offsets: + .globl lineinfo_data_size + .balign 4 +lineinfo_data_size: + .long 0 + .globl lineinfo_data +lineinfo_data: .globl lineinfo_file_offsets lineinfo_file_offsets: .globl lineinfo_filenames_size
scripts/gen_lineinfo.c +119 -70
254 lines not on this page · raw message
scripts/kallsyms.c +4 -3
@@ -80,11 +80,12 @@ static bool is_ignored_symbol(const char *name, char type) {/* Ignore lineinfo symbols for kallsyms pass stability */ static const char *const lineinfo_syms[] = { -"lineinfo_addrs" , -"lineinfo_file_ids" , +"lineinfo_block_addrs" , +"lineinfo_block_offsets" , +"lineinfo_data" ,"lineinfo_file_offsets" ,"lineinfo_filenames" , -"lineinfo_lines" , +"lineinfo_num_blocks" ,"lineinfo_num_entries" ,"lineinfo_num_files" , };
Signature · 1 line
2.51.0
PATCH v3 4/4 kallsyms: add KUnit tests for lineinfo feature
In reply to Sasha LevinAdd a KUnit test module (CONFIG_LINEINFO_KUNIT_TEST) that verifies the kallsyms lineinfo feature produces correct source file:line annotations in stack traces.
Export sprint_backtrace() and sprint_backtrace_build_id() as GPL symbols so the test module can exercise the backtrace APIs.
--- MAINTAINERS | 1 + kernel/kallsyms.c | 2 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 813 +++++++++++++++++++++++++++++++++++++ 5 files changed, 829 insertions(+) create mode 100644 lib/tests/lineinfo_kunit.c
MAINTAINERS +1 -0
@@ -13733,6 +13733,7 @@ M: Sasha Levin <[email protected]> S: Maintained F: Documentation/admin-guide/kallsyms-lineinfo.rst F: include/linux/mod_lineinfo.h +F: lib/tests/lineinfo_kunit.c F: scripts/gen-mod-lineinfo.sh F: scripts/gen_lineinfo.c
kernel/kallsyms.c +2 -0
@@ -625,6 +625,7 @@ int sprint_backtrace(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-1 ,1 ,0 ); } +EXPORT_SYMBOL_GPL (sprint_backtrace);/** * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
@@ -645,6 +646,7 @@ int sprint_backtrace_build_id(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-1 ,1 ,1 ); } +EXPORT_SYMBOL_GPL (sprint_backtrace_build_id);/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ struct kallsym_iter {
lib/Kconfig.debug +10 -0
@@ -3048,6 +3048,16 @@ config LONGEST_SYM_KUNIT_TEST If unsure, say N. +config LINEINFO_KUNIT_TEST + tristate "KUnit tests for kallsyms lineinfo" if !KUNIT_ALL_TESTS + depends on KUNIT && KALLSYMS_LINEINFO + default KUNIT_ALL_TESTS + help + KUnit tests for the kallsyms source line info feature. + Verifies that stack traces include correct (file.c:line) annotations. + + If unsure, say N. + config HW_BREAKPOINT_KUNIT_TEST bool "Test hw_breakpoint constraints accounting" if !KUNIT_ALL_TESTS depends on HAVE_HW_BREAKPOINT
lib/tests/Makefile +3 -0
@@ -36,6 +36,9 @@ obj-$(CONFIG_LIVEUPDATE_TEST) += liveupdate.o CFLAGS_longest_symbol_kunit.o += $(call cc-disable-warning, missing-prototypes) obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) += longest_symbol_kunit.o +CFLAGS_lineinfo_kunit.o += -fno-inline-functions-called-once +obj-$(CONFIG_LINEINFO_KUNIT_TEST) += lineinfo_kunit.o + obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
lib/tests/lineinfo_kunit.c new file +813 -0
813 lines not on this page · raw message
Signature · 1 line
2.51.0
Petr Pavlu
maintainerRe: [PATCH v3 2/4] kallsyms: extend lineinfo to loadable modules
In reply to Sasha LevinQuoted 91 lines · Sasha Levin
Add
CONFIG_KALLSYMS_LINEINFO_MODULES, which extends theCONFIG_KALLSYMS_LINEINFOfeature to loadable kernel modules.At build time, each .ko is post-processed by
scripts/gen-mod-lineinfo.sh(modeled ongen-btf.sh) which runsscripts/gen_lineinfo--module on the .ko, generates a .mod_lineinfo section containing a compact binary table of .text-relative offsets, file IDs, line numbers, and filenames, and embeds it back into the .ko via objcopy.At runtime,
module_lookup_lineinfo()performs a binary search on the module's .mod_lineinfo section, and__sprint_symbol()calls it for addresses that fall within a module. The lookup is NMI/panic-safe (no locks, no allocations) — the data lives in read-only module memory and is freed automatically when the module is unloaded.The
gen_lineinfotool gains --module mode which:
- Uses .text section address as base (ET_RELfiles have no_textsymbol)
- Filters entries to .text-only (excludes .init.text/.exit.text)
- Handles libdw'sET_RELpath-doubling quirk inmake_relative()
- Outputs a flat binary-format section instead of named global symbolsPer-module overhead is approximately 10 bytes per DWARF line entry.
Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>--- .../admin-guide/kallsyms-lineinfo.rst | 40 +- MAINTAINERS | 2 + include/linux/mod_lineinfo.h | 68 ++++ include/linux/module.h | 5 + init/Kconfig | 13 + kernel/kallsyms.c | 18 +- kernel/module/kallsyms.c | 91 +++++ kernel/module/main.c | 3 + scripts/Makefile | 1 + scripts/Makefile.modfinal | 6 + scripts/gen-mod-lineinfo.sh | 48 +++ scripts/gen_lineinfo.c | 349 ++++++++++++++++-- 12 files changed, 604 insertions(+), 40 deletions(-) create mode 100644 include/linux/mod_lineinfo.h create mode 100755 scripts/gen-mod-lineinfo.shDocumentation/admin-guide/kallsyms-lineinfo.rst +27
-7@@ -51,22 +51,46 @@ With ``CONFIG_KALLSYMS_LINEINFO``:: Note that assembly routines (such as ``entry_SYSCALL_64_after_hwframe``) are not annotated because they lack DWARF debug information. +Module Support +============== + +``CONFIG_KALLSYMS_LINEINFO_MODULES`` extends the feature to loadable kernel +modules. When enabled, each ``.ko`` is post-processed at build time to embed +a ``.mod_lineinfo`` section containing the same kind of address-to-source +mapping. + +Enable in addition to the base options:: + + CONFIG_MODULES=y + CONFIG_KALLSYMS_LINEINFO_MODULES=y + +Stack traces from module code will then include annotations:: + + my_driver_func+0x30/0x100 [my_driver] (drivers/foo/bar.c:123) + +The ``.mod_lineinfo`` section is loaded into read-only module memory alongside +the module text. No additional runtime memory allocation is required; the data +is freed when the module is unloaded. + Memory Overhead =============== -The lineinfo tables are stored in ``.rodata`` and typically add approximately -44 MiB to the kernel image for a standard configuration (~4.6 million DWARF -line entries, ~10 bytes per entry after deduplication). +The vmlinux lineinfo tables are stored in ``.rodata`` and typically add +approximately 44 MiB to the kernel image for a standard configuration +(~4.6 million DWARF line entries, ~10 bytes per entry after deduplication). + +Per-module lineinfo adds approximately 10 bytes per DWARF line entry to each +``.ko`` file. Known Limitations ================= -- **vmlinux only**: Only symbols in the core kernel image are annotated. - Module symbols are not covered. -- **4 GiB offset limit**: Address offsets from ``_text`` are stored as 32-bit - values. Entries beyond 4 GiB from ``_text`` are skipped at build time with
Documentation/admin-guide/kallsyms-lineinfo.rst With ``CONFIG_KALLSYMS_LINEINFO``:: · line 96
Quoted 10 lines
Documentation/admin-guide/kallsyms-lineinfo.rst +5
-1- a warning. +- **4 GiB offset limit**: Address offsets from ``_text`` (vmlinux) or + ``.text`` base (modules) are stored as 32-bit values. Entries beyond + 4 GiB are skipped at build time with a warning. - **65535 file limit**: Source file IDs are stored as 16-bit values. Builds with more than 65535 unique source files will fail with an error. - **No assembly annotations**: Functions implemented in assembly that lack DWARF ``.debug_line`` data are not annotated. +- **No init text**: For modules, functions in ``.init.text`` are not annotated + because that memory is freed after module initialization.
A second table .init.mod_lineinfo could be added to provide the necessary information for .init sections, which would be dropped along with all the other init code+data.
Quoted 21 lines
MAINTAINERS +2
-0@@ -13732,6 +13732,8 @@ KALLSYMS LINEINFO M: Sasha Levin <[email protected]> S: Maintained F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: include/linux/mod_lineinfo.h +F: scripts/gen-mod-lineinfo.sh F: scripts/gen_lineinfo.c KASANinclude/linux/mod_lineinfo.h new file +2
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/*
include/linux/mod_lineinfo.h line 12
Quoted 10 lines
include/linux/mod_lineinfo.h new file +10
-0+* mod_lineinfo.h - Binary format for per-module source line information +* +* This header defines the layout of the .mod_lineinfo section embedded +* in loadable kernel modules. It is dual-use: included from both the +* kernel and the userspace gen_lineinfo tool. +* +* Section layout (all values in target-native endianness): +* +* struct mod_lineinfo_header (16 bytes) +* u32 addrs[num_entries] -- offsets from .text base, sorted
Modules are relocatable objects. The typical way to express a reference from one section to data in another section is to use relocations. Choosing to use an implicit base and resolved offsets means that the code has trouble correctly referencing the .text section and can't express line information data for other sections, such as .exit.text.
include/linux/mod_lineinfo.h line 17
Quoted 5 lines
include/linux/mod_lineinfo.h new file +5
-0+* u16 file_ids[num_entries] -- parallel to addrs +* <2-byte pad if num_entries is odd> +* u32 lines[num_entries] -- parallel to addrs +* u32 file_offsets[num_files] -- byte offset into filenames[] +* char filenames[filenames_size] -- concatenated NUL-terminated strings
Nit: The description could be a bit easier to navigate if the mod_lineinfo_header struct was expanded, so it is clear where num_entries, num_files and filenames_size come from.
Quoted 6 lines
include/linux/mod_lineinfo.h new file +6
-0+*/ +#ifndef _LINUX_MOD_LINEINFO_H +#define _LINUX_MOD_LINEINFO_H + +#ifdef __KERNEL__ +#include <linux/types.h>
include/linux/mod_lineinfo.h line 33
Quoted 10 lines
include/linux/mod_lineinfo.h new file +10
-0+#else +#include <stdint.h> +typedef uint32_t u32 ; +typedef uint16_t u16 ; +#endif + +struct mod_lineinfo_header { +u32 num_entries; +u32 num_files; +u32 filenames_size;/* total bytes of concatenated filenames */
An alternative would be to say that the filenames data extends to the end of the section, without requiring an explicit filenames_size.
include/linux/mod_lineinfo.h line 34
Quoted 1 line
include/linux/mod_lineinfo.h new file +1
-0+u32 reserved;/* padding, must be 0 */
I believe the format should remain internal to the kernel, so there is no need for such a reserved member.
Quoted 22 lines
include/linux/mod_lineinfo.h new file +22
-0+}; + +/* Offset helpers: compute byte offset from start of section to each array */ + +static inline u32 mod_lineinfo_addrs_off (void ) +{ +return sizeof (struct mod_lineinfo_header ); +} + +static inline u32 mod_lineinfo_file_ids_off (u32 num_entries) +{ +return mod_lineinfo_addrs_off () + num_entries *sizeof (u32); +} + +static inline u32 mod_lineinfo_lines_off (u32 num_entries) +{ +/* u16 file_ids[] may need 2-byte padding to align lines[] to 4 bytes */ +u32 off =mod_lineinfo_file_ids_off (num_entries) + + num_entries *sizeof (u16); +return (off +3 ) & ~3u ; +} +
include/linux/mod_lineinfo.h line 66
Quoted 10 lines
include/linux/mod_lineinfo.h new file +10
-0+static inline u32 mod_lineinfo_file_offsets_off (u32 num_entries) +{ +return mod_lineinfo_lines_off (num_entries) + num_entries *sizeof (u32); +} + +static inline u32 mod_lineinfo_filenames_off (u32 num_entries,u32 num_files) +{ +return mod_lineinfo_file_offsets_off (num_entries) + + num_files *sizeof (u32); +}
These helpers are used only from kernel/module/kallsyms.c. I assume they are present in this header file to stay close to the description of the format.
I personally find them quite verbose. The module_lookup_lineinfo() function needs an intimate knowledge of the data format anyway. The code in module_lookup_lineinfo() could be replaced with just:
addrs = base + sizeof(struct mod_lineinfo_header); file_ids = addrs + num_entries * sizeof(u32); lines = (file_ids + num_entries * sizeof(u16) + 3) & ~3u; file_offsets = lines + num_entries * sizeof(u32); filenames = file_offsets + num_files * sizeof(u32);
include/linux/module.h struct module { · line 512
Quoted 12 lines
include/linux/mod_lineinfo.h new file +2
-0+ +#endif /* _LINUX_MOD_LINEINFO_H */ include/linux/module.h +2
-0@@ -508,6 +508,8 @@ struct module {void *btf_data;void *btf_base_data;#endif +void *lineinfo_data;/* .mod_lineinfo section in MOD_RODATA */ +unsigned int lineinfo_data_size;
The lineinfo-specific members should be enclosed within the #ifdef CONFIG_KALLSYMS_LINEINFO_MODULES.
This will require module_lookup_lineinfo() to be conditionally compiled based on CONFIG_KALLSYMS_LINEINFO_MODULES, with a dummy version provided otherwise. Alternatively, accessors to module::lineinfo_data and module::lineinfo_data_size that handle CONFIG_KALLSYMS_LINEINFO_MODULES could be introduced in include/linux/module.h. For example, see module_buildid() or is_livepatch_module.
Quoted 124 lines
include/linux/module.h +3
-0#ifdef CONFIG_JUMP_LABEL struct jump_entry *jump_entries;unsigned int num_jump_entries;@@ -1021,6 +1023,9 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,#endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ +bool module_lookup_lineinfo (struct module *mod,unsigned long addr, +const char **file,unsigned int *line); +/* Define __free(module_put) macro for struct module *. */ DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))init/Kconfig +13
-0@@ -2070,6 +2070,19 @@ config KALLSYMS_LINEINFO If unsure, say N. +config KALLSYMS_LINEINFO_MODULES + bool "Embed source file:line information in module stack traces" + depends on KALLSYMS_LINEINFO && MODULES + help + Extends KALLSYMS_LINEINFO to loadable kernel modules. Each .ko + gets a lineinfo table generated from its DWARF data at build time, + so stack traces from module code include (file.c:123) annotations. + + Requires elfutils (libdw-dev/elfutils-devel) on the build host. + Increases .ko sizes by approximately 10 bytes per DWARF line entry. + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKSkernel/kallsyms.c +15
-3@@ -543,12 +543,24 @@ static int __sprint_symbol(char *buffer, unsigned long address, len +=sprintf (buffer + len,"]" ); } -if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) && !modname) { +if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO )) {const char *li_file;unsigned int li_line; +bool found = false; + +if (!modname) + found =kallsyms_lookup_lineinfo (address, + &li_file, &li_line); +else if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) { +struct module *mod =__module_address (address); + +if (mod) + found =module_lookup_lineinfo (mod, address, + &li_file, + &li_line); + } -if (kallsyms_lookup_lineinfo (address, - &li_file, &li_line)) +if (found) len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len," (%s:%u)" , li_file, li_line); }kernel/module/kallsyms.c +47
-0@@ -494,3 +494,94 @@ int module_kallsyms_on_each_symbol(const char *modname,mutex_unlock (&module_mutex);return ret; } + +#include <linux/mod_lineinfo.h> + +/* +* Look up source file:line for an address within a loaded module. +* Uses the .mod_lineinfo section embedded in the .ko at build time. +* +* Safe in NMI/panic context: no locks, no allocations. +* Caller must hold RCU read lock (or be in a context where the module +* cannot be unloaded). +*/ +bool module_lookup_lineinfo (struct module *mod,unsigned long addr, +const char **file,unsigned int *line) +{ +const struct mod_lineinfo_header *hdr; +const void *base; +const u32 *addrs, *lines, *file_offsets; +const u16 *file_ids; +const char *filenames; +u32 num_entries, num_files, filenames_size; +unsigned long text_base; +unsigned int offset; +unsigned long long raw_offset; +unsigned int low, high, mid; +u16 file_id; + +if (!IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) +return false; + + base = mod->lineinfo_data; +if (!base) +return false; + +if (mod->lineinfo_data_size <sizeof (*hdr)) +return false; + + hdr = base; + num_entries = hdr->num_entries; + num_files = hdr->num_files; + filenames_size = hdr->filenames_size; + +if (num_entries ==0 ) +return false; + +/* Validate section is large enough for all arrays */ +if (mod->lineinfo_data_size < +mod_lineinfo_filenames_off (num_entries, num_files) + filenames_size)
kernel/module/kallsyms.c int module_kallsyms_on_each_symbol(const char *modname, · line 553
Quoted 10 lines
kernel/module/kallsyms.c +10
-0+return false; + + addrs = base +mod_lineinfo_addrs_off (); + file_ids = base +mod_lineinfo_file_ids_off (num_entries); + lines = base +mod_lineinfo_lines_off (num_entries); + file_offsets = base +mod_lineinfo_file_offsets_off (num_entries); + filenames = base +mod_lineinfo_filenames_off (num_entries, num_files); + +/* Compute offset from module .text base */ + text_base = (unsigned long )mod->mem[MOD_TEXT ].base;
The module::mem[] covers module memory regions. One can think of them as ELF segments, except they are created dynamically by the module loader. The code conflates the .text section and the TEXT segment. I'm not aware of any guarantee that the .text section will be always placed as the first section in this segment.
Relocations can be used to accurately reference the .text section.
kernel/module/kallsyms.c int module_kallsyms_on_each_symbol(const char *modname, · line 558
Quoted 5 lines
kernel/module/kallsyms.c +5
-0+if (addr < text_base) +return false; + + raw_offset = addr - text_base; +if (raw_offset >UINT_MAX )
The offsets in the addrs array are of the u32 type, so this should be strictly speaking checked against U32_MAX.
Quoted 48 lines
kernel/module/kallsyms.c +29
-0+return false; + offset = (unsigned int )raw_offset; + +/* Binary search for largest entry <= offset */ + low =0 ; + high = num_entries; +while (low < high) { + mid = low + (high - low) /2 ; +if (addrs[mid] <= offset) + low = mid +1 ; +else + high = mid; + } + +if (low ==0 ) +return false; + low--; + + file_id = file_ids[low]; +if (file_id >= num_files) +return false; + +if (file_offsets[file_id] >= filenames_size) +return false; + + *file = &filenames[file_offsets[file_id]]; + *line = lines[low]; +return true; +}kernel/module/main.c +3
-0@@ -2648,6 +2648,9 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->btf_base_data =any_section_objs (info,".BTF.base" ,1 , &mod->btf_base_data_size);#endif +if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) + mod->lineinfo_data =any_section_objs (info,".mod_lineinfo" ,1 , + &mod->lineinfo_data_size);#ifdef CONFIG_JUMP_LABEL mod->jump_entries = section_objs(info, "__jump_table", sizeof(*mod->jump_entries),scripts/Makefile +0
-0
scripts/Makefile line 8
Quoted 4 lines
scripts/Makefile +1
-0@@ -5,6 +5,7 @@ hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO_MODULES) += gen_lineinfo
This line is unnecessary because CONFIG_KALLSYMS_LINEINFO_MODULES depends on CONFIG_KALLSYMS_LINEINFO.
Quoted 18 lines
scripts/Makefile +0
-0hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compilerscripts/Makefile.modfinal +3
-0@@ -46,6 +46,9 @@ quiet_cmd_btf_ko = BTF [M] $@ $(CONFIG_SHELL) $(srctree)/scripts/gen-btf.sh --btf_base $(objtree)/vmlinux $@; \ fi; +quiet_cmd_lineinfo_ko = LINEINFO [M] $@ + cmd_lineinfo_ko = $(CONFIG_SHELL) $(srctree)/scripts/gen-mod-lineinfo.sh $@ + # Same as newer-prereqs, but allows to exclude specified extra dependencies newer_prereqs_except = $(filter-out $(PHONY) $(1),$?)
scripts/Makefile.modfinal if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · line 67
Quoted 6 lines
scripts/Makefile.modfinal +3
-0@@ -59,6 +62,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) +endif +ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + +$(if $(newer-prereqs),$(call cmd,lineinfo_ko))
Should this be 'if_changed_except.. vmlinux'?
Quoted 126 lines
scripts/Makefile.modfinal +0
-0endif +$(call cmd,check_tracepoint)scripts/gen-mod-lineinfo.sh new file +48
-0@@ -0,0 +1,48 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# gen-mod-lineinfo.sh - Embed source line info into a kernel module (.ko) +# +# Reads DWARF from the .ko, generates a .mod_lineinfo section, and +# embeds it back into the .ko. Modeled on scripts/gen-btf.sh. + +set -e + +if [ $# -ne 1 ]; then + echo "Usage: $0 <module.ko>" >&2 + exit 1 +fi + +KO="$1" + +cleanup() { + rm -f "${KO}.lineinfo.S" "${KO}.lineinfo.o" "${KO}.lineinfo.bin" +} +trap cleanup EXIT + +case "${KBUILD_VERBOSE}" in +*1*) + set -x + ;; +esac + +# Generate assembly from DWARF -- if it fails (no DWARF), silently skip +if ! ${objtree}/scripts/gen_lineinfo --module "${KO}" > "${KO}.lineinfo.S"; then + exit 0 +fi + +# Compile assembly to object file +${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \ + ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_MODULE} \ + -c -o "${KO}.lineinfo.o" "${KO}.lineinfo.S" + +# Extract raw section content +${OBJCOPY} -O binary --only-section=.mod_lineinfo \ + "${KO}.lineinfo.o" "${KO}.lineinfo.bin" + +# Embed into the .ko with alloc,readonly flags +${OBJCOPY} --add-section ".mod_lineinfo=${KO}.lineinfo.bin" \ + --set-section-flags .mod_lineinfo=alloc,readonly \ + "${KO}" + +exit 0scripts/gen_lineinfo.c +27
-21@@ -23,8 +23,16 @@#include <gelf.h> #include <limits.h> +#include "../include/linux/mod_lineinfo.h" + +static int module_mode; +static unsigned int skipped_overflow; +/* .text range for module mode (keep only runtime code) */ +static unsigned long long text_section_start; +static unsigned long long text_section_end; +struct line_entry {unsigned int offset;/* offset from _text */ unsigned int file_id;@@ -148,27 +156,25 @@ static const char *make_relative(const char *path, const char *comp_dir) {const char *p; -/* If already relative, use as-is */ -if (path[0 ] !='/' ) -return path; - -/* comp_dir from DWARF is the most reliable method */ -if (comp_dir) { -size_t len =strlen (comp_dir); - -if (!strncmp (path, comp_dir, len) && path[len] =='/' ) { -const char *rel = path + len +1 ; - -/* -* If comp_dir pointed to a subdirectory -* (e.g. arch/parisc/kernel) rather than -* the tree root, stripping it leaves a -* bare filename. Fall through to the -* kernel_dirs scan so we recover the full -* relative path instead. -*/ -if (strchr (rel,'/' )) -return rel; +if (path[0 ] =='/' ) { +/* Try comp_dir prefix from DWARF */ +if (comp_dir) { +size_t len =strlen (comp_dir); + +if (!strncmp (path, comp_dir, len) && path[len] =='/' ) { +const char *rel = path + len +1 ; + +/* +* If comp_dir pointed to a subdirectory +* (e.g. arch/parisc/kernel) rather than +* the tree root, stripping it leaves a +* bare filename. Fall through to the +* kernel_dirs scan so we recover the full +* relative path instead. +*/ +if (strchr (rel,'/' )) +return rel; + } }/*
scripts/gen_lineinfo.c static const char *make_relative(const char *path, const char *comp_dir) · line 206
Quoted 10 lines
scripts/gen_lineinfo.c +4
-3@@ -194,9 +200,45 @@ static const char *make_relative(const char *path, const char *comp_dir)return p ? p +1 : path; } -/* Fall back to basename */ - p =strrchr (path,'/' ); -return p ? p +1 : path; +/* +* Relative path — check for duplicated-path quirk from libdw +* on ET_REL files (e.g., "a/b.c/a/b.c" → "a/b.c"). +*/
When does this quirk occur? Is it a bug in libdw?
scripts/gen_lineinfo.c static const char *make_relative(const char *path, const char *comp_dir) · line 216
Quoted 10 lines
scripts/gen_lineinfo.c +10
-0+ { +size_t len =strlen (path); + +for (p = path; (p =strchr (p,'/' )) !=NULL ; p++) { +size_t prefix = p - path; +size_t rest = len - prefix -1 ; + +if (rest == prefix && !memcmp (path, p +1 , prefix)) +return p +1 ; + }
Isn't this loop same as:
size_t mid = len / 2;
if (path[mid] == '/' && !memcmp(path, path + mid + 1, mid - 1))
return path + mid + 1;
Quoted 84 lines
scripts/gen_lineinfo.c +77
-0+ } + +/* +* Bare filename with no directory component — try to recover the +* relative path using comp_dir. Some toolchains/elfutils combos +* produce bare filenames where comp_dir holds the source directory. +* Construct the absolute path and run the kernel_dirs scan. +*/ +if (!strchr (path,'/' ) && comp_dir && comp_dir[0 ] =='/' ) { +static char buf[PATH_MAX ]; + +snprintf (buf,sizeof (buf),"%s/%s" , comp_dir, path); +for (p = buf +1 ; *p; p++) { +if (*(p -1 ) =='/' ) { +for (unsigned int i =0 ; i <sizeof (kernel_dirs) / +sizeof (kernel_dirs[0 ]); i++) { +if (!strncmp (p, kernel_dirs[i], +strlen (kernel_dirs[i]))) +return p; + } + } + } + } + +return path; }static int compare_entries (const void *a,const void *b)@@ -248,6 +290,159 @@ static unsigned long long find_text_addr(Elf *elf)exit (1 ); } +static void find_text_section_range (Elf *elf) +{ +Elf_Scn *scn =NULL ; +GElf_Shdr shdr; +size_t shstrndx; + +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return ; + +while ((scn =elf_nextscn (elf, scn)) !=NULL ) { +const char *name; + +if (!gelf_getshdr (scn, &shdr)) +continue ; + name =elf_strptr (elf, shstrndx, shdr.sh_name); +if (name && !strcmp (name,".text" )) { + text_section_start = shdr.sh_addr; + text_section_end = shdr.sh_addr + shdr.sh_size; +return ; + } + } +} + +/* +* Apply .rela.debug_line relocations to a mutable copy of .debug_line data. +* +* elfutils libdw (through at least 0.194) does NOT apply relocations for +* ET_REL files when using dwarf_begin_elf(). The internal libdwfl layer +* does this via __libdwfl_relocate(), but that API is not public. +* +* For DWARF5, the .debug_line file name table uses DW_FORM_line_strp +* references into .debug_line_str. Without relocation, all these offsets +* resolve to 0 (or garbage), causing dwarf_linesrc()/dwarf_filesrc() to +* return wrong filenames (typically the comp_dir for every file). +* +* This function applies the relocations manually so that the patched +* .debug_line data can be fed to dwarf_begin_elf() and produce correct +* results. +* +* See elfutils bug https://sourceware.org/bugzilla/show_bug.cgi?id=31447 +* A fix (dwelf_elf_apply_relocs) was proposed but not yet merged as of +* elfutils 0.194: https://sourceware.org/pipermail/elfutils-devel/2024q3/007388.html +*/ +/* +* Determine the relocation type for a 32-bit absolute reference +* on the given architecture. Returns 0 if unknown. +*/ +static unsigned int r_type_abs32 (unsigned int e_machine) +{ +switch (e_machine) { +case EM_X86_64 :return 10 ;/* R_X86_64_32 */ +case EM_386 :return 1 ;/* R_386_32 */
scripts/gen_lineinfo.c static unsigned long long find_text_addr(Elf *elf) · line 354
Quoted 10 lines
scripts/gen_lineinfo.c +10
-0+case EM_AARCH64 :return 258 ;/* R_AARCH64_ABS32 */ +case EM_ARM :return 2 ;/* R_ARM_ABS32 */ +case EM_RISCV :return 1 ;/* R_RISCV_32 */ +case EM_S390 :return 4 ;/* R_390_32 */ +case EM_MIPS :return 2 ;/* R_MIPS_32 */ +case EM_PPC64 :return 1 ;/* R_PPC64_ADDR32 */ +case EM_PPC :return 1 ;/* R_PPC_ADDR32 */ +case EM_LOONGARCH :return 1 ;/* R_LARCH_32 */ +case EM_PARISC :return 1 ;/* R_PARISC_DIR32 */ +default :return 0 ;
The source file already includes elf.h from elfutils. Is it necessary to hardcode these relocation values here?
Quoted 95 lines
scripts/gen_lineinfo.c +91
-0+ } +} + +static void apply_debug_line_relocations (Elf *elf) +{ +Elf_Scn *scn =NULL ; +Elf_Scn *debug_line_scn =NULL ; +Elf_Scn *rela_debug_line_scn =NULL ; +Elf_Scn *symtab_scn =NULL ; +GElf_Shdr shdr; +GElf_Ehdr ehdr; +unsigned int abs32_type; +size_t shstrndx; +Elf_Data *dl_data, *rela_data, *sym_data; +GElf_Shdr rela_shdr, sym_shdr; +size_t nrels, i; + +if (gelf_getehdr (elf, &ehdr) ==NULL ) +return ; + + abs32_type =r_type_abs32 (ehdr.e_machine); +if (!abs32_type) +return ; + +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return ; + +/* Find the relevant sections */ +while ((scn =elf_nextscn (elf, scn)) !=NULL ) { +const char *name; + +if (!gelf_getshdr (scn, &shdr)) +continue ; + name =elf_strptr (elf, shstrndx, shdr.sh_name); +if (!name) +continue ; + +if (!strcmp (name,".debug_line" )) + debug_line_scn = scn; +else if (!strcmp (name,".rela.debug_line" )) + rela_debug_line_scn = scn; +else if (shdr.sh_type ==SHT_SYMTAB ) + symtab_scn = scn; + } + +if (!debug_line_scn || !rela_debug_line_scn || !symtab_scn) +return ; + + dl_data =elf_getdata (debug_line_scn,NULL ); + rela_data =elf_getdata (rela_debug_line_scn,NULL ); + sym_data =elf_getdata (symtab_scn,NULL ); +if (!dl_data || !rela_data || !sym_data) +return ; + +if (!gelf_getshdr (rela_debug_line_scn, &rela_shdr)) +return ; +if (!gelf_getshdr (symtab_scn, &sym_shdr)) +return ; + + nrels = rela_shdr.sh_size / rela_shdr.sh_entsize; + +for (i =0 ; i < nrels; i++) { +GElf_Rela rela; +GElf_Sym sym; +unsigned int r_type; +size_t r_sym; +uint32_t value; + +if (!gelf_getrela (rela_data, i, &rela)) +continue ; + + r_type =GELF_R_TYPE (rela.r_info); + r_sym =GELF_R_SYM (rela.r_info); + +/* Only handle the 32-bit absolute reloc for this arch */ +if (r_type != abs32_type) +continue ; + +if (!gelf_getsym (sym_data, r_sym, &sym)) +continue ; + +/* Relocated value = sym.st_value + addend */ + value = (uint32_t )(sym.st_value + rela.r_addend); + +/* Patch the .debug_line data at the relocation offset */ +if (rela.r_offset +4 <= dl_data->d_size) +memcpy ((char *)dl_data->d_buf + rela.r_offset, + &value,sizeof (value)); + } +} +static void process_dwarf (Dwarf *dwarf,unsigned long long text_addr) {Dwarf_Off off =0 , next_off;
scripts/gen_lineinfo.c static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) · line 498
Quoted 9 lines
scripts/gen_lineinfo.c +6
-0@@ -295,6 +490,16 @@ static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr)if (addr < text_addr)continue ; +/* +* In module mode, keep only .text addresses. +* In ET_REL .ko files, .init.text/.exit.text may +* overlap with .text address ranges, so we must +* explicitly check against the .text bounds. +*/
Nit: The use of "may" in this comment seems strange. It is fully expected that .text, .init.text, .exit.text or any other section will have their sh_addr set to 0 in relocatable objects and therefore the sections have overlapping address ranges.
scripts/gen_lineinfo.c static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) · line 502
Quoted 4 lines
scripts/gen_lineinfo.c +4
-0+if (module_mode && text_section_end > text_section_start && + (addr < text_section_start || addr >= text_section_end)) +continue ; +
The current code is very specific to the .text section. It would be good to cover all sections in the future. I think it will naturally require use of relocations to refer to individual sections.
Quoted 138 lines
scripts/gen_lineinfo.c +91
-5{unsigned long long raw_offset = addr - text_addr;@@ -440,6 +645,63 @@ static void output_assembly(void)printf ("\n" ); } +static void output_module_assembly (void ) +{ +unsigned int filenames_size =0 ; + +for (unsigned int i =0 ; i < num_files; i++) + filenames_size +=strlen (files[i].name) +1 ; + +printf ("/* SPDX-License-Identifier: GPL-2.0 */\n" ); +printf ("/*\n" ); +printf (" * Automatically generated by scripts/gen_lineinfo --module\n" ); +printf (" * Do not edit.\n" ); +printf (" */\n\n" ); + +printf ("\t.section .mod_lineinfo, \"a\"\n\n" ); + +/* Header: num_entries, num_files, filenames_size, reserved */ +printf ("\t.balign 4\n" ); +printf ("\t.long %u\n" , num_entries); +printf ("\t.long %u\n" , num_files); +printf ("\t.long %u\n" , filenames_size); +printf ("\t.long 0\n\n" ); + +/* addrs[] */ +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.long 0x%x\n" , entries[i].offset); +if (num_entries) +printf ("\n" ); + +/* file_ids[] */ +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.short %u\n" , entries[i].file_id); + +/* Padding to align lines[] to 4 bytes */ +if (num_entries &1 ) +printf ("\t.short 0\n" ); +if (num_entries) +printf ("\n" ); + +/* lines[] */ +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.long %u\n" , entries[i].line); +if (num_entries) +printf ("\n" ); + +/* file_offsets[] */ +for (unsigned int i =0 ; i < num_files; i++) +printf ("\t.long %u\n" , files[i].str_offset); +if (num_files) +printf ("\n" ); + +/* filenames[] */ +for (unsigned int i =0 ; i < num_files; i++) +print_escaped_asciz (files[i].name); +if (num_files) +printf ("\n" ); +} +int main (int argc,char *argv[]) {int fd;@@ -447,12 +709,23 @@ int main(int argc, char *argv[])Dwarf *dwarf;unsigned long long text_addr; +if (argc >=2 && !strcmp (argv[1 ],"--module" )) { + module_mode =1 ; + argv++; + argc--; + } +if (argc !=2 ) { -fprintf (stderr,"Usage: %s <vmlinux>\n" , argv[0 ]); +fprintf (stderr,"Usage: %s [--module] <ELF file>\n" , argv[0 ]);return 1 ; } - fd =open (argv[1 ],O_RDONLY ); +/* +* For module mode, open O_RDWR so we can apply debug section +* relocations to the in-memory ELF data. The modifications +* are NOT written back to disk (no elf_update() call). +*/ + fd =open (argv[1 ], module_mode ?O_RDWR :O_RDONLY );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , argv[1 ],strerror (errno));@@ -460,7 +733,7 @@ int main(int argc, char *argv[]) }elf_version (EV_CURRENT ); - elf =elf_begin (fd,ELF_C_READ ,NULL ); + elf =elf_begin (fd, module_mode ?ELF_C_RDWR :ELF_C_READ ,NULL );if (!elf) {fprintf (stderr,"elf_begin failed: %s\n" ,elf_errmsg (elf_errno ()));@@ -468,7 +741,22 @@ int main(int argc, char *argv[])return 1 ; } - text_addr =find_text_addr (elf); +if (module_mode) { +/* +* .ko files are ET_REL after ld -r. libdw does NOT apply +* relocations for ET_REL files, so DW_FORM_line_strp +* references in .debug_line are not resolved. Apply them +* ourselves so that dwarf_linesrc() returns correct paths. +* +* DWARF addresses include the .text sh_addr. Use .text +* sh_addr as the base so offsets are .text-relative. +*/ +apply_debug_line_relocations (elf); +find_text_section_range (elf); + text_addr = text_section_start; + }else { + text_addr =find_text_addr (elf); + } dwarf =dwarf_begin_elf (elf,DWARF_C_READ ,NULL );if (!dwarf) {@@ -494,7 +782,10 @@ int main(int argc, char *argv[])fprintf (stderr,"lineinfo: %u entries, %u files\n" , num_entries, num_files); -output_assembly (); +if (module_mode) +output_module_assembly (); +else +output_assembly ();dwarf_end (dwarf);elf_end (elf);
Signature · 2 lines
Thanks, Petr
Re: [PATCH v3 2/4] kallsyms: extend lineinfo to loadable modules
Thanks for the great review!
Quoted 7 lines · Petr Pavlu
Quoted 2 lines · Sasha Levin
+- **No init text**: For modules, functions in ``.init.text`` are not annotated + because that memory is freed after module initialization.A second table .init.mod_lineinfo could be added to provide the necessary information for .init sections, which would be dropped along with all the other init code+data.
Sure, it's something we can look at it later. For this series I'd like to keep scope to runtime .text, since init code runs briefly and rarely appears in stack traces that need debugging. Adding a second section would require module loader changes for loading and freeing it alongside init memory.
Quoted 13 lines
Quoted 13 lines
include/linux/mod_lineinfo.h new file +10
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* mod_lineinfo.h - Binary format for per-module source line information +* +* This header defines the layout of the .mod_lineinfo section embedded +* in loadable kernel modules. It is dual-use: included from both the +* kernel and the userspace gen_lineinfo tool. +* +* Section layout (all values in target-native endianness): +*
Quoted 8 lines
include/linux/mod_lineinfo.h line 12
Quoted 2 lines
include/linux/mod_lineinfo.h new file +2
-0+* struct mod_lineinfo_header (16 bytes) +* u32 addrs[num_entries] -- offsets from .text base, sorted Modules are relocatable objects. The typical way to express a reference from one section to data in another section is to use relocations. Choosing to use an implicit base and resolved offsets means that the code has trouble correctly referencing the .text section and can't express line information data for other sections, such as .exit.text.
I agree, which is why I scoped this just to .text :)
My thinking was that using ELF relocations would add significant complexity to both the build tool and the runtime lookup path, which must remain NMI-safe and allocation-free.
Quoted 9 lines
include/linux/mod_lineinfo.h line 17
Quoted 5 lines
include/linux/mod_lineinfo.h new file +5
-0+* u16 file_ids[num_entries] -- parallel to addrs +* <2-byte pad if num_entries is odd> +* u32 lines[num_entries] -- parallel to addrs +* u32 file_offsets[num_files] -- byte offset into filenames[] +* char filenames[filenames_size] -- concatenated NUL-terminated strings Nit: The description could be a bit easier to navigate if the
mod_lineinfo_headerstruct was expanded, so it is clear wherenum_entries,num_filesandfilenames_sizecome from.
Makes sense
Quoted 11 lines
Quoted 11 lines
include/linux/mod_lineinfo.h new file +11
-0+*/ +#ifndef _LINUX_MOD_LINEINFO_H +#define _LINUX_MOD_LINEINFO_H + +#ifdef __KERNEL__ +#include <linux/types.h> +#else +#include <stdint.h> +typedef uint32_t u32 ; +typedef uint16_t u16 ; +#endif
Quoted 8 lines
include/linux/mod_lineinfo.h line 33
Quoted 5 lines
include/linux/mod_lineinfo.h new file +5
-0+ +struct mod_lineinfo_header { +u32 num_entries; +u32 num_files; +u32 filenames_size;/* total bytes of concatenated filenames */ An alternative would be to say that the filenames data extends to the end of the section, without requiring an explicit
filenames_size.
I'd prefer to keep filenames_size explicit: it allows lineinfo_search() to validate filename offsets without knowing the section size. This keeps the search function reusable between vmlinux (where data comes from linker globals with no "section size") and modules (where it comes from a section). The cost is 4 bytes per module.
Quoted 4 lines
include/linux/mod_lineinfo.h line 34
Quoted 1 line
include/linux/mod_lineinfo.h new file +1
-0+u32 reserved;/* padding, must be 0 */ I believe the format should remain internal to the kernel, so there is no need for such a reserved member.
The format is indeed internal and we don't generally concern ourselves with out-of-tree modules. I've originally added it as a minimal safety net: if the format ever changes and a stale .ko with an old-format .mod_lineinfo gets loaded, the kernel would silently misparse lineinfo data.
I don't feel too strongly about it either way, but 4 bytes felt a pretty cheap price to pay for this safety net :)
Quoted 27 lines
Quoted 8 lines
include/linux/mod_lineinfo.h new file +8
-0+static inline u32 mod_lineinfo_lines_off (u32 num_entries) +{ +/* u16 file_ids[] may need 2-byte padding to align lines[] to 4 bytes */ +u32 off =mod_lineinfo_file_ids_off (num_entries) + + num_entries *sizeof (u16); +return (off +3 ) & ~3u ; +} +include/linux/mod_lineinfo.h line 66
Quoted 10 lines
include/linux/mod_lineinfo.h new file +10
-0+static inline u32 mod_lineinfo_file_offsets_off (u32 num_entries) +{ +return mod_lineinfo_lines_off (num_entries) + num_entries *sizeof (u32); +} + +static inline u32 mod_lineinfo_filenames_off (u32 num_entries,u32 num_files) +{ +return mod_lineinfo_file_offsets_off (num_entries) + + num_files *sizeof (u32); +}These helpers are used only from
kernel/module/kallsyms.c. I assume they are present in this header file to stay close to the description of the format.I personally find them quite verbose. The
module_lookup_lineinfo()function needs an intimate knowledge of the data format anyway. The code inmodule_lookup_lineinfo()could be replaced with just:
Quoted 5 lines
addrs = base + sizeof(struct mod_lineinfo_header); file_ids = addrs + num_entries * sizeof(u32); lines = (file_ids + num_entries * sizeof(u16) + 3) & ~3u; file_offsets = lines + num_entries * sizeof(u32); filenames = file_offsets + num_files * sizeof(u32);
They are very verbose mostly to make it easy for me to understand :)
Note that the next patch which adds compression rewrites these completely.
I kept these here just to make it easier to understand what's happening during my development work as well as reviews.
Quoted 16 lines
include/linux/module.h struct module { · line 512
Quoted 12 lines
include/linux/mod_lineinfo.h new file +2
-0+ +#endif /* _LINUX_MOD_LINEINFO_H */ include/linux/module.h +2
-0@@ -508,6 +508,8 @@ struct module {void *btf_data;void *btf_base_data;#endif +void *lineinfo_data;/* .mod_lineinfo section in MOD_RODATA */ +unsigned int lineinfo_data_size;The lineinfo-specific members should be enclosed within the
#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES.
Quoted 6 lines
This will require
module_lookup_lineinfo()to be conditionally compiled based onCONFIG_KALLSYMS_LINEINFO_MODULES, with a dummy version provided otherwise. Alternatively, accessors tomodule::lineinfo_dataandmodule::lineinfo_data_sizethat handleCONFIG_KALLSYMS_LINEINFO_MODULEScould be introduced ininclude/linux/module.h. For example, seemodule_buildid()oris_livepatch_module.
The struct members were deliberately left without #ifdef guards following Helge Deller's suggestion in the v1 review[1]. I don't really mind either way, but I'd prefer to have a consensus before flipping it back and forth.
Helge?
[1] https://lore.kernel.org/all/[email protected]/
Quoted 6 lines
Quoted 6 lines
include/linux/module.h +6
-0+ addrs = base +mod_lineinfo_addrs_off (); + file_ids = base +mod_lineinfo_file_ids_off (num_entries); + lines = base +mod_lineinfo_lines_off (num_entries); + file_offsets = base +mod_lineinfo_file_offsets_off (num_entries); + filenames = base +mod_lineinfo_filenames_off (num_entries, num_files); +
Quoted 8 lines
include/linux/module.h
Quoted 2 lines
include/linux/module.h +2
-0+/* Compute offset from module .text base */ + text_base = (unsigned long )mod->mem[MOD_TEXT ].base;The module::mem[] covers module memory regions. One can think of them as ELF segments, except they are created dynamically by the module loader. The code conflates the .text section and the TEXT segment. I'm not aware of any guarantee that the .text section will be always placed as the first section in this segment.
You're right that this conflates section and segment. In practice, .text is always first in MOD_TEXT because __layout_sections() processes SHF_EXECINSTR sections in ELF order, and .text is conventionally first. But I agree this shouldn't be an implicit assumption.
We can add a validation check at module load time to verify the assumption for now, and address it better when ...
Quoted 1 line
Relocations can be used to accurately reference the .text section.
... we add full relocation support.
Quoted 8 lines
include/linux/module.h
Quoted 5 lines
include/linux/module.h +5
-0+if (addr < text_base) +return false; + + raw_offset = addr - text_base; +if (raw_offset >UINT_MAX )The offsets in the addrs array are of the u32 type, so this should be strictly speaking checked against
U32_MAX.
Right
Quoted 10 lines
scripts/Makefile line 8
Quoted 7 lines
scripts/Makefile +1
-0@@ -5,6 +5,7 @@ hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO_MODULES) += gen_lineinfoThis line is unnecessary because
CONFIG_KALLSYMS_LINEINFO_MODULESdepends onCONFIG_KALLSYMS_LINEINFO.
It is. I mostly left it here for clarity's sake. I'll drop it.
Quoted 9 lines
scripts/Makefile if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · line 67
Quoted 7 lines
scripts/Makefile +3
-0@@ -59,6 +62,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) +endif +ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + +$(if $(newer-prereqs),$(call cmd,lineinfo_ko))Should this be '
if_changed_except.. vmlinux'?
Lineinfo generation doesn't depend on vmlinux - it reads DWARF directly from the .ko file itself. Unlike BTF (which uses vmlinux as a base for deduplication), there's no vmlinux prerequisite to exclude.
Quoted 4 lines
Quoted 4 lines
scripts/Makefile +0
-0@@ -194,9 +200,45 @@ static const char *make_relative(const char *path, const char *comp_dir) return p ? p + 1 : path; }
Quoted 9 lines
scripts/Makefile static const char *make_relative(const char *path, const char *comp_dir) · line 206
Quoted 7 lines
scripts/Makefile +4
-3- /* Fall back to basename */ - p = strrchr(path, '/'); - return p ? p + 1 : path; + /* + * Relative path — check for duplicated-path quirk from libdw + * on ET_REL files (e.g., "a/b.c/a/b.c" → "a/b.c"). + */When does this quirk occur? Is it a bug in libdw?
This occurs with elfutils libdw when processing ET_REL .ko files. libdw constructs source paths by concatenating DW_AT_comp_dir with DW_AT_name from the compilation unit. For modules where both are relative paths with the same prefix, this can produce doubled results like "net/foo/bar.c/net/foo/bar.c". It appears to be a libdw quirk with ET_REL DWARF handling.
Quoted 13 lines
scripts/Makefile static const char *make_relative(const char *path, const char *comp_dir) · line 216
Quoted 10 lines
scripts/Makefile +10
-0+ { + size_t len = strlen(path); + + for (p = path; (p = strchr(p, '/')) != NULL; p++) { + size_t prefix = p - path; + size_t rest = len - prefix - 1; + + if (rest == prefix && !memcmp(path, p + 1, prefix)) + return p + 1; + }Isn't this loop same as:
Quoted 3 lines
size_tmid = len / 2;
if (path[mid] == '/' && !memcmp(path, path + mid + 1, mid - 1))return path + mid + 1;
I think so! For a true duplication "X/X", the split is always at the exact midpoint (len/2), so the loop over every '/' is unnecessary. I'll adopt this approach with the memcmp length as mid rather than mid - 1 to compare the full second half.
Quoted 10 lines
Quoted 10 lines
scripts/Makefile +10
-0+static unsigned int r_type_abs32(unsigned int e_machine) +{ + switch (e_machine) { + case EM_X86_64: return 10; /* R_X86_64_32 */ + case EM_386: return 1; /* R_386_32 */ + case EM_AARCH64: return 258; /* R_AARCH64_ABS32 */ + case EM_ARM: return 2; /* R_ARM_ABS32 */ + case EM_RISCV: return 1; /* R_RISCV_32 */ + case EM_S390: return 4; /* R_390_32 */ + case EM_MIPS: return 2; /* R_MIPS_32 */
Quoted 8 lines
scripts/Makefile static const char *make_relative(const char *path, const char *comp_dir) · line 231
Quoted 5 lines
scripts/Makefile +5
-0+ case EM_PPC64: return 1; /* R_PPC64_ADDR32 */ + case EM_PPC: return 1; /* R_PPC_ADDR32 */ + case EM_LOONGARCH: return 1; /* R_LARCH_32 */ + case EM_PARISC: return 1; /* R_PARISC_DIR32 */ + default: return 0;The source file already includes
elf.hfrom elfutils. Is it necessary to hardcode these relocation values here?
Right!
Quoted 7 lines
Quoted 7 lines
scripts/Makefile +0
-0static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) { Dwarf_Off off = 0, next_off;@@ -295,6 +490,16 @@ static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) if (addr < text_addr) continue;
Quoted 11 lines
scripts/Makefile static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) · line 498
Quoted 6 lines
scripts/Makefile +6
-0+ /* + * In module mode, keep only .text addresses. + * In ET_REL .ko files, .init.text/.exit.text may + * overlap with .text address ranges, so we must + * explicitly check against the .text bounds. + */Nit: The use of "may" in this comment seems strange. It is fully expected that .text, .init.text, .exit.text or any other section will have their
sh_addrset to 0 in relocatable objects and therefore the sections have overlapping address ranges.
I guess I wasn't sure if it's really always the case. We can adjust the comment.
Quoted 8 lines
scripts/Makefile static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr) · line 502
Quoted 4 lines
scripts/Makefile +4
-0+ if (module_mode && text_section_end > text_section_start && + (addr < text_section_start || addr >= text_section_end)) + continue; +The current code is very specific to the .text section. It would be good to cover all sections in the future. I think it will naturally require use of relocations to refer to individual sections.
Yup, we can definitely give it a go in the future.
Signature · 2 lines
Thanks, Sasha
Petr Pavlu
maintainerRe: [PATCH v3 2/4] kallsyms: extend lineinfo to loadable modules
Quoted 22 lines · Sasha Levin
Quoted 13 lines
Quoted 13 lines
include/linux/mod_lineinfo.h new file +10
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* mod_lineinfo.h - Binary format for per-module source line information +* +* This header defines the layout of the .mod_lineinfo section embedded +* in loadable kernel modules. It is dual-use: included from both the +* kernel and the userspace gen_lineinfo tool. +* +* Section layout (all values in target-native endianness): +* Quoted 8 lines
include/linux/mod_lineinfo.h line 12
Quoted 2 lines
include/linux/mod_lineinfo.h new file +2
-0+* struct mod_lineinfo_header (16 bytes) +* u32 addrs[num_entries] -- offsets from .text base, sorted Modules are relocatable objects. The typical way to express a reference from one section to data in another section is to use relocations. Choosing to use an implicit base and resolved offsets means that the code has trouble correctly referencing the .text section and can't express line information data for other sections, such as .exit.text.
Quoted 5 lines
I agree, which is why I scoped this just to .text :)
My thinking was that using ELF relocations would add significant complexity to both the build tool and the runtime lookup path, which must remain NMI-safe and allocation-free.
I agree that using ELF relocations would be somewhat more complex. I had a look at adding a new u32/u64 "target" member to the mod_lineinfo_header struct which would point to the .text section in the resulting module via an absolute relocation. The idea was to keep the current offset and compression schema and only have a relocation that identifies the associated text section. I hoped to replace the objcopy steps in scripts/gen-mod-lineinfo.sh with an additional partial link to combine ${KO} with ${KO}.lineinfo.o.
However, the issue with this approach is that the relocation in ${KO}.lineinfo.o cannot cross-reference the .text symbol in ${KO}. It could be worked around by defining a start .text symbol in scripts/module.lds.S but this doesn't scale well if more sections need to be covered in the future. A separate utility would likely be required to join the two objects properly together.
For the runtime part, I don't immediately see any issues. Relocations are resolved by the module loader when a module is loaded, so this would have no effect on the functionality being NMI-safe or allocation-free.
Quoted 23 lines
Quoted 16 lines
include/linux/module.h struct module { · line 512
Quoted 12 lines
include/linux/mod_lineinfo.h new file +2
-0+ +#endif /* _LINUX_MOD_LINEINFO_H */ include/linux/module.h +2
-0@@ -508,6 +508,8 @@ struct module {void *btf_data;void *btf_base_data;#endif +void *lineinfo_data;/* .mod_lineinfo section in MOD_RODATA */ +unsigned int lineinfo_data_size;The lineinfo-specific members should be enclosed within the
#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES.Quoted 6 lines
This will require
module_lookup_lineinfo()to be conditionally compiled based onCONFIG_KALLSYMS_LINEINFO_MODULES, with a dummy version provided otherwise. Alternatively, accessors tomodule::lineinfo_dataandmodule::lineinfo_data_sizethat handleCONFIG_KALLSYMS_LINEINFO_MODULEScould be introduced ininclude/linux/module.h. For example, seemodule_buildid()oris_livepatch_module.
Quoted 3 lines
The struct members were deliberately left without #ifdef guards following Helge Deller's suggestion in the v1 review[1]. I don't really mind either way, but I'd prefer to have a consensus before flipping it back and forth.
The alternative suggestion I mentioned proposes using accessors to module::lineinfo_data and module::lineinfo_data_size that would handle CONFIG_KALLSYMS_LINEINFO_MODULES. This approach avoids having unused data in the module struct when CONFIG_KALLSYMS_LINEINFO_MODULES=n, while also allowing module_lookup_lineinfo() to use the IS_ENABLED(CONFIG_KALLSYMS_LINEINFO_MODULES) pattern and be always syntax-checked. Only the accessors won't be always syntax-checked but their code should be trivial.
The file include/linux/module.h could contain something like this:
struct {
[...]
#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES
void *lineinfo_data; /* .mod_lineinfo section in MOD_RODATA */
unsigned int lineinfo_data_size;
#endif
[...]
}[...]
static inline void *module_lineinfo_data(struct module *mod, unsigned int *size) {
#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES *size = mod->lineinfo_data_size; return mod->lineinfo_data; #else *size = 0; return NULL;
#endif
}
Quoted 10 lines
Quoted 9 lines
include/linux/module.h if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · line 67
Quoted 7 lines
include/linux/module.h +3
-0@@ -59,6 +62,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) +endif +ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + +$(if $(newer-prereqs),$(call cmd,lineinfo_ko))Should this be '
if_changed_except.. vmlinux'?
Quoted 3 lines
Lineinfo generation doesn't depend on vmlinux - it reads DWARF directly from the .ko file itself. Unlike BTF (which uses vmlinux as a base for deduplication), there's no vmlinux prerequisite to exclude.
The Makefile rule is:
%.ko: %.o %.mod.o .module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE
If $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN)) then $(objtree)/vmlinux is added as a prerequisite for the target. Since the new lineinfo_ko call uses $(newer-prereqs), it is invoked even when vmlinux changes.
However, the mentioned change to 'if_changed_except.. vmlinux' won't quite work either because if_changed_except appears to have a similar limitation as if_changed. It can be used only once per target due to storing the executed command in a corresponding .cmd file.
Additionally, even the already present if_changed_except call for ld_ko_o doesn't seem to work as intended because it excludes "./vmlinux" but the same rule prerequisite is canonicalized to only "vmlinux". This means modules are always linked even when only vmlinux changes. I'll send a fix for this separately.
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
include/linux/module.h +0
-0@@ -194,9 +200,45 @@ static const char *make_relative(const char *path, const char *comp_dir)return p ? p +1 : path; }Quoted 9 lines
include/linux/module.h static const char *make_relative(const char *path, const char *comp_dir) · line 206
Quoted 7 lines
include/linux/module.h +4
-3-/* Fall back to basename */ - p =strrchr (path,'/' ); -return p ? p +1 : path; +/* +* Relative path — check for duplicated-path quirk from libdw +* on ET_REL files (e.g., "a/b.c/a/b.c" → "a/b.c"). +*/ When does this quirk occur? Is it a bug in libdw?
Quoted 5 lines
This occurs with elfutils libdw when processing
ET_REL.ko files. libdw constructs source paths by concatenatingDW_AT_comp_dirwithDW_AT_namefrom the compilation unit. For modules where both are relative paths with the same prefix, this can produce doubled results like "net/foo/bar.c/net/foo/bar.c". It appears to be a libdw quirk withET_RELDWARF handling.
Thanks for the explanation.
-- Petr