From a29b17f1cb53ee24075f09674d760a40eec6294f Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 14 Jan 2026 08:53:29 +0800 Subject: [PATCH] readelf: Display and return true for empty note section/segment Change readelf to display and return true for empty note section and segment. Add a linker test to verify that empty note section is discarded. binutils/ PR ld/33780 * readelf.c (process_notes_at): Display and return true for empty note section and segment. gas/ PR ld/33780 * testsuite/gas/elf/elf.exp: Run empty-note. * testsuite/gas/elf/empty-note.d: New file. * testsuite/gas/elf/empty-note.s: Likewise. ld/ PR ld/33780 * testsuite/ld-elf/empty-note.d: New file. * testsuite/ld-elf/empty-note.s: Likewise. Signed-off-by: H.J. Lu --- binutils/readelf.c | 13 +++++++++++-- gas/testsuite/gas/elf/elf.exp | 5 +++++ gas/testsuite/gas/elf/empty-note.d | 14 ++++++++++++++ gas/testsuite/gas/elf/empty-note.s | 16 ++++++++++++++++ ld/testsuite/ld-elf/empty-note.d | 11 +++++++++++ ld/testsuite/ld-elf/empty-note.s | 2 ++ 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 gas/testsuite/gas/elf/empty-note.d create mode 100644 gas/testsuite/gas/elf/empty-note.s create mode 100644 ld/testsuite/ld-elf/empty-note.d create mode 100644 ld/testsuite/ld-elf/empty-note.s diff --git a/binutils/readelf.c b/binutils/readelf.c index 5b7fc0dc1de..a889cd62607 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -23885,8 +23885,17 @@ process_notes_at (Filedata * filedata, char *end; bool res = true; - if (length <= 0) - return false; + if (length == 0) + { + if (section) + printf (_("Empty note section: %s\n"), + printable_section_name (filedata, section)); + else + printf (_("Empty note segment at file offset 0x%08" PRIx64 + " with length 0x%08" PRIx64 ":\n"), + offset, length); + return true; + } if (section) { diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp index b4b4272ab9f..457871e1afa 100644 --- a/gas/testsuite/gas/elf/elf.exp +++ b/gas/testsuite/gas/elf/elf.exp @@ -294,7 +294,9 @@ if { [is_elf_format] } then { run_dump_test "sh-link-abs-2" run_dump_test "sh-link-common-1" run_dump_test "sh-link-common-2" + set saved_ASFLAGS "$ASFLAGS" if [is_elf64 tmpdir/section30.o] { + set ASFLAGS "$ASFLAGS --defsym ALIGN=3" run_dump_test "sh-link-abs-3-64" run_dump_test "sh-link-abs-4-64" run_dump_test "sh-link-common-3-64" @@ -304,11 +306,14 @@ if { [is_elf_format] } then { run_dump_test "sh-link-large-common-3" run_dump_test "sh-link-large-common-4" } else { + set ASFLAGS "$ASFLAGS --defsym ALIGN=2" run_dump_test "sh-link-abs-3-32" run_dump_test "sh-link-abs-4-32" run_dump_test "sh-link-common-3-32" run_dump_test "sh-link-common-4-32" } + run_dump_test "empty-note" + set ASFLAGS "$saved_ASFLAGS" run_dump_test "sh-link-zero" run_dump_test "string" run_dump_test "size" diff --git a/gas/testsuite/gas/elf/empty-note.d b/gas/testsuite/gas/elf/empty-note.d new file mode 100644 index 00000000000..00141423ff0 --- /dev/null +++ b/gas/testsuite/gas/elf/empty-note.d @@ -0,0 +1,14 @@ +#readelf: -n +#notarget: am33_2.0-*-* hppa*-*-hpux* mn10300-*-* +# Assembly source file for the HPPA assembler is renamed and modifed by +# sed. mn10300 has relocations in .note.gnu.property section which +# elf_parse_notes doesn't support. + +#... +Empty note section: .note.foo + +Displaying notes found in: .note.gnu.property +[ ]+Owner[ ]+Data size[ ]+Description + GNU 0x[0-9a-f]+ NT_GNU_PROPERTY_TYPE_0 + Properties: UINT32_OR \(0xb000ffff\): 0x3 +#pass diff --git a/gas/testsuite/gas/elf/empty-note.s b/gas/testsuite/gas/elf/empty-note.s new file mode 100644 index 00000000000..44880391b4c --- /dev/null +++ b/gas/testsuite/gas/elf/empty-note.s @@ -0,0 +1,16 @@ + .section .note.foo,"","note" + .section ".note.gnu.property", "a" + .p2align ALIGN + .long 1f - 0f /* name length */ + .long 5f - 2f /* data length */ + .long 5 /* note type */ +0: .asciz "GNU" /* vendor name */ +1: + .p2align ALIGN +2: .long 0xb000ffff /* pr_type. */ + .long 4f - 3f /* pr_datasz. */ +3: + .long 0x3 +4: + .p2align ALIGN +5: diff --git a/ld/testsuite/ld-elf/empty-note.d b/ld/testsuite/ld-elf/empty-note.d new file mode 100644 index 00000000000..9cd9545a78b --- /dev/null +++ b/ld/testsuite/ld-elf/empty-note.d @@ -0,0 +1,11 @@ +#as: --generate-missing-build-notes=no +#ld: -shared +#readelf: -SlW +#xfail: ![check_shared_lib_support] + +#failif +#... + \[[ 0-9]+\] \.note.foo[ \t]+NOTE[ \t]+.* +#... + NOTE +.* +#... diff --git a/ld/testsuite/ld-elf/empty-note.s b/ld/testsuite/ld-elf/empty-note.s new file mode 100644 index 00000000000..7b0e88c2707 --- /dev/null +++ b/ld/testsuite/ld-elf/empty-note.s @@ -0,0 +1,2 @@ + .section .note.foo,"a","note" + .section .note.GNU-stack,"","progbits" -- 2.52.0