Skip to contentSkip to conversation

compiler.h: avoid escaped section names

PATCH Applied Nick Desaulniers 5 participants 12 messages Sep 29, 2020 – Sep 30, 2020

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

PATCH 1/1 compiler.h: avoid escaped section names

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__section("___kentry" "+" #sym )			\
+	__attribute__((__section__("___kentry+" #sym)))		\
 	= (unsigned long)&sym;
 #endif
 
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
#1 lore raw
Quoted 34 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__section("___kentry" "+" #sym )			\
+	__attribute__((__section__("___kentry+" #sym)))		\
 	= (unsigned long)&sym;
 #endif
 
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

There are still other instances that need to be fixed, right?

Thanks.

#2 lore raw
Quoted 41 lines · Arvind Sankar
Quoted 34 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__section("___kentry" "+" #sym )			\
+	__attribute__((__section__("___kentry+" #sym)))		\
 	= (unsigned long)&sym;
 #endif
 
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

There are still other instances that need to be fixed, right?

Thanks.

Ignore the last question, I see you have separate patches for the rest.

#3 lore raw
In reply to Arvind Sankar
Quoted 42 lines · Arvind Sankar
Quoted 32 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -2
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
      extern typeof(sym) sym;                                 \
      static const unsigned long __kentry_##sym               \
      __used                                                  \
-     __section("___kentry" "+" #sym )                        \
+     __attribute__((__section__("___kentry+" #sym)))         \
      = (unsigned long)&sym;
 #endif
 
--
Quoted 2 lines

2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

Oh, sorry I didn't respond on that thread; I could have sworn I ran a grep for KENTRY back then.

$ git log -S KENTRY

Doesn't seem to get any hits, so I'm not sure what I should use for a proper Fixes tag in the event we just remove it. Let me grab lunch, then I'll send a v2 that just removes the KENTRY block. Thanks for the reminder!

And I don't remember what ever happened to Joe's script for treewide conversion of __section.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw
Quoted 49 lines · Nick Desaulniers
Quoted 42 lines · Arvind Sankar
Quoted 32 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -2
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
      extern typeof(sym) sym;                                 \
      static const unsigned long __kentry_##sym               \
      __used                                                  \
-     __section("___kentry" "+" #sym )                        \
+     __attribute__((__section__("___kentry+" #sym)))         \
      = (unsigned long)&sym;
 #endif
 
--
Quoted 2 lines

2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

Quoted 4 lines

Oh, sorry I didn't respond on that thread; I could have sworn I ran a grep for KENTRY back then.

$ git log -S KENTRY

Added by
b67067f1176df6ee727450546b58704e4b588563 ?

Quoted 11 lines

Doesn't seem to get any hits, so I'm not sure what I should use for a proper Fixes tag in the event we just remove it. Let me grab lunch, then I'll send a v2 that just removes the KENTRY block. Thanks for the reminder!

And I don't remember what ever happened to Joe's script for treewide conversion of __section.
--
Thanks,
~Nick Desaulniers

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw
Quoted 51 lines · Nick Desaulniers
Quoted 49 lines · Nick Desaulniers
Quoted 46 lines · Arvind Sankar
Quoted 32 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -2
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
      extern typeof(sym) sym;                                 \
      static const unsigned long __kentry_##sym               \
      __used                                                  \
-     __section("___kentry" "+" #sym )                        \
+     __attribute__((__section__("___kentry+" #sym)))         \
      = (unsigned long)&sym;
 #endif
 
--
Quoted 2 lines

2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

Quoted 8 lines
Quoted 4 lines

Oh, sorry I didn't respond on that thread; I could have sworn I ran a grep for KENTRY back then.

$ git log -S KENTRY

Added by
b67067f1176df6ee727450546b58704e4b588563 ?

Yeah but nothing seems to have used it. I assume for LTO we use some other technique to mark functions as used?

#6 lore raw
Quoted 56 lines · Arvind Sankar
Quoted 54 lines · Nick Desaulniers
Quoted 52 lines · Nick Desaulniers
Quoted 42 lines · Arvind Sankar
Quoted 32 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -2
index 92ef163a7479..ac45f6d40d39 100644
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
      extern typeof(sym) sym;                                 \
      static const unsigned long __kentry_##sym               \
      __used                                                  \
-     __section("___kentry" "+" #sym )                        \
+     __attribute__((__section__("___kentry+" #sym)))         \
      = (unsigned long)&sym;
 #endif
 
--
Quoted 2 lines

2.28.0.709.gb0816b6eb0-goog

There was this previous mini-thread:
https://lore.kernel.org/lkml/[email protected]/
and this older one:
https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines

Just for my own curiosity: how does KENTRY actually get used? grep doesn't show any hits, and the thread from 2019 was actually going to drop it if I read it right, and also just remove stringification from the __section macro.

Oh, sorry I didn't respond on that thread; I could have sworn I ran a grep for KENTRY back then.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
$ git log -S KENTRY

Added by
b67067f1176df6ee727450546b58704e4b588563 ?

Yeah but nothing seems to have used it. I assume for LTO we use some other technique to mark functions as used?

Nicholas, do you recall why KENTRY was added in b67067f1176df6ee727450546b58704e4b588563? May I remove that and the addition to INIT_DATA from that commit?

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw
In reply to Nick Desaulniers
Quoted 2 lines · Nick Desaulniers

And I don't remember what ever happened to Joe's script for treewide conversion of __section.

Nor do I but here (attached) is the script.

My recollection is there was some problem with mscros with ## concatenation in some converted uses.

#8 lore raw
Quoted 8 lines · Joe Perches
Quoted 2 lines · Nick Desaulniers

And I don't remember what ever happened to Joe's script for treewide conversion of __section.

Nor do I but here (attached) is the script.

My recollection is there was some problem with mscros with ## concatenation in some converted uses.

I believe I have it sorted now and I've attached a new version of the script.

It runs against -next (or any other tree) and produces a single commit.

It converts all the various uses of

	__attribute__((section(<foo>)))
to
	__section("<foo>")

changes the various macros with token pasting uses I believe appropriately as well.

Please give it a spin.

There were 4 problems as below.

With these 4 items fixed, the build works (seems to at least)

1: compiler_attributes.h needed to unquote the __section__(#S)
the old automated patch didn't apply as the file had changed

include/linux/compiler_attributes.h +1 -1
index ea7b756b1c8f..b6fef9033c0b 100644
@@ -254,7 +254,7 @@
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#i>
  * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec>
  */
-#define __section(S)                    __attribute__((__section__(#S)))
+#define __section(section)              __attribute__((__section__(section)))
 

2: The script needed to use different token pasting for

    __section(foo##bar##baz)

    The script converted this to
	__section("foo" ## bar ## "baz")
    instead this needed to be
	__section("foo" #bar "baz")

3: scripts/mod/modpost.c needed quoting of its internal __section uses:

scripts/mod/modpost.c +2 -2
index 69341b36f271..f882ce0d9327 100644
@@ -2254,7 +2254,7 @@ static void add_header(struct buffer *b, struct module *mod)
        buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
        buf_printf(b, "\n");
        buf_printf(b, "__visible struct module __this_module\n");
-       buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
+       buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
        buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
        if (mod->has_init)
                buf_printf(b, "\t.init = init_module,\n");
@@ -2308,7 +2308,7 @@ static int add_versions(struct buffer *b, struct module *mod)
 
        buf_printf(b, "\n");
        buf_printf(b, "static const struct modversion_info ____versions[]\n");
-       buf_printf(b, "__used __section(__versions) = {\n");
+       buf_printf(b, "__used __section(\"__versions\") = {\n");
 

4: tools/ was excluded, but now needs conversion

 tools/include/linux/objtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/linux/objtool.h +1 -1
index ab82c793c897..577f51436cf9 100644
@@ -60,7 +60,7 @@ struct unwind_hint {
  * For more information, see tools/objtool/Documentation/stack-validation.txt.
  */
 #define STACK_FRAME_NON_STANDARD(func) \
-	static void __used __section(.discard.func_stack_frame_non_standard) \
+	static void __used __section(".discard.func_stack_frame_non_standard") \
 		*__func_stack_frame_non_standard_##func = func
 
 #else /* __ASSEMBLY__ */
#9 lore raw

convert_section.pl attached

Here's a new version that does not update arch/powerpc/boot/ files to avoid the defective conversions that Nick found in powerpc.

#10 lore raw
In reply to Nick Desaulniers

Hi Nick,

Quoted 11 lines · Nick Desaulniers

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Let's add a comment about this in the code -- otherwise we/someone will convert it back without noticing. Also we could add another on __section itself warning about this.

Quoted 1 line

Is there a link / have we opened a bug on GCC's side too?

Thanks!

Cheers,
Miguel

#11 lore raw
In reply to Nick Desaulniers

From: Nick Desaulniers

Quoted 25 lines

Sent: 29 September 2020 20:43

The stringification operator, #, in the preprocessor escapes strings. For example, # "foo" becomes "\"foo\"". GCC and Clang differ in how they treat section names that contain \".

The portable solution is to not use a string literal with the preprocessor stringification operator.

In this case, since __section unconditionally uses the stringification operator, we actually want the more verbose __attribute__((__section__())).

Fixes commit e04462fb82f8 ("Compiler Attributes: remove uses of __attribute__ from compiler.h")
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +0 -0
index 92ef163a7479..ac45f6d40d39 100644

include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 160

Quoted 7 lines
include/linux/compiler.h +1 -1
@@ -155,7 +155,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__section("___kentry" "+" #sym )			\
+	__attribute__((__section__("___kentry+" #sym)))		\
 	= (unsigned long)&sym;
 #endif

I guess what this really wants is:

	__section(__kentry+##sym)

but that generates an error because you can only use ## between variable names.

Perhaps someone shouldn't have tries to be clever and not put an unusual character in the section name.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)