Skip to contentSkip to conversation

kbuild: Makefile.lib: fix offsets generation with debug info

PATCH Superseded Dmitry Kandybka 4 participants 10 messages Sep 28 – Nov 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: Makefile.lib: fix offsets generation with debug info

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

    .ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)"  #
    ...
    .ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

Fixes 7dd47b95b0f5 ("kbuild: consolidate redundant sed script ASM offset generation")
---
 scripts/Makefile.lib | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/Makefile.lib +2 -3
index 0a4fdd8bd975..32251eb6ca33 100644
@@ -493,9 +493,8 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p;}'
 endef
 
 # Use filechk to avoid rebuilds when a header changes, but the resulting file
Signature · 1 line
2.54.0
#1 lore raw

Hi Dmitry,

Quoted 32 lines

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

    .ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)"  #
    ...
    .ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

Fixes 7dd47b95b0f5 ("kbuild: consolidate redundant sed script ASM offset generation")
---
 scripts/Makefile.lib | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/Makefile.lib +0 -0
index 0a4fdd8bd975..32251eb6ca33 100644

scripts/Makefile.lib quiet_cmd_zstd22_with_size = ZSTD22 $@ · line 497

Quoted 8 lines
scripts/Makefile.lib +2 -3
@@ -493,9 +493,8 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p;}'

Sashiko notes [1] that this results in BLANK() being dropped, which does not result in any functional change as far as I can tell but it does kind of make these generated headers a little harder to read in some cases. Obviously the default fallback is what causes the problem here, so maybe we can change BLANK() to include something that we can look for like the name of the macro? This diff on top of your patchset appears to keep the generated files the same while resolving your issue.

[1]: https://sashiko.dev/#/patchset/[email protected]

include/linux/kbuild.h +1 -1
index e7be517aaaf6..945006392cde 100644
@@ -5,7 +5,7 @@
 #define DEFINE(sym, val) \
 	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
 
-#define BLANK() asm volatile("\n.ascii \"->\"" : : )
+#define BLANK() asm volatile("\n.ascii \"->BLANK()\"" : : )
 
 #define OFFSET(sym, str, mem) \
 	DEFINE(sym, offsetof(struct str, mem))
scripts/Makefile.lib +2 -1
index 32251eb6ca33..96c2ec50f06e 100644
@@ -494,7 +494,8 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
 	/^->/{s:->#\(.*\):/* \1 */:p; \
-	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p;}'
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p; \
+	s:->BLANK()::p;}'
 endef
 
 # Use filechk to avoid rebuilds when a header changes, but the resulting file
Signature · 2 lines
Cheers,
Nathan
#2 lore raw

PATCH v2 1/1 kbuild: Makefile.lib: fix offsets generation with debug info

In reply to Dmitry Kandybka

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

.ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)" #
...
.ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

v2:
- Add BLANK() macro for better readability of generated headers (suggested by Nathan Chancellor).

Fixes 7dd47b95b0f5 ("kbuild: consolidate redundant sed script ASM offset generation")
---
 include/linux/kbuild.h | 2 +-
 scripts/Makefile.lib   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/kbuild.h +1 -1
index e7be517aaaf6..118568971fdf 100644
@@ -5,7 +5,7 @@
 #define DEFINE(sym, val) \
 	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
 
-#define BLANK() asm volatile("\n.ascii \"->\"" : : )
+#define BLANK() do { asm volatile ("\n.ascii \"->BLANK()\"" : : ); } while (0)
 
 #define OFFSET(sym, str, mem) \
 	DEFINE(sym, offsetof(struct str, mem))
scripts/Makefile.lib +3 -3
index 0a4fdd8bd975..96c2ec50f06e 100644
@@ -493,9 +493,9 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p; \
+	s:->BLANK()::p;}'
 endef
 
 # Use filechk to avoid rebuilds when a header changes, but the resulting file
Signature · 1 line
2.54.0
#3 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] kbuild: unanchored sed substitutions in sed-offsets can break build --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kbuild: unanchored sed substitutions in sed-offsets can break build --

