Skip to contentSkip to conversation

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

PATCH In review Jan Beulich 2 participants 2 messages Nov 18

lore
Share & tools
lore
#0 lore raw

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

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.

---
v2: Use ELFNOTE_NO_CONST as the external control identifier.

xen/common/kexec.c +3 -0
@@ -6,6 +6,9 @@
  * - Magnus Damm <[email protected]>
  */
 
+/* We're producing ELF notes here. */
+#define ELFNOTE_NO_CONST
+
 #include <xen/acpi.h>
 #include <xen/console.h>
 #include <xen/cpu.h>
xen/include/xen/elf.h +9 -2
@@ -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__ */
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Jan Beulich

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.

Quoted 8 lines

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

No functional change intended.

---
v2: Use ELFNOTE_NO_CONST as the external control identifier.

Quoted 24 lines
xen/common/kexec.c +3 -0
@@ -6,6 +6,9 @@
   * - Magnus Damm <[email protected]>
   */
 
+/* We're producing ELF notes here. */
+#define ELFNOTE_NO_CONST
+
  #include <xen/acpi.h>
  #include <xen/console.h>
  #include <xen/cpu.h>
xen/include/xen/elf.h +5 -0
@@ -29,9 +29,16 @@
 
  #include <xen/elfstructs.h>
 
+#ifndef ELFNOTE_NO_CONST
+#define ELFNOTE_CONST const
+#else
+#define ELFNOTE_CONST
+#endif

xen/include/xen/elf.h line 44

Quoted 10 lines
xen/include/xen/elf.h +4 -2
+
  #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.