elfutils: Backport some patches to fix errors
authorRyan Keane <the.ra2.ifv@gmail.com>
Fri, 8 Nov 2024 06:36:23 +0000 (22:36 -0800)
committerPetr Štetiar <ynezz@true.cz>
Thu, 28 Nov 2024 18:47:36 +0000 (18:47 +0000)
On aarch64 musl gcc 14.x compiler, trying compiling elfutils 0.192 with
lto option enabled will cause null-dereference error.
Example error message:

...
elf_compress.c: In function 'elf_compress':
elf_compress.c:675:26: error: potential null pointer dereference [-Werror=null-dereference]
  675 |           shdr->sh_flags |= SHF_COMPRESSED;
      |                          ^
elf_compress_gnu.c: In function 'elf_compress_gnu':
elf_compress_gnu.c:127:25: error: potential null pointer dereference [-Werror=null-dereference]
  127 |           shdr->sh_size = new_size;
      |                         ^                      ^
...

This is a false postive warning but will abort compilation if gcc has
`-Werror` flag. This commit add a patch for this, see the bugzilla
report below.

This commit backports a series of patches to fix some errors.

Add patch:
- 007-add-libeu-symbols-to-libelf.patch
- 008-fix-autoconf-ENABLE_IMA_VERIFICATION.patch
- 009-fix-null-dereference-with-lto.patch

Link: https://sourceware.org/bugzilla/show_bug.cgi?id=32311
Signed-off-by: Ryan Keane <the.ra2.ifv@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/16886
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit afffcd09e5f15be53f327a80ee87d391312eb805)
Link: https://github.com/openwrt/openwrt/pull/17097
Signed-off-by: Petr Štetiar <ynezz@true.cz>
package/libs/elfutils/patches/007-add-libeu-symbols-to-libelf.patch [new file with mode: 0644]
package/libs/elfutils/patches/008-fix-autoconf-ENABLE_IMA_VERIFICATION.patch [new file with mode: 0644]
package/libs/elfutils/patches/009-fix-null-dereference-with-lto.patch [new file with mode: 0644]

