[PATCH v2] ELF/notes: use pointer-to-const by default in ELFNOTE_...()

Jan Beulich posted 1 patch 5 days, 13 hours ago
[PATCH v2] ELF/notes: use pointer-to-const by default in ELFNOTE_...()
Posted by Jan Beulich 5 days, 13 hours ago
Not doing so results in a number of Misra rule 11.8 (casting away of
const-ness) violations. We need to allow kexec to use pointer to non-
const though, so provide a means to override the default.

For ELFNOTE_NEXT() we can do better and simply re-apply the type of the
incoming pointer.

No functional change intended.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
v2: Use ELFNOTE_NO_CONST as the external control identifier.

--- a/xen/common/kexec.c
+++ b/xen/common/kexec.c
@@ -6,6 +6,9 @@
  * - Magnus Damm <magnus@valinux.co.jp>
  */
 
+/* We're producing ELF notes here. */
+#define ELFNOTE_NO_CONST
+
 #include <xen/acpi.h>
 #include <xen/console.h>
 #include <xen/cpu.h>
--- a/xen/include/xen/elf.h
+++ b/xen/include/xen/elf.h
@@ -29,9 +29,16 @@
 
 #include <xen/elfstructs.h>
 
+#ifndef ELFNOTE_NO_CONST
+#define ELFNOTE_CONST const
+#else
+#define ELFNOTE_CONST
+#endif
+
 #define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3)
-#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_)))
+#define ELFNOTE_NAME(_n_) ((ELFNOTE_CONST char *)(_n_) + sizeof(*(_n_)))
 #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
-#define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN((_n_)->descsz)))
+#define ELFNOTE_NEXT(_n_) ((typeof(_n_))(ELFNOTE_DESC(_n_) + \
+                                         ELFNOTE_ALIGN((_n_)->descsz)))
 
 #endif /* __XEN_ELF_H__ */
Re: [PATCH v2] ELF/notes: use pointer-to-const by default in ELFNOTE_...()
Posted by Dmytro Prokopchuk 5 days, 8 hours ago

On 11/18/26 10:24, Jan Beulich wrote:
> Not doing so results in a number of Misra rule 11.8 (casting away of
> const-ness) violations. We need to allow kexec to use pointer to non-
> const though, so provide a means to override the default.
> 
> For ELFNOTE_NEXT() we can do better and simply re-apply the type of the
> incoming pointer.
> 
> No functional change intended.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> v2: Use ELFNOTE_NO_CONST as the external control identifier.

Reviewed-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>

> 
> --- a/xen/common/kexec.c
> +++ b/xen/common/kexec.c
> @@ -6,6 +6,9 @@
>    * - Magnus Damm <magnus@valinux.co.jp>
>    */
>   
> +/* We're producing ELF notes here. */
> +#define ELFNOTE_NO_CONST
> +
>   #include <xen/acpi.h>
>   #include <xen/console.h>
>   #include <xen/cpu.h>
> --- a/xen/include/xen/elf.h
> +++ b/xen/include/xen/elf.h
> @@ -29,9 +29,16 @@
>   
>   #include <xen/elfstructs.h>
>   
> +#ifndef ELFNOTE_NO_CONST
> +#define ELFNOTE_CONST const
> +#else
> +#define ELFNOTE_CONST
> +#endif
> +
>   #define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3)
> -#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_)))
> +#define ELFNOTE_NAME(_n_) ((ELFNOTE_CONST char *)(_n_) + sizeof(*(_n_)))
>   #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
> -#define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN((_n_)->descsz)))
> +#define ELFNOTE_NEXT(_n_) ((typeof(_n_))(ELFNOTE_DESC(_n_) + \
> +                                         ELFNOTE_ALIGN((_n_)->descsz)))
>   
>   #endif /* __XEN_ELF_H__ */

BR, Dmytro.