2023-03-01 06:23:45

by Tianyi Liu

[permalink] [raw]
Subject: [PATCH] Discard .note.gnu.property in vmlinux

When the kernel image is finally linked, all the notes are packed into a
single .notes section, but these notes may have different alignments.

binutils above 2.32 adds a ".note.gnu.property" section to the compiled
output, which is 4-byte aligned on 32-bit, but 8-byte aligned on 64-bit.
At present, the notes generated by both the ELFNOTE macro and the VDSO
linker script are 4-byte aligned. So in a 64-bit kernel, packing segments
with different alignments will cause LibElf and tools like readelf to
crush or to read wrong values [1][2].

This patch discards ".note.gnu.property" from vmlinux.

Note that H.J. Lu has submitted a similar patch in the past[3],
but it was not merged.

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/

Signed-off-by: Tianyi Liu <[email protected]>
---
include/asm-generic/vmlinux.lds.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index d1f57e4868ed..6db0f664c7d6 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -891,9 +891,13 @@
/*
* Discard .note.GNU-stack, which is emitted as PROGBITS by the compiler.
* Otherwise, the type of .notes section would become PROGBITS instead of NOTES.
+ *
+ * Discard .note.gnu.property, which is 8-byte aligned and emitted by the
+ * compiler. Otherwise, the .notes section will be 8-byte aligned and other
+ * notes cannot be read.
*/
#define NOTES \
- /DISCARD/ : { *(.note.GNU-stack) } \
+ /DISCARD/ : { *(.note.GNU-stack) *(.note.gnu.property) } \
.notes : AT(ADDR(.notes) - LOAD_OFFSET) { \
BOUNDED_SECTION_BY(.note.*, _notes) \
} NOTES_HEADERS \
--
2.39.2