diff --git a/package/libs/elfutils/patches/007-add-libeu-symbols-to-libelf.patch b/package/libs/elfutils/patches/007-add-libeu-symbols-to-libelf.patch
new file mode 100644 (file)
index 0000000..23489b5
--- /dev/null
@@ -0,0 +1,31 @@
+From f5d6e088f84dd05278c4698a21cbf1ff4569978d Mon Sep 17 00:00:00 2001
+From: Mark Wielaard <mark@klomp.org>
+Date: Tue, 22 Oct 2024 15:03:42 +0200
+Subject: [PATCH] libelf: Add libeu objects to libelf.a static archive
+
+libelf might use some symbols from libeu.a, specifically the eu-search
+wrappers. But we don't ship libeu.a separately. So include the libeu
+objects in the libelf.a archive to facilitate static linking.
+
+       * libelf/Makefile.am (libeu_objects): New variable.
+       (libelf_a_LIBADD): New, add libeu_objects.
+
+https://sourceware.org/bugzilla/show_bug.cgi?id=32293
+
+Signed-off-by: Mark Wielaard <mark@klomp.org>
+---
+ libelf/Makefile.am | 3 +++
+ 1 file changed, 3 insertions(+)
+
+--- a/libelf/Makefile.am
++++ b/libelf/Makefile.am
+@@ -122,6 +122,9 @@ libelf.so: $(srcdir)/libelf.map $(libelf
+       @$(textrel_check)
+       $(AM_V_at)ln -fs $@ $@.$(VERSION)
++libeu_objects = $(shell $(AR) t ../lib/libeu.a)
++libelf_a_LIBADD = $(addprefix ../lib/,$(libeu_objects))
++
+ install: install-am libelf.so
+       $(mkinstalldirs) $(DESTDIR)$(libdir)
+       $(INSTALL_PROGRAM) libelf.so $(DESTDIR)$(libdir)/libelf-$(PACKAGE_VERSION).so
diff --git a/package/libs/elfutils/patches/008-fix-autoconf-ENABLE_IMA_VERIFICATION.patch b/package/libs/elfutils/patches/008-fix-autoconf-ENABLE_IMA_VERIFICATION.patch
new file mode 100644 (file)
index 0000000..e5732d2
--- /dev/null
@@ -0,0 +1,24 @@
+From f3c664d069d81a4872a1ec8241ee709f37c53e9c Mon Sep 17 00:00:00 2001
+From: Aaron Merey <amerey@redhat.com>
+Date: Tue, 29 Oct 2024 14:16:57 -0400
+Subject: [PATCH] configure.ac: Fix ENABLE_IMA_VERIFICATION conditional
+
+Fix test statement for ENABLE_IMA_VERIFICATION always evalutating to
+false due to a missing 'x'.
+
+Signed-off-by: Aaron Merey <amerey@redhat.com>
+---
+ configure.ac | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+--- a/configure.ac
++++ b/configure.ac
+@@ -895,7 +895,7 @@ AS_IF([test "x$enable_debuginfod" != "xn
+ AM_CONDITIONAL([DEBUGINFOD],[test "x$enable_debuginfod" = "xyes"])
+ AS_IF([test "x$enable_debuginfod_ima_verification" = "xyes"],AC_DEFINE([ENABLE_IMA_VERIFICATION],[1],[Build IMA verification]))
+ AS_IF([test "x$have_libarchive" = "xyes"],AC_DEFINE([HAVE_LIBARCHIVE],[1],[Define to 1 if libarchive is available]))
+-AM_CONDITIONAL([ENABLE_IMA_VERIFICATION],[test "$enable_debuginfod_ima_verification" = "xyes"])
++AM_CONDITIONAL([ENABLE_IMA_VERIFICATION],[test "x$enable_debuginfod_ima_verification" = "xyes"])
+ AM_CONDITIONAL([OLD_LIBMICROHTTPD],[test "x$old_libmicrohttpd" = "xyes"])
+ dnl for /etc/profile.d/elfutils.{csh,sh}
diff --git a/package/libs/elfutils/patches/009-fix-null-dereference-with-lto.patch b/package/libs/elfutils/patches/009-fix-null-dereference-with-lto.patch
new file mode 100644 (file)
index 0000000..1179a1e
--- /dev/null
@@ -0,0 +1,193 @@
+From 8707194a9f2f0b13e53041b03ebfdbdbd2942e43 Mon Sep 17 00:00:00 2001
+From: Mark Wielaard <mark@klomp.org>
+Date: Tue, 5 Nov 2024 23:31:14 +0100
+Subject: [PATCH 1/1] libelf: Only fetch shdr once in elf_compress[_gnu]
+
+Some compilers assume the second call to elf[32|64]_getshdr can fail
+and produce error: potential null pointer dereference. Just store the
+result of the first call and reuse (when not NULL).
+
+       * libelf/elf_compress.c (elf_compress): Store getshdr result in
+       a shdr union var.
+       * libelf/elf_compress_gnu.c (): Likewise
+
+https://sourceware.org/bugzilla/show_bug.cgi?id=32311
+
+Signed-off-by: Mark Wielaard <mark@klomp.org>
+---
+ libelf/elf_compress.c     | 55 +++++++++++++++++++++------------------
+ libelf/elf_compress_gnu.c | 45 ++++++++++++++------------------
+ 2 files changed, 48 insertions(+), 52 deletions(-)
+
+--- a/libelf/elf_compress.c
++++ b/libelf/elf_compress.c
+@@ -584,25 +584,30 @@ elf_compress (Elf_Scn *scn, int type, un
+   Elf64_Xword sh_flags;
+   Elf64_Word sh_type;
+   Elf64_Xword sh_addralign;
++  union shdr
++  {
++    Elf32_Shdr *s32;
++    Elf64_Shdr *s64;
++  } shdr;
+   if (elfclass == ELFCLASS32)
+     {
+-      Elf32_Shdr *shdr = elf32_getshdr (scn);
+-      if (shdr == NULL)
++      shdr.s32 = elf32_getshdr (scn);
++      if (shdr.s32 == NULL)
+       return -1;
+-      sh_flags = shdr->sh_flags;
+-      sh_type = shdr->sh_type;
+-      sh_addralign = shdr->sh_addralign;
++      sh_flags = shdr.s32->sh_flags;
++      sh_type = shdr.s32->sh_type;
++      sh_addralign = shdr.s32->sh_addralign;
+     }
+   else
+     {
+-      Elf64_Shdr *shdr = elf64_getshdr (scn);
+-      if (shdr == NULL)
++      shdr.s64 = elf64_getshdr (scn);
++      if (shdr.s64 == NULL)
+       return -1;
+-      sh_flags = shdr->sh_flags;
+-      sh_type = shdr->sh_type;
+-      sh_addralign = shdr->sh_addralign;
++      sh_flags = shdr.s64->sh_flags;
++      sh_type = shdr.s64->sh_type;
++      sh_addralign = shdr.s64->sh_addralign;
+     }
+   if ((sh_flags & SHF_ALLOC) != 0)
+@@ -679,17 +684,17 @@ elf_compress (Elf_Scn *scn, int type, un
+        correctly and ignored when SHF_COMPRESSED is set.  */
+       if (elfclass == ELFCLASS32)
+       {
+-        Elf32_Shdr *shdr = elf32_getshdr (scn);
+-        shdr->sh_size = new_size;
+-        shdr->sh_addralign = __libelf_type_align (ELFCLASS32, ELF_T_CHDR);
+-        shdr->sh_flags |= SHF_COMPRESSED;
++        shdr.s32->sh_size = new_size;
++        shdr.s32->sh_addralign = __libelf_type_align (ELFCLASS32,
++                                                      ELF_T_CHDR);
++        shdr.s32->sh_flags |= SHF_COMPRESSED;
+       }
+       else
+       {
+-        Elf64_Shdr *shdr = elf64_getshdr (scn);
+-        shdr->sh_size = new_size;
+-        shdr->sh_addralign = __libelf_type_align (ELFCLASS64, ELF_T_CHDR);
+-        shdr->sh_flags |= SHF_COMPRESSED;
++        shdr.s64->sh_size = new_size;
++        shdr.s64->sh_addralign = __libelf_type_align (ELFCLASS64,
++                                                      ELF_T_CHDR);
++        shdr.s64->sh_flags |= SHF_COMPRESSED;
+       }
+       __libelf_reset_rawdata (scn, out_buf, new_size, 1, ELF_T_CHDR);
+@@ -731,17 +736,15 @@ elf_compress (Elf_Scn *scn, int type, un
+        correctly and ignored when SHF_COMPRESSED is set.  */
+       if (elfclass == ELFCLASS32)
+       {
+-        Elf32_Shdr *shdr = elf32_getshdr (scn);
+-        shdr->sh_size = scn->zdata_size;
+-        shdr->sh_addralign = scn->zdata_align;
+-        shdr->sh_flags &= ~SHF_COMPRESSED;
++        shdr.s32->sh_size = scn->zdata_size;
++        shdr.s32->sh_addralign = scn->zdata_align;
++        shdr.s32->sh_flags &= ~SHF_COMPRESSED;
+       }
+       else
+       {
+-        Elf64_Shdr *shdr = elf64_getshdr (scn);
+-        shdr->sh_size = scn->zdata_size;
+-        shdr->sh_addralign = scn->zdata_align;
+-        shdr->sh_flags &= ~SHF_COMPRESSED;
++        shdr.s64->sh_size = scn->zdata_size;
++        shdr.s64->sh_addralign = scn->zdata_align;
++        shdr.s64->sh_flags &= ~SHF_COMPRESSED;
+       }
+       __libelf_reset_rawdata (scn, scn->zdata_base,
+--- a/libelf/elf_compress_gnu.c
++++ b/libelf/elf_compress_gnu.c
+@@ -59,25 +59,30 @@ elf_compress_gnu (Elf_Scn *scn, int infl
+   Elf64_Xword sh_flags;
+   Elf64_Word sh_type;
+   Elf64_Xword sh_addralign;
++  union shdr
++  {
++    Elf32_Shdr *s32;
++    Elf64_Shdr *s64;
++  } shdr;
+   if (elfclass == ELFCLASS32)
+     {
+-      Elf32_Shdr *shdr = elf32_getshdr (scn);
+-      if (shdr == NULL)
++      shdr.s32 = elf32_getshdr (scn);
++      if (shdr.s32 == NULL)
+       return -1;
+-      sh_flags = shdr->sh_flags;
+-      sh_type = shdr->sh_type;
+-      sh_addralign = shdr->sh_addralign;
++      sh_flags = shdr.s32->sh_flags;
++      sh_type = shdr.s32->sh_type;
++      sh_addralign = shdr.s32->sh_addralign;
+     }
+   else
+     {
+-      Elf64_Shdr *shdr = elf64_getshdr (scn);
+-      if (shdr == NULL)
++      shdr.s64 = elf64_getshdr (scn);
++      if (shdr.s64 == NULL)
+       return -1;
+-      sh_flags = shdr->sh_flags;
+-      sh_type = shdr->sh_type;
+-      sh_addralign = shdr->sh_addralign;
++      sh_flags = shdr.s64->sh_flags;
++      sh_type = shdr.s64->sh_type;
++      sh_addralign = shdr.s64->sh_addralign;
+     }
+   /* Allocated sections, or sections that are already are compressed
+@@ -122,15 +127,9 @@ elf_compress_gnu (Elf_Scn *scn, int infl
+        sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
+        Just adjust the sh_size.  */
+       if (elfclass == ELFCLASS32)
+-      {
+-        Elf32_Shdr *shdr = elf32_getshdr (scn);
+-        shdr->sh_size = new_size;
+-      }
++        shdr.s32->sh_size = new_size;
+       else
+-      {
+-        Elf64_Shdr *shdr = elf64_getshdr (scn);
+-        shdr->sh_size = new_size;
+-      }
++        shdr.s64->sh_size = new_size;
+       __libelf_reset_rawdata (scn, out_buf, new_size, 1, ELF_T_BYTE);
+@@ -187,15 +186,9 @@ elf_compress_gnu (Elf_Scn *scn, int infl
+        sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
+        Just adjust the sh_size.  */
+       if (elfclass == ELFCLASS32)
+-      {
+-        Elf32_Shdr *shdr = elf32_getshdr (scn);
+-        shdr->sh_size = size;
+-      }
++      shdr.s32->sh_size = size;
+       else
+-      {
+-        Elf64_Shdr *shdr = elf64_getshdr (scn);
+-        shdr->sh_size = size;
+-      }
++      shdr.s64->sh_size = size;
+       __libelf_reset_rawdata (scn, buf_out, size, sh_addralign,
+                             __libelf_data_type (&ehdr, sh_type,