commit 48672ceb9d59753178f125f7c16956dd57252dcd
Author: Dmitry Kandybka <[email protected]>

kbuild: Makefile.lib: fix offsets generation with debug info

This commit fixes offset generation when debug information is enabled by making the sed pattern matching stricter and adding a BLANK() macro for readability. It prevents debug metadata from leaking into generated header files and breaking the build.

Quoted 5 lines
scripts/Makefile.lib +0 -0
index 19e96b30a122f..173bc3aa4247f 100644

scripts/Makefile.lib quiet_cmd_zstd22_with_size = ZSTD22 $@ · line 498

Quoted 9 lines
scripts/Makefile.lib +3 -3
@@ -493,9 +493,9 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p; \
+	s:->BLANK()::p;}'

[Severity: Low]
Could this unanchored sed substitution break the build under certain conditions?

If the kernel is built with debug info (e.g., -ggdb3), the compiler may emit .ascii directives containing debug strings from source code. If a string literal from an included header starts with -> and contains -># or ->BLANK() mid-string (for example, ->foo ->BLANK()), the unanchored sed substitution will match mid-string.

Because of the :p flag, wouldn't it print the partially modified pattern space (e.g., ->foo ), resulting in invalid C code being written to include/generated/asm-offsets.h?

Should the pattern be anchored (e.g., s:^->BLANK()$::p;) to ensure it only matches the intended kbuild macros?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
Quoted 39 lines · On Fri, Oct 16, 2026 at 09:02:07PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kbuild: unanchored sed substitutions in sed-offsets can break build --

commit 48672ceb9d59753178f125f7c16956dd57252dcd
Author: Dmitry Kandybka <[email protected]>

kbuild: Makefile.lib: fix offsets generation with debug info

This commit fixes offset generation when debug information is enabled by making the sed pattern matching stricter and adding a BLANK() macro for readability. It prevents debug metadata from leaking into generated header files and breaking the build.

Quoted 5 lines
scripts/Makefile.lib +0 -0
index 19e96b30a122f..173bc3aa4247f 100644

scripts/Makefile.lib quiet_cmd_zstd22_with_size = ZSTD22 $@ · line 498

Quoted 9 lines
scripts/Makefile.lib +3 -3
@@ -493,9 +493,9 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p; \
+	s:->BLANK()::p;}'

[Severity: Low]
Could this unanchored sed substitution break the build under certain conditions?

If the kernel is built with debug info (e.g., -ggdb3), the compiler may emit .ascii directives containing debug strings from source code. If a string literal from an included header starts with -> and contains -># or ->BLANK() mid-string (for example, ->foo ->BLANK()), the unanchored sed substitution will match mid-string.

Quoted 9 lines

Because of the :p flag, wouldn't it print the partially modified pattern space (e.g., ->foo ), resulting in invalid C code being written to include/generated/asm-offsets.h?

Should the pattern be anchored (e.g., s:^->BLANK()$::p;) to ensure it only matches the intended kbuild macros?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

This definitely feels a little contrived but anchoring at the beginning would avoid this and seems to match the rest of the macro.

s:^->BLANK()::p;}'

Signature · 2 lines
Cheers,
Nathan
#5 lore raw
Quoted 7 lines · Nathan Chancellor

This definitely feels a little contrived but anchoring at the beginning would avoid this and seems to match the rest of the macro.

s:^->BLANK()::p;}'

Thank you for better view on this patch. I agree with your correction.

--
best regards,
Dmitry

#6 lore raw

PATCH v3 1/1 kbuild: Makefile.lib: fix offsets generation with debug info

In reply to Dmitry Kandybka

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

.ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)" #
...
.ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

v2:
- Add BLANK() macro for better readability of generated headers (suggested by Nathan Chancellor).
v3:
- Check for ->BLANK() at the beginning of a line (suggested by Nathan Chancellor).

Fixes 7dd47b95b0f5 ("kbuild: consolidate redundant sed script ASM offset generation")
---
 include/linux/kbuild.h | 2 +-
 scripts/Makefile.lib   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/kbuild.h +1 -1
