Bug 106571 - Implement -Wsection diag
Summary: Implement -Wsection diag
Status: WAITING
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2022-08-09 11:36 UTC by Boris
Modified: 2022-08-12 00:22 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-08-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Boris 2022-08-09 11:36:29 UTC
Hi,

clang has this -Wsection diag which does:

https://clang.llvm.org/docs/DiagnosticsReference.html#wsection

It would be good to have it in gcc too so that declarations like

extern u64 x86_spec_ctrl_current;

for variable definitions which belong to a specific section:

__attribute__((section(".data..percpu" ""))) __typeof__(u64) x86_spec_ctrl_current;

get caught:

arch/x86/kernel/cpu/bugs.c:58:21: error: section attribute is specified on redeclared variable [-Werror,-Wsection]

Thx.
Comment 1 Andrew Pinski 2022-08-09 17:01:57 UTC
This example seems not to be correct as this is section is not needed on the declaration only the definition.

If this is the example, I think the warning is incorrect and should not be implemented in GCC.
Comment 2 Boris 2022-08-09 17:08:26 UTC
How can you check a mismatch if only the definition has the section attribute?

Here's the kernel commit which fixes this for clang:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=db886979683a8360ced9b24ab1125ad0c4d2cf76

there's the same example in the commit message.
Comment 3 Andrew Pinski 2022-08-09 17:30:15 UTC
(In reply to Boris from comment #2)
> How can you check a mismatch if only the definition has the section
> attribute?

You don't need to.

> 
> Here's the kernel commit which fixes this for clang:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/
> ?id=db886979683a8360ced9b24ab1125ad0c4d2cf76
> 
> there's the same example in the commit message.

Oh I see the section here has more semantics than the normal section attribute does. There should be an enhancement request for a new attribute which does more than the current section attribute instead.
Comment 4 Michael Matz 2022-08-10 11:57:22 UTC
To elaborate: normally you don't need to know which section a variable will be placed into when generating code to access it.  (In fact you don't even need to
know if it's defined in the same linking component at all)

So, for declarations the section is immaterial, and checking for a mismatch
between decl and def doesn't seem appropriate.  In the general case.  This is not
the general case, the per-cpu sections of the kernel have special semantics,
and I agree with Andrew that checking would be better implemented with something
orthogonal to the section attribute itself.

Boris: what does DECLARE_PER_CPU() expand into?  Are there other attributes
that could be usefully checked for mismatch between decl and def?
Comment 5 Boris 2022-08-10 15:11:49 UTC
(In reply to Michael Matz from comment #4)
> Boris: what does DECLARE_PER_CPU() expand into?  Are there other attributes
> that could be usefully checked for mismatch between decl and def?

Unfortunately,

DECLARE_PER_CPU(u64, x86_spec_ctrl_current);

expands only to:

extern __attribute__((section(".data..percpu" ""))) __typeof__(u64) x86_spec_ctrl_current;