index e7be517aaaf6..118568971fdf 100644
@@ -5,7 +5,7 @@
 #define DEFINE(sym, val) \
 	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
 
-#define BLANK() asm volatile("\n.ascii \"->\"" : : )
+#define BLANK() do { asm volatile ("\n.ascii \"->BLANK()\"" : : ); } while (0)
 
 #define OFFSET(sym, str, mem) \
 	DEFINE(sym, offsetof(struct str, mem))
scripts/Makefile.lib +3 -3
index 0a4fdd8bd975..90422215046f 100644
@@ -493,9 +493,9 @@ quiet_cmd_zstd22_with_size = ZSTD22  $@
 # the .ascii directive whereas GCC keeps the <space> as-is.
 define sed-offsets
 	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
-	/^->/{s:->#\(.*\):/* \1 */:; \
-	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
-	s:->::; p;}'
+	/^->/{s:->#\(.*\):/* \1 */:p; \
+	s:^->\([[:alnum:]_]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:p; \
+	s:^->BLANK()::p;}'
 endef
 
 # Use filechk to avoid rebuilds when a header changes, but the resulting file
Signature · 1 line
2.54.0
maintainer Reviewed-by on v3 1/1
#7 lore raw
Quoted 21 lines

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

   .ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)"  #
   ...
   .ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

Quoted 6 lines

v2:
- Add BLANK() macro for better readability of generated headers (suggested by Nathan Chancellor).
v3:
- Check for ->BLANK() at the beginning of a line (suggested by Nathan Chancellor).

For the future, please include this changelog underneath the '---' below your signoff so that it is not included when we apply the patch. Nicolas, would you mind snipping that during application?

Quoted 2 lines
Fixes 7dd47b95b0f5 ("kbuild: consolidate redundant sed script ASM offset generation")
Signature · 2 lines
Cheers,
Nathan
maintainer
#8 lore raw
Quoted 28 lines · Nathan Chancellor
Quoted 21 lines

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

   .ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)"  #
   ...
   .ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

The current sed-offsets macro uses [^ ]* to match constant names, which is too permissive and can match unexpected characters when debug directives are present. Additionally, the fallback command 's:->::; p;' strips '->' from any unmatched line and prints it, which causes debug metadata to leak into the generated header file (include/generated/rq-offsets.h), breaking the build.

Replace [^ ]* with [[:alnum:]_]* to match only valid C identifiers, and remove the fallback 's:->::' command so that only successfully transformed lines are emitted into the generated header.

Quoted 6 lines

v2:
- Add BLANK() macro for better readability of generated headers (suggested by Nathan Chancellor).
v3:
- Check for ->BLANK() at the beginning of a line (suggested by Nathan Chancellor).

Quoted 3 lines

For the future, please include this changelog underneath the '---' below your signoff so that it is not included when we apply the patch. Nicolas, would you mind snipping that during application?

Sure, thanks for the reminder!

Signature · 1 line
Nicolas
maintainer
#9 lore raw
In reply to Dmitry Kandybka
Quoted 11 lines · Dmitry Kandybka

When debug information is enabled (e.g. -ggdb3), the assembler output file (e.g. kernel/sched/rq-offsets.s) contains additional directives such as .ascii with offset info and debug metadata.

For example:

   .ascii "->RQ_nr_pinned $3048 offsetof(struct rq, nr_pinned)"  #
   ...
   .ascii "->func); __data = (it_func_ptr)->data; ((void(*)(void *, pro"

[...]

Applied to kbuild/linux.git (kbuild-next-unstable), with patch history snipped. Thanks!

[1/1] kbuild: Makefile.lib: fix offsets generation with debug info

      https://git.kernel.org/kbuild/c/131d86d0

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped, reverted or modified (e.g. trailers).

Patches applied to the kbuild-next-unstable branch are accepted pending wider testing in linux-next and any post-commit review; they will generally be moved to the kbuild-next branch in about a week if no issues are found.

Best regards,

Signature · 1 line
Nicolas