public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] gentoo commit in src/patchsets/elfutils/0.131: elfutils-0.118-PaX-support.patch elfutils-0.118-libelf-link.patch elfutils-portability.patch elfutils-robustify.patch elfutils-strip-copy-symtab.patch
@ 2007-12-30 18:59 Diego Petteno (flameeyes)
  0 siblings, 0 replies; 2+ messages in thread
From: Diego Petteno (flameeyes) @ 2007-12-30 18:59 UTC (permalink / raw
  To: gentoo-commits

flameeyes    07/12/30 18:59:51

  Added:                elfutils-0.118-PaX-support.patch
                        elfutils-0.118-libelf-link.patch
                        elfutils-portability.patch elfutils-robustify.patch
                        elfutils-strip-copy-symtab.patch
  Log:
  Update patches for elfutils-0.131.

Revision  Changes    Path
1.1                  src/patchsets/elfutils/0.131/elfutils-0.118-PaX-support.patch

file : http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-0.118-PaX-support.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-0.118-PaX-support.patch?rev=1.1&content-type=text/plain

Index: elfutils-0.118-PaX-support.patch
===================================================================
Add support for PaX ELF markings

Patch by Kevin F. Quinn <kevquinn@gentoo.org>

http://bugs.gentoo.org/115100

--- libelf/elf.h
+++ libelf/elf.h
@@ -568,6 +568,7 @@
 #define PT_GNU_EH_FRAME	0x6474e550	/* GCC .eh_frame_hdr segment */
 #define PT_GNU_STACK	0x6474e551	/* Indicates stack executability */
 #define PT_GNU_RELRO	0x6474e552	/* Read-only after relocation */
+#define PT_PAX_FLAGS    0x65041580  /* Indicates PaX flag markings */
 #define PT_LOSUNW	0x6ffffffa
 #define PT_SUNWBSS	0x6ffffffa	/* Sun Specific segment */
 #define PT_SUNWSTACK	0x6ffffffb	/* Stack segment */
--- src/elflint.c
+++ src/elflint.c
@@ -3187,6 +3187,7 @@
 
       if (phdr->p_type >= PT_NUM && phdr->p_type != PT_GNU_EH_FRAME
 	  && phdr->p_type != PT_GNU_STACK && phdr->p_type != PT_GNU_RELRO
+	  && phdr->p_type != PT_PAX_FLAGS
 	  /* Check for a known machine-specific type.  */
 	  && ebl_segment_type_name (ebl, phdr->p_type, NULL, 0) == NULL)
 	ERROR (gettext ("\



1.1                  src/patchsets/elfutils/0.131/elfutils-0.118-libelf-link.patch

file : http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-0.118-libelf-link.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-0.118-libelf-link.patch?rev=1.1&content-type=text/plain

Index: elfutils-0.118-libelf-link.patch
===================================================================
libdw needs libelf

http://bugs.gentoo.org/115385

--- src/Makefile.am
+++ src/Makefile.am
@@ -93,7 +93,7 @@
 ld_LDFLAGS = -rdynamic
 elflint_LDADD  = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 findtextrel_LDADD = $(libdw) $(libelf) $(libmudflap)
-addr2line_LDADD = $(libdw) $(libmudflap)
+addr2line_LDADD = $(libdw) $(libelf) $(libmudflap)
 elfcmp_LDADD = $(libebl) $(libelf) $(libmudflap) -ldl
 objdump_LDADD  = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 ranlib_LDADD = $(libelf) $(libeu) $(libmudflap)
--- tests/Makefile.am
+++ tests/Makefile.am
@@ -160,12 +160,12 @@
 get_aranges_LDADD = $(libdw) $(libelf) $(libmudflap)
 allfcts_LDADD = $(libdw) $(libelf) $(libmudflap)
 line2addr_no_Wformat = yes
-line2addr_LDADD = $(libdw) $(libmudflap)
-addrscopes_LDADD = $(libdw) $(libmudflap)
-funcscopes_LDADD = $(libdw) $(libmudflap)
-funcretval_LDADD = $(libdw) $(libmudflap)
-allregs_LDADD = $(libdw) $(libmudflap)
-find_prologues_LDADD = $(libdw) $(libmudflap)
+line2addr_LDADD = $(libdw) $(libelf) $(libmudflap)
+addrscopes_LDADD = $(libdw) $(libelf) $(libmudflap)
+funcscopes_LDADD = $(libdw) $(libelf) $(libmudflap)
+funcretval_LDADD = $(libdw) $(libelf) $(libmudflap)
+allregs_LDADD = $(libdw) $(libelf) $(libmudflap)
+find_prologues_LDADD = $(libdw) $(libelf) $(libmudflap)
 #show_ciefde_LDADD = ../libdwarf/libdwarf.so $(libelf) $(libmudflap)
 asm_tst1_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
 asm_tst2_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl



1.1                  src/patchsets/elfutils/0.131/elfutils-portability.patch

file : http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-portability.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-portability.patch?rev=1.1&content-type=text/plain

Index: elfutils-portability.patch
===================================================================
--- elfutils/backends/ChangeLog
+++ elfutils/backends/ChangeLog
@@ -135,6 +135,11 @@
 	* sparc_init.c: Likewise.
 	* x86_64_init.c: Likewise.
 
+2005-11-22  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (LD_AS_NEEDED): New variable, substituted by configure.
+	(libebl_%.so rule): Use it in place of -Wl,--as-needed.
+
 2005-11-19  Roland McGrath  <roland@redhat.com>
 
 	* ppc64_reloc.def: REL30 -> ADDR30.
@@ -157,6 +162,9 @@
 	* Makefile.am (uninstall): Don't try to remove $(pkgincludedir).
 	(CLEANFILES): Add libebl_$(m).so.
 
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 	* ppc_reloc.def: Update bits per Alan Modra <amodra@bigpond.net.au>.
 	* ppc64_reloc.def: Likewise.
 
--- elfutils/backends/Makefile.am
+++ elfutils/backends/Makefile.am
@@ -25,12 +25,14 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -DOBJDIR=\"$(shell pwd)\"
+WEXTRA = @WEXTRA@
+LD_AS_NEEDED = @LD_AS_NEEDED@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
 AM_CFLAGS =
 endif
-AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2 \
+AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused $(WEXTRA) -Wformat=2 \
 	     -std=gnu99
 INCLUDES = -I$(srcdir) -I$(top_srcdir)/libebl \
 	   -I$(top_srcdir)/libelf -I$(top_srcdir)/libdw \
@@ -62,7 +64,7 @@ textrel_check = if readelf -d $@ | fgrep
 libebl_%.so: libebl_%_pic.a libebl_%.map $(libelf) $(libdw)
 	$(LINK) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
 		-Wl,--version-script,$(word 2,$^) \
-		-Wl,-z,defs -Wl,--as-needed $(libelf) $(libdw) $(libmudflap)
+		-Wl,-z,defs $(LD_AS_NEEDED) $(libelf) $(libdw) $(libmudflap)
 	$(textrel_check)
 
 libebl_%.map: Makefile
--- elfutils/ChangeLog
+++ elfutils/ChangeLog
@@ -15,6 +15,10 @@
 	* configure.ac: Add dummy automake conditional to get dependencies
 	for non-generic linker right.  See src/Makefile.am.
 
+2005-11-22  Roland McGrath  <roland@redhat.com>
+
+	* configure.ac: Check for --as-needed linker option.
+
 2005-11-18  Roland McGrath  <roland@redhat.com>
 
 	* Makefile.am (DISTCHECK_CONFIGURE_FLAGS): New variable.
@@ -62,6 +66,17 @@
 	* Makefile.am (all_SUBDIRS): Add libdwfl.
 	* configure.ac: Write libdwfl/Makefile.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* configure.ac (WEXTRA): Check for -Wextra and set this substitution.
+
+ 	* configure.ac: Check for struct stat st_?tim members.
+ 	* src/strip.c (process_file): Use st_?time if st_?tim are not there.
+
+ 	* configure.ac: Check for futimes function.
+ 	* src/strip.c (handle_elf) [! HAVE_FUTIMES]: Use utimes instead.
+ 	(handle_ar) [! HAVE_FUTIMES]: Likewise.
+
 2005-05-19  Roland McGrath  <roland@redhat.com>
 
 	* configure.ac [AH_BOTTOM] (INTDECL, _INTDECL): New macros.
--- elfutils/configure.ac
+++ elfutils/configure.ac
@@ -70,6 +70,34 @@ CFLAGS="$old_CFLAGS"])
 AS_IF([test "x$ac_cv_c99" != xyes],
       AC_MSG_ERROR([gcc with C99 support required]))
 
+AC_CACHE_CHECK([for -Wextra option to $CC], ac_cv_cc_wextra, [dnl
+old_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -Wextra"
+AC_COMPILE_IFELSE([void foo (void) { }],
+		  ac_cv_cc_wextra=yes, ac_cv_cc_wextra=no)
+CFLAGS="$old_CFLAGS"])
+AC_SUBST(WEXTRA)
+AS_IF([test "x$ac_cv_cc_wextra" = xyes], [WEXTRA=-Wextra], [WEXTRA=-W])
+
+AC_CACHE_CHECK([for --as-needed linker option],
+	       ac_cv_as_needed, [dnl
+cat > conftest.c <<EOF
+int main (void) { return 0; }
+EOF
+if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+			    -fPIC -shared -o conftest.so conftest.c
+			    -Wl,--as-needed 1>&AS_MESSAGE_LOG_FD])
+then
+  ac_cv_as_needed=yes
+else
+  ac_cv_as_needed=no
+fi
+rm -f conftest*])
+AS_IF([test "x$ac_cv_as_needed" = xyes],
+      [LD_AS_NEEDED=-Wl,--as-needed], [LD_AS_NEEDED=])
+AC_SUBST(LD_AS_NEEDED)
+
+
 LOCALEDIR=$datadir
 AC_SUBST(LOCALEDIR)
 AC_DEFINE_UNQUOTED(LOCALEDIR, "$LOCALEDIR")
--- elfutils/lib/ChangeLog
+++ elfutils/lib/ChangeLog
@@ -16,6 +16,11 @@
 	* Makefile.am (libeu_a_SOURCES): Add it.
 	* system.h: Declare crc32_file.
 
+2005-02-07  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-04-30  Ulrich Drepper  <drepper@redhat.com>
 
 	* Makefile.am: Use -ffunction-sections for xmalloc.c.
--- elfutils/lib/Makefile.am
+++ elfutils/lib/Makefile.am
@@ -25,12 +25,13 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
 AM_CFLAGS =
 endif
-AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused -Wextra $($(*F)_CFLAGS)
+AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused $(WEXTRA) $($(*F)_CFLAGS)
 INCLUDES = -I$(srcdir)/../libelf -I..
 
 noinst_LIBRARIES = libeu.a
--- elfutils/libasm/ChangeLog
+++ elfutils/libasm/ChangeLog
@@ -32,6 +32,11 @@
 	* asm_error.c: Add new error ASM_E_IOERROR.
 	* libasmP.h: Add ASM_E_IOERROR definition.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-02-15  Ulrich Drepper  <drepper@redhat.com>
 
 	* Makefile.am (AM_CFLAGS): Add -Wunused -Wextra -Wformat=2.
--- elfutils/libasm/Makefile.am
+++ elfutils/libasm/Makefile.am
@@ -25,12 +25,13 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
 AM_CFLAGS =
 endif
-AM_CFLAGS += -std=gnu99 -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2
+AM_CFLAGS += -std=gnu99 -Wall -Wshadow -Werror -Wunused $(WEXTRA) -Wformat=2
 INCLUDES = -I. -I$(srcdir) -I.. \
 	   -I$(top_srcdir)/libelf -I$(top_srcdir)/libebl -I$(top_srcdir)/libdw\
 	   -I$(top_srcdir)/lib
--- elfutils/libcpu/ChangeLog
+++ elfutils/libcpu/ChangeLog
@@ -1,3 +1,8 @@
+2005-04-04  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it instead of -Wextra.
+
 2005-02-15  Ulrich Drepper  <drepper@redhat.com>
 
 	* Makefile (AM_CFLAGS): Add -Wunused -Wextra -Wformat=2.
--- elfutils/libcpu/Makefile.am
+++ elfutils/libcpu/Makefile.am
@@ -25,7 +25,8 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
-AM_CFLAGS = -Wall -Wshadow -Werror -Wextra -Wformat=2 -Wunused
+WEXTRA = @WEXTRA@
+AM_CFLAGS = -Wall -Wshadow -Werror $(WEXTRA) -Wformat=2 -Wunused
 INCLUDES = -I$(srcdir)
 
 noinst_LIBRARIES = libcpu_i386.a
--- elfutils/libdw/ChangeLog
+++ elfutils/libdw/ChangeLog
@@ -370,6 +370,11 @@
 
 2005-05-31  Roland McGrath  <roland@redhat.com>
 
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
 	* dwarf_formref_die.c (dwarf_formref_die): Add CU header offset to
 	formref offset.
 
--- elfutils/libdw/Makefile.am
+++ elfutils/libdw/Makefile.am
@@ -25,6 +25,7 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -DIS_LIBDW
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
@@ -33,7 +34,7 @@ endif
 if BUILD_STATIC
 AM_CFLAGS += -fpic
 endif
-AM_CFLAGS += -Wall -Werror -Wshadow -Wunused -Wformat=2 -Wextra -std=gnu99
+AM_CFLAGS += -Wall -Werror -Wshadow -Wunused -Wformat=2 $(WEXTRA) -std=gnu99
 INCLUDES = -I. -I$(srcdir) -I$(srcdir)/../libelf -I.. -I$(srcdir)/../lib
 VERSION = 1
 
--- elfutils/libdwfl/ChangeLog
+++ elfutils/libdwfl/ChangeLog
@@ -474,6 +474,11 @@
 
 2005-07-21  Roland McGrath  <roland@redhat.com>
 
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
+2005-07-21  Roland McGrath  <roland@redhat.com>
+
 	* Makefile.am (noinst_HEADERS): Add loc2c.c.
 
 	* test2.c (main): Check sscanf result to quiet warning.
--- elfutils/libdwfl/Makefile.am
+++ elfutils/libdwfl/Makefile.am
@@ -27,12 +27,13 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
 AM_CFLAGS =
 endif
-AM_CFLAGS += -Wall -Werror -Wshadow -Wunused -Wformat=2 -Wextra -std=gnu99
+AM_CFLAGS += -Wall -Werror -Wshadow -Wunused -Wformat=2 $(WEXTRA) -std=gnu99
 INCLUDES = -I. -I$(srcdir) -I$(srcdir)/../libelf -I$(srcdir)/../libebl \
 	   -I$(srcdir)/../libdw -I.. -I$(srcdir)/../lib
 VERSION = 1
--- elfutils/libebl/ChangeLog
+++ elfutils/libebl/ChangeLog
@@ -465,6 +465,11 @@
 	* Makefile.am (libebl_*_so_SOURCES): Set to $(*_SRCS) so dependency
 	tracking works right.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-05-21  Ulrich Drepper  <drepper@redhat.com>
 
 	* libebl_x86_64.map: Add x86_64_core_note.
--- elfutils/libebl/Makefile.am
+++ elfutils/libebl/Makefile.am
@@ -25,12 +25,13 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -DOBJDIR=\"$(shell pwd)\"
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
 AM_CFLAGS =
 endif
-AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2 \
+AM_CFLAGS += -fpic -Wall -Wshadow -Werror -Wunused $(WEXTRA) -Wformat=2 \
 	     -std=gnu99
 
 INCLUDES = -I$(srcdir) -I$(top_srcdir)/libelf -I$(top_srcdir)/libdw \
--- elfutils/libelf/ChangeLog
+++ elfutils/libelf/ChangeLog
@@ -221,6 +221,11 @@
 	If section content hasn't been read yet, do it before looking for the
 	block size.  If no section data present, infer size of section header.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-05-11  Ulrich Drepper  <drepper@redhat.com>
 
 	* elf.h: Update again.
--- elfutils/libelf/common.h
+++ elfutils/libelf/common.h
@@ -155,7 +155,7 @@ libelf_release_all (Elf *elf)
   (Var) = (sizeof (Var) == 1						      \
 	   ? (unsigned char) (Var)					      \
 	   : (sizeof (Var) == 2						      \
-	      ? bswap_16 (Var)						      \
+	      ? (unsigned short int) bswap_16 (Var)			      \
 	      : (sizeof (Var) == 4					      \
 		 ? bswap_32 (Var)					      \
 		 : bswap_64 (Var))))
@@ -164,7 +164,7 @@ libelf_release_all (Elf *elf)
   (Dst) = (sizeof (Var) == 1						      \
 	   ? (unsigned char) (Var)					      \
 	   : (sizeof (Var) == 2						      \
-	      ? bswap_16 (Var)						      \
+	      ? (unsigned short int) bswap_16 (Var)			      \
 	      : (sizeof (Var) == 4					      \
 		 ? bswap_32 (Var)					      \
 		 : bswap_64 (Var))))
--- elfutils/libelf/Makefile.am
+++ elfutils/libelf/Makefile.am
@@ -25,6 +25,7 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
@@ -33,7 +34,10 @@ endif
 if BUILD_STATIC
 AM_CFLAGS += -fpic
 endif
-AM_CFLAGS += -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2 -std=gnu99 \
+if BUILD_STATIC
+AM_CFLAGS += -fpic
+endif
+AM_CFLAGS += -Wall -Wshadow -Werror -Wunused $(WEXTRA) -Wformat=2 -std=gnu99 \
 	     $($(*F)_CFLAGS)
 INCLUDES = -I$(srcdir) -I$(top_srcdir)/lib -I..
 GCC_INCLUDE = -I$(shell $(CC) -print-file-name=include)
--- elfutils/src/ChangeLog
+++ elfutils/src/ChangeLog
@@ -87,6 +87,10 @@
 	* elflint.c (valid_e_machine): Add EM_ALPHA.
 	Reported by Christian Aichinger <Greek0@gmx.net>.
 
+	* strings.c (map_file): Define POSIX_MADV_SEQUENTIAL to
+	MADV_SEQUENTIAL if undefined.  	Don't call posix_madvise
+	if neither is defined.
+
 2006-08-08  Ulrich Drepper  <drepper@redhat.com>
 
 	* elflint.c (check_dynamic): Don't require DT_HASH for DT_SYMTAB.
@@ -163,6 +167,10 @@
 	* Makefile.am: Add hacks to create dependency files for non-generic
 	linker.
 
+2006-04-05  Roland McGrath  <roland@redhat.com>
+
+	* strings.c (MAP_POPULATE): Define to 0 if undefined.
+
 2006-06-12  Ulrich Drepper  <drepper@redhat.com>
 
 	* ldgeneric.c (ld_generic_generate_sections): Don't create .interp
@@ -511,6 +519,11 @@
 	* readelf.c (print_debug_loc_section): Fix indentation for larger
 	address size.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-05-30  Roland McGrath  <roland@redhat.com>
 
 	* readelf.c (print_debug_line_section): Print section offset of each
--- elfutils/src/findtextrel.c
+++ elfutils/src/findtextrel.c
@@ -488,7 +488,11 @@ ptrcompare (const void *p1, const void *
 
 
 static void
-check_rel (size_t nsegments, struct segments segments[nsegments],
+check_rel (size_t nsegments, struct segments segments[
+#if __GNUC__ >= 4
+						      nsegments
+#endif
+	   ],
 	   GElf_Addr addr, Elf *elf, Elf_Scn *symscn, Dwarf *dw,
 	   const char *fname, bool more_than_one, void **knownsrcs)
 {
--- elfutils/src/Makefile.am
+++ elfutils/src/Makefile.am
@@ -26,6 +26,7 @@
 ##
 DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H $(YYDEBUG) \
        -DSRCDIR=\"$(shell cd $(srcdir);pwd)\" -DOBJDIR=\"$(shell pwd)\"
+WEXTRA = @WEXTRA@
 if MUDFLAP
 AM_CFLAGS = -fmudflap
 else
@@ -33,7 +34,7 @@ AM_CFLAGS =
 endif
 AM_CFLAGS += -Wall -Wshadow -std=gnu99 $(native_ld_cflags) \
 	     $(if $($(*F)_no_Werror),,-Werror) \
-	     $(if $($(*F)_no_Wunused),,-Wunused -Wextra) \
+	     $(if $($(*F)_no_Wunused),,-Wunused $(WEXTRA)) \
 	     $(if $($(*F)_no_Wformat),,-Wformat=2) $(CFLAGS_$(*F))
 
 INCLUDES = -I$(srcdir) -I$(srcdir)/../libelf -I$(srcdir)/../libebl \
--- elfutils/src/strings.c
+++ elfutils/src/strings.c
@@ -51,6 +51,10 @@
 
 #include <system.h>
 
+#ifndef MAP_POPULATE
+# define MAP_POPULATE 0
+#endif
+
 
 /* Prototypes of local functions.  */
 static int read_fd (int fd, const char *fname, off64_t fdlen);
@@ -491,8 +495,13 @@ map_file (int fd, off64_t start_off, off
 		    fd, start_off);
       if (mem != MAP_FAILED)
 	{
+#if !defined POSIX_MADV_SEQUENTIAL && defined MADV_SEQUENTIAL
+# define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
+#endif
+#ifdef POSIX_MADV_SEQUENTIAL
 	  /* We will go through the mapping sequentially.  */
 	  (void) posix_madvise (mem, map_size, POSIX_MADV_SEQUENTIAL);
+#endif
 	  break;
 	}
       if (errno != EINVAL && errno != ENOMEM)
--- elfutils/src/strip.c
+++ elfutils/src/strip.c
@@ -52,6 +52,12 @@
 #include <libebl.h>
 #include <system.h>
 
+#ifdef HAVE_FUTIMES
+# define FUTIMES(fd, fname, tvp) futimes (fd, tvp)
+#else
+# define FUTIMES(fd, fname, tvp) utimes (fname, tvp)
+#endif
+
 
 /* Name and version of program.  */
 static void print_version (FILE *stream, struct argp_state *state);
@@ -300,8 +306,18 @@ process_file (const char *fname)
 
       /* If we have to preserve the timestamp, we need it in the
 	 format utimes() understands.  */
+#ifdef HAVE_STRUCT_STAT_ST_ATIM
       TIMESPEC_TO_TIMEVAL (&tv[0], &pre_st.st_atim);
+#else
+      tv[0].tv_sec = pre_st.st_atime;
+      tv[0].tv_usec = 0;
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_MTIM
       TIMESPEC_TO_TIMEVAL (&tv[1], &pre_st.st_mtim);
+#else
+      tv[1].tv_sec = pre_st.st_atime;
+      tv[1].tv_usec = 0;
+#endif
     }
 
   /* Open the file.  */
@@ -1691,7 +1707,7 @@ handle_elf (int fd, Elf *elf, const char
   /* If requested, preserve the timestamp.  */
   if (tvp != NULL)
     {
-      if (futimes (fd, tvp) != 0)
+      if (FUTIMES (fd, output_fname, tvp) != 0)
 	{
 	  error (0, errno, gettext ("\
 cannot set access and modification date of '%s'"),
@@ -1748,7 +1764,7 @@ handle_ar (int fd, Elf *elf, const char 
 
   if (tvp != NULL)
     {
-      if (unlikely (futimes (fd, tvp) != 0))
+      if (unlikely (FUTIMES (fd, fname, tvp) != 0))
 	{
 	  error (0, errno, gettext ("\
 cannot set access and modification date of '%s'"), fname);
--- elfutils/tests/ChangeLog
+++ elfutils/tests/ChangeLog
@@ -473,6 +473,11 @@
 	* Makefile.am (TESTS): Add run-elflint-test.sh.
 	(EXTRA_DIST): Add run-elflint-test.sh and testfile18.bz2.
 
+2005-05-31  Roland McGrath  <roland@redhat.com>
+
+	* Makefile.am (WEXTRA): New variable, substituted by configure.
+	(AM_CFLAGS): Use it in place of -Wextra.
+
 2005-05-24  Ulrich Drepper  <drepper@redhat.com>
 
 	* get-files.c (main): Use correct format specifier.
--- elfutils/tests/Makefile.am
+++ elfutils/tests/Makefile.am
@@ -25,12 +25,13 @@
 ## <http://www.openinventionnetwork.com>.
 ##
 DEFS = -DHAVE_CONFIG_H -D_GNU_SOURCE
+WEXTRA = @WEXTRA@
 if MUDFLAP
-AM_CFLAGS = -Wall -Werror -Wextra -std=gnu99 -fmudflap\
+AM_CFLAGS = -Wall -Werror $(WEXTRA) -std=gnu99 -fmudflap\
 	    $(if $($(*F)_no_Wformat),-Wno-format,-Wformat=2)
 BUILD_RPATH = \$$ORIGIN/../backends
 else
-AM_CFLAGS = -Wall -Werror -Wextra -std=gnu99 \
+AM_CFLAGS = -Wall -Werror $(WEXTRA) -std=gnu99 \
 	    $(if $($(*F)_no_Wformat),-Wno-format,-Wformat=2)
 BUILT_RPATH = \$$ORIGIN/../libasm:\$$ORIGIN/../libdw:\$$ORIGIN/../backends:\$$ORIGIN/../libelf
 endif



1.1                  src/patchsets/elfutils/0.131/elfutils-robustify.patch

file : http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-robustify.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-robustify.patch?rev=1.1&content-type=text/plain

Index: elfutils-robustify.patch
===================================================================
src/
2005-06-09  Roland McGrath  <roland@redhat.com>

	* readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
	(handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
	(handle_scngrp): Check for bogus sh_info.

	* strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
	st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
	Don't use assert on input values, instead bail with "illformed" error.

2005-05-17  Jakub Jelinek  <jakub@redhat.com>

libelf/
	* elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
	table fits into object's bounds.
	* elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
	elf->map_address.  Check if first section header fits into object's
	bounds.
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
	Check if section header table fits into object's bounds.
	* elf_begin.c (get_shnum): Ensure section headers fits into
	object's bounds.
	(file_read_elf): Make sure scncnt is small enough to allocate both
	ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
	tables fit into object's bounds.  Avoid memory leak on failure.

src/
	* elflint.c (check_hash): Don't check entries beyond end of section.
	(section_name): Return <invalid> if gelf_getshdr returns NULL.

2005-05-14  Jakub Jelinek  <jakub@redhat.com>

libelf/
	* libelfP.h (INVALID_NDX): Define.
	* gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
	* gelf_getlib.c (gelf_getlib): Likewise.
	* gelf_getmove.c (gelf_getmove): Likewise.
	* gelf_getrel.c (gelf_getrel): Likewise.
	* gelf_getrela.c (gelf_getrela): Likewise.
	* gelf_getsym.c (gelf_getsym): Likewise.
	* gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
	* gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
	* gelf_getversym.c (gelf_getversym): Likewise.
	* gelf_update_dyn.c (gelf_update_dyn): Likewise.
	* gelf_update_lib.c (gelf_update_lib): Likewise.
	* gelf_update_move.c (gelf_update_move): Likewise.
	* gelf_update_rel.c (gelf_update_rel): Likewise.
	* gelf_update_rela.c (gelf_update_rela): Likewise.
	* gelf_update_sym.c (gelf_update_sym): Likewise.
	* gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
	* gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
	* gelf_update_versym.c (gelf_update_versym): Likewise.
	* elf_newscn.c (elf_newscn): Check for overflow.
	* elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
	(__elfw2(LIBELFBITS,updatefile)): Likewise.
	* elf_begin.c (file_read_elf): Likewise.
	* elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
	* elf_getarsym.c (elf_getarsym): Likewise.
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
src/
	* elflint.c (section_name): Return "<invalid>" instead of
	crashing on invalid section name.
	(check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
	check_symtab_shndx, check_hash, check_versym): Robustify.

--- elfutils-0.127/src/readelf.c.robustify
+++ elfutils-0.127/src/readelf.c
@@ -958,6 +958,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
 
   GElf_Sym sym_mem;
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
+
   printf ((grpref[0] & GRP_COMDAT)
 	  ? ngettext ("\
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
@@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
 		      data->d_size / sizeof (Elf32_Word) - 1),
 	  elf_ndxscn (scn),
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
-	  elf_strptr (ebl->elf, symshdr->sh_link,
-		      gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
+	  (sym == NULL ? NULL
+	   : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
 	  ?: gettext ("<INVALID SYMBOL>"),
 	  data->d_size / sizeof (Elf32_Word) - 1);
 
@@ -1122,7 +1124,8 @@ static void
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 {
   int class = gelf_getclass (ebl->elf);
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink;
   Elf_Data *data;
   size_t cnt;
   size_t shstrndx;
@@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
     error (EXIT_FAILURE, 0,
 	   gettext ("cannot get section header string table index"));
 
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+	   elf_ndxscn (scn));
+
   printf (ngettext ("\
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
 		    "\
@@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
 	  shdr->sh_offset,
 	  (int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
 
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
@@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
     error (EXIT_FAILURE, 0,
 	   gettext ("cannot get section header string table index"));
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+				   &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+	   elf_ndxscn (scn));
+
   /* Now we can compute the number of entries in the section.  */
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
 				       ? sizeof (Elf32_Sym)
@@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
 		    nsyms),
 	  (unsigned int) elf_ndxscn (scn),
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
-  GElf_Shdr glink;
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
 		    " %lu local symbols  String table: [%2u] '%s'\n",
 		    shdr->sh_info),
 	  (unsigned long int) shdr->sh_info,
 	  (unsigned int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   fputs_unlocked (class == ELFCLASS32
 		  ? gettext ("\
@@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
     error (EXIT_FAILURE, 0,
 	   gettext ("cannot get section header string table index"));
 
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+				   &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+	   elf_ndxscn (scn));
+
   printf (ngettext ("\
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
 		    "\
@@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
 	  shdr->sh_offset,
 	  (unsigned int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
   for (int cnt = shdr->sh_info; --cnt >= 0; )
@@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
     error (EXIT_FAILURE, 0,
 	   gettext ("cannot get section header string table index"));
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+				   &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+	   elf_ndxscn (scn));
+
   int class = gelf_getclass (ebl->elf);
-  GElf_Shdr glink;
   printf (ngettext ("\
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
 		    "\
@@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
 	  shdr->sh_offset,
 	  (unsigned int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
   for (int cnt = shdr->sh_info; --cnt >= 0; )
@@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
       filename = NULL;
     }
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+				   &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+	   elf_ndxscn (scn));
+
   /* Print the header.  */
-  GElf_Shdr glink;
   printf (ngettext ("\
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
 		    "\
@@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
 	  shdr->sh_offset,
 	  (unsigned int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   /* Now we can finally look at the actual contents of this section.  */
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
@@ -2330,7 +2352,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
     ++counts[lengths[cnt]];
 
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
+					       shdr->sh_link),
+				   &glink_mem);
+  if (glink == NULL)
+    {
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
+	     elf_ndxscn (scn));
+      return;
+    }
+
   printf (ngettext ("\
 \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
 		    "\
@@ -2343,9 +2375,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
 	  shdr->sh_addr,
 	  shdr->sh_offset,
 	  (unsigned int) shdr->sh_link,
-	  elf_strptr (ebl->elf, shstrndx,
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-				    &glink)->sh_name));
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   if (extrastr != NULL)
     fputs (extrastr, stdout);
@@ -3655,6 +3685,16 @@ print_debug_aranges_section (Ebl *ebl __
       return;
     }
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink;
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
+  if (glink == NULL)
+    {
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
+	     elf_ndxscn (scn));
+      return;
+    }
+
   printf (ngettext ("\
 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
 		    "\
--- elfutils-0.127/src/strip.c.robustify
+++ elfutils-0.127/src/strip.c
@@ -412,6 +412,7 @@ handle_elf (int fd, Elf *elf, const char
   Elf_Data debuglink_crc_data;
   bool any_symtab_changes = false;
   Elf_Data *shstrtab_data = NULL;
+  size_t shdridx = 0;
 
   /* Create the full name of the file.  */
   if (prefix != NULL)
@@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
       goto fail_close;
     }
 
+  if (shstrndx >= shnum)
+    goto illformed;
+
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
+
   /* Storage for section information.  We leave room for two more
      entries since we unconditionally create a section header string
      table.  Maybe some weird tool created an ELF file without one.
@@ -563,7 +569,7 @@ handle_elf (int fd, Elf *elf, const char
     {
       /* This should always be true (i.e., there should not be any
 	 holes in the numbering).  */
-      assert (elf_ndxscn (scn) == cnt);
+      elf_assert (elf_ndxscn (scn) == cnt);
 
       shdr_info[cnt].scn = scn;
 
@@ -576,6 +582,7 @@ handle_elf (int fd, Elf *elf, const char
 					shdr_info[cnt].shdr.sh_name);
       if (shdr_info[cnt].name == NULL)
 	{
+	illformed:
 	  error (0, 0, gettext ("illformed file '%s'"), fname);
 	  goto fail_close;
 	}
@@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
 
       /* Remember the shdr.sh_link value.  */
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
+      if (shdr_info[cnt].old_sh_link >= shnum)
+	goto illformed;
 
       /* Sections in files other than relocatable object files which
 	 are not loaded can be freely moved by us.  In relocatable
@@ -597,7 +606,7 @@ handle_elf (int fd, Elf *elf, const char
 	 appropriate reference.  */
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
 	{
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
 	  shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
 	}
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
@@ -614,7 +623,12 @@ handle_elf (int fd, Elf *elf, const char
 	  for (inner = 1;
 	       inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
 	       ++inner)
+	    {
+	      if (grpref[inner] < shnum)
 	    shdr_info[grpref[inner]].group_idx = cnt;
+	      else
+		goto illformed;
+	    }
 
 	  if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
 	    /* If the section group contains only one element and this
@@ -625,7 +639,7 @@ handle_elf (int fd, Elf *elf, const char
 	}
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
 	{
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
 	  shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
 	}
 
@@ -633,7 +647,7 @@ handle_elf (int fd, Elf *elf, const char
 	 discarded right away.  */
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
 	{
-	  assert (shdr_info[cnt].group_idx != 0);
+	  elf_assert (shdr_info[cnt].group_idx != 0);
 
 	  if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
 	    {
@@ -708,11 +722,15 @@ handle_elf (int fd, Elf *elf, const char
 	    {
 	      /* If a relocation section is marked as being removed make
 		 sure the section it is relocating is removed, too.  */
-	      if ((shdr_info[cnt].shdr.sh_type == SHT_REL
+	      if (shdr_info[cnt].shdr.sh_type == SHT_REL
 		   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
+		{
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
+		    goto illformed;
+		  else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
 		shdr_info[cnt].idx = 1;
 	    }
+	    }
 
 	  if (shdr_info[cnt].idx == 1)
 	    {
@@ -737,7 +755,7 @@ handle_elf (int fd, Elf *elf, const char
 		  if (shdr_info[cnt].symtab_idx != 0
 		      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
 		    {
-		      assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
+		      elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
 
 		      shdr_info[shdr_info[cnt].symtab_idx].data
 			= elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
@@ -777,6 +795,9 @@ handle_elf (int fd, Elf *elf, const char
 		      else if (scnidx == SHN_XINDEX)
 			scnidx = xndx;
 
+		      if (scnidx >= shnum)
+			goto illformed;
+
 		      if (shdr_info[scnidx].idx == 0)
 			{
 			  /* Mark this section as used.  */
@@ -808,12 +829,16 @@ handle_elf (int fd, Elf *elf, const char
 		}
 
 	      /* Handle references through sh_info.  */
-	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
+	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
+		{
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
+		    goto illformed;
+		  else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
 		{
 		  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
 		  changes |= shdr_info[cnt].shdr.sh_info < cnt;
 		}
+		}
 
 	      /* Mark the section as investigated.  */
 	      shdr_info[cnt].idx = 2;
@@ -912,7 +937,7 @@ handle_elf (int fd, Elf *elf, const char
 	  error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
 		 elf_errmsg (-1));
 
-	assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
+	elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
 
 	/* Add this name to the section header string table.  */
 	shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
@@ -949,7 +974,7 @@ handle_elf (int fd, Elf *elf, const char
 	error (EXIT_FAILURE, 0,
 	       gettext ("while create section header section: %s"),
 	       elf_errmsg (-1));
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
 
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
       if (shdr_info[cnt].data == NULL)
@@ -980,7 +1005,7 @@ handle_elf (int fd, Elf *elf, const char
     }
 
   /* Index of the section header table in the shdr_info array.  */
-  size_t shdridx = cnt;
+  shdridx = cnt;
 
   /* Add the section header string table section name.  */
   shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
@@ -1005,7 +1030,7 @@ handle_elf (int fd, Elf *elf, const char
     error (EXIT_FAILURE, 0,
 	   gettext ("while create section header section: %s"),
 	   elf_errmsg (-1));
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
 
   /* Finalize the string table and fill in the correct indices in the
      section headers.  */
@@ -1095,20 +1120,20 @@ handle_elf (int fd, Elf *elf, const char
 		    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
 					     NULL);
 
-		    assert ((versiondata->d_size / sizeof (Elf32_Word))
+		    elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
 			    >= shdr_info[cnt].data->d_size / elsize);
 		  }
 
 		if (shdr_info[cnt].version_idx != 0)
 		  {
-		    assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
+		    elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
 		    /* This section has associated version
 		       information.  We have to modify that
 		       information, too.  */
 		    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
 					       NULL);
 
-		    assert ((versiondata->d_size / sizeof (GElf_Versym))
+		    elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
 			    >= shdr_info[cnt].data->d_size / elsize);
 		  }
 
@@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
 		      sec = shdr_info[sym->st_shndx].idx;
 		    else
 		      {
-			assert (shndxdata != NULL);
+			elf_assert (shndxdata != NULL);
 
 			sec = shdr_info[xshndx].idx;
 		      }
@@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
 			    nxshndx = sec;
 			  }
 
-			assert (sec < SHN_LORESERVE || shndxdata != NULL);
+			elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
 
 			if ((inner != destidx || nshndx != sym->st_shndx
 			     || (shndxdata != NULL && nxshndx != xshndx))
@@ -1207,7 +1232,7 @@ handle_elf (int fd, Elf *elf, const char
 		    else
 		      /* This is a section symbol for a section which has
 			 been removed.  */
-		      assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
+		      elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
 		  }
 
 		if (destidx != inner)
@@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
 		    {
 		      GElf_Sym sym_mem;
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
-		      assert (sym != NULL);
+		      elf_assert (sym != NULL);
 
 		      const char *name = elf_strptr (elf, strshndx,
 						     sym->st_name);
-		      assert (name != NULL);
+		      elf_assert (name != NULL);
 		      size_t hidx = elf_hash (name) % nbucket;
 
 		      if (bucket[hidx] == 0)
@@ -1394,7 +1419,7 @@ handle_elf (int fd, Elf *elf, const char
 	      else
 		{
 		  /* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
-		  assert (shdr_info[cnt].shdr.sh_entsize
+		  elf_assert (shdr_info[cnt].shdr.sh_entsize
 			  == sizeof (Elf64_Xword));
 
 		  Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
@@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
 		    {
 		      GElf_Sym sym_mem;
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
-		      assert (sym != NULL);
+		      elf_assert (sym != NULL);
 
 		      const char *name = elf_strptr (elf, strshndx,
 						     sym->st_name);
-		      assert (name != NULL);
+		      elf_assert (name != NULL);
 		      size_t hidx = elf_hash (name) % nbucket;
 
 		      if (bucket[hidx] == 0)
--- elfutils-0.127/src/elflint.c.robustify
+++ elfutils-0.127/src/elflint.c
@@ -123,6 +123,9 @@ static uint32_t shstrndx;
 /* Array to count references in section groups.  */
 static int *scnref;
 
+/* Number of sections.  */
+static unsigned int shnum;
+
 
 int
 main (int argc, char *argv[])
@@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
 {
   GElf_Shdr shdr_mem;
   GElf_Shdr *shdr;
+  const char *ret;
+
+  if ((unsigned int) idx > shnum)
+    return "<invalid>";
 
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
+  if (shdr == NULL)
+    return "<invalid>";
 
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
+  if (ret == NULL)
+    return "<invalid>";
+  return ret;
 }
 
 
@@ -337,10 +349,6 @@ static const int valid_e_machine[] =
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
 
 
-/* Number of sections.  */
-static unsigned int shnum;
-
-
 static void
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
 {
@@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot 
 	  }
       }
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext ("\
 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
 	   cnt, section_name (ebl, cnt));
@@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
 	       xndxscnidx, section_name (ebl, xndxscnidx));
     }
 
-  for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
       if (sym == NULL)
@@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid 
       else
 	{
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
-	  assert (name != NULL);
+	  assert (name != NULL
+		  || strshdr->sh_type != SHT_STRTAB);
 	}
 
       if (sym->st_shndx == SHN_XINDEX)
@@ -981,9 +991,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
     {
       GElf_Shdr rcshdr_mem;
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
-      assert (rcshdr != NULL);
 
-      if (rcshdr->sh_type == SHT_DYNAMIC)
+      if (rcshdr == NULL)
+	break;
+
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
 	{
 	  /* Found the dynamic section.  Look through it.  */
 	  Elf_Data *d = elf_getdata (scn, NULL);
@@ -993,7 +1005,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
 	    {
 	      GElf_Dyn dyn_mem;
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
-	      assert (dyn != NULL);
+
+	      if (dyn == NULL)
+		break;
 
 	      if (dyn->d_tag == DT_RELCOUNT)
 		{
@@ -1007,7 +1021,9 @@ section [%2d] '%s': DT_RELCOUNT used for
 		      /* Does the number specified number of relative
 			 relocations exceed the total number of
 			 relocations?  */
-		      if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
+		      if (shdr->sh_entsize != 0
+			  && dyn->d_un.d_val > (shdr->sh_size
+						/ shdr->sh_entsize))
 			ERROR (gettext ("\
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
 			       idx, section_name (ebl, idx),
@@ -1167,7 +1183,8 @@ section [%2d] '%s': no relocations for m
 	}
     }
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext (reltype == ELF_T_RELA ? "\
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
@@ -1389,7 +1406,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
   Elf_Data *symdata = elf_getdata (symscn, NULL);
   enum load_state state = state_undecided;
 
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Rela rela_mem;
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
@@ -1439,7 +1457,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
   Elf_Data *symdata = elf_getdata (symscn, NULL);
   enum load_state state = state_undecided;
 
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Rel rel_mem;
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
@@ -1543,7 +1562,8 @@ section [%2d] '%s': referenced as string
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
 	   idx, section_name (ebl, idx));
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext ("\
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
 	   idx, section_name (ebl, idx));
@@ -1553,7 +1573,7 @@ section [%2d] '%s': section entry size d
 	   idx, section_name (ebl, idx));
 
   bool non_null_warned = false;
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Dyn dyn_mem;
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
@@ -1834,6 +1854,8 @@ section [%2d] '%s': entry size does not 
 	   idx, section_name (ebl, idx));
 
   if (symshdr != NULL
+      && shdr->sh_entsize
+      && symshdr->sh_entsize
       && (shdr->sh_size / shdr->sh_entsize
 	  < symshdr->sh_size / symshdr->sh_entsize))
     ERROR (gettext ("\
@@ -1860,6 +1882,12 @@ section [%2d] '%s': extended section ind
     }
 
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
+  if (data == NULL)
+    {
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
+ 	     idx, section_name (ebl, idx));
+      return;
+    }
 
   if (*((Elf32_Word *) data->d_buf) != 0)
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
@@ -1902,7 +1930,7 @@ section [%2d] '%s': hash table section i
 
   size_t maxidx = nchain;
 
-  if (symshdr != NULL)
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
     {
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
 
@@ -1913,18 +1941,28 @@ section [%2d] '%s': hash table section i
       maxidx = symsize;
     }
 
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
   size_t cnt;
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+	break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
 	     idx, section_name (ebl, idx), cnt - 2);
+    }
 
   for (; cnt < 2 + nbucket + nchain; ++cnt)
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+	break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
 	     idx, section_name (ebl, idx), cnt - 2 - nbucket);
+    }
 }
 
 
@@ -1954,18 +1992,28 @@ section [%2d] '%s': hash table section i
       maxidx = symsize;
     }
 
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
   size_t cnt;
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+	break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
 	     idx, section_name (ebl, idx), cnt - 2);
+    }
 
   for (; cnt < 2 + nbucket + nchain; ++cnt)
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+	break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
-	     idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
+	       idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
+    }
 }
 
 
@@ -1990,7 +2038,7 @@ section [%2d] '%s': bitmask size not pow
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
     {
       ERROR (gettext ("\
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
 	     idx, section_name (ebl, idx), (long int) shdr->sh_size,
 	     (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
       return;
@@ -2644,8 +2692,9 @@ section [%2d] '%s' refers in sh_link to 
 
   /* The number of elements in the version symbol table must be the
      same as the number of symbols.  */
-  if (shdr->sh_size / shdr->sh_entsize
-      != symshdr->sh_size / symshdr->sh_entsize)
+  if (shdr->sh_entsize && symshdr->sh_entsize
+      && (shdr->sh_size / shdr->sh_entsize
+	  != symshdr->sh_size / symshdr->sh_entsize))
     ERROR (gettext ("\
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
 	   idx, section_name (ebl, idx),
--- elfutils-0.127/libelf/elf_begin.c.robustify
+++ elfutils-0.127/libelf/elf_begin.c
@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
 
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
 	{
-	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
+	      || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
 	    /* Cannot read the first section header.  */
 	    return 0;
 
@@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
 
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
 	{
-	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
 	    /* Cannot read the first section header.  */
 	    return 0;
 
@@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
     /* Could not determine the number of sections.  */
     return NULL;
 
+  /* Check for too many sections.  */
+  if (e_ident[EI_CLASS] == ELFCLASS32)
+    {
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
+	return NULL;
+    }
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
+    return NULL;
+
   /* We can now allocate the memory.  */
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
@@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
 	{
 	  /* We can use the mmapped memory.  */
 	  elf->state.elf32.ehdr = ehdr;
+
+	  if (unlikely (ehdr->e_shoff >= maxsize)
+	      || unlikely (ehdr->e_shoff
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
+	    {
+	    free_and_out:
+	      free (elf);
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
+	      return NULL;
+	    }
 	  elf->state.elf32.shdr
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
+
 	  if (ehdr->e_phnum > 0)
+	    {
 	    /* Assign a value only if there really is a program
 	       header.  Otherwise the value remains NULL.  */
+	      if (unlikely (ehdr->e_phoff >= maxsize)
+		  || unlikely (ehdr->e_phoff
+			       + ehdr->e_phnum
+			       * sizeof (Elf32_Phdr) > maxsize))
+		goto free_and_out;
 	    elf->state.elf32.phdr
 	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
+	    }
 
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
 	    {
@@ -383,13 +412,26 @@ file_read_elf (int fildes, void *map_add
 	{
 	  /* We can use the mmapped memory.  */
 	  elf->state.elf64.ehdr = ehdr;
+
+	  if (unlikely (ehdr->e_shoff >= maxsize)
+	      || unlikely (ehdr->e_shoff
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
+	    goto free_and_out;
 	  elf->state.elf64.shdr
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
+
 	  if (ehdr->e_phnum > 0)
+	    {
 	    /* Assign a value only if there really is a program
 	       header.  Otherwise the value remains NULL.  */
+	      if (unlikely (ehdr->e_phoff >= maxsize)
+		  || unlikely (ehdr->e_phoff
+			       + ehdr->e_phnum
+			       * sizeof (Elf32_Phdr) > maxsize))
+		goto free_and_out;
 	    elf->state.elf64.phdr
 	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
+	    }
 
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
 	    {
--- elfutils-0.127/libelf/libelfP.h.robustify
+++ elfutils-0.127/libelf/libelfP.h
@@ -574,4 +574,13 @@ extern uint32_t __libelf_crc32 (uint32_t
       }									      \
   } while (0)
 
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
+   2 bytes.  */
+#if SIZE_MAX > 4294967295U
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
+#else
+# define INVALID_NDX(ndx, type) \
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
+#endif
+
 #endif  /* libelfP.h */
--- elfutils-0.127/libelf/gelf_update_move.c.robustify
+++ elfutils-0.127/libelf/gelf_update_move.c
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
+  if (INVALID_NDX (ndx, GElf_Move)
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
--- elfutils-0.127/libelf/gelf_getsym.c.robustify
+++ elfutils-0.127/libelf/gelf_getsym.c
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
 	 table entries has to be adopted.  The user better has provided
 	 a buffer where we can store the information.  While copying the
 	 data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
 	 index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym)
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_getrela.c.robustify
+++ elfutils-0.127/libelf/gelf_getrela.c
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  result = NULL;
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
 	 correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  result = NULL;
--- elfutils-0.127/libelf/gelf_getmove.c.robustify
+++ elfutils-0.127/libelf/gelf_getmove.c
@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Move)
+      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
--- elfutils-0.127/libelf/gelf_update_symshndx.c.robustify
+++ elfutils-0.127/libelf/gelf_update_symshndx.c
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
   if (symdata == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
 	}
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_update_dyn.c.robustify
+++ elfutils-0.127/libelf/gelf_update_dyn.c
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
     {
       /* The type of the data better should match.  */
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
 	}
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Dyn)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_update_rela.c.robustify
+++ elfutils-0.127/libelf/gelf_update_rela.c
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       /* The type of the data better should match.  */
@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
 	}
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_getsymshndx.c.robustify
+++ elfutils-0.127/libelf/gelf_getsymshndx.c
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
      section index table.  */
   if (likely (shndxdata_scn != NULL))
     {
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Word)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Word)
+		       > shndxdata_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
 	 table entries has to be adopted.  The user better has provided
 	 a buffer where we can store the information.  While copying the
 	 data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
 
       /* The data is already in the correct form.  Just make sure the
 	 index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym)
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/elf32_newphdr.c.robustify
+++ elfutils-0.127/libelf/elf32_newphdr.c
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
     {
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
+	{
+	  result = NULL;
+	  goto out;
+	}
+
       /* Allocate a new program header with the appropriate number of
 	 elements.  */
       result = (ElfW2(LIBELFBITS,Phdr) *)
--- elfutils-0.127/libelf/gelf_update_sym.c.robustify
+++ elfutils-0.127/libelf/gelf_update_sym.c
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
 	}
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_getsyminfo.c.robustify
+++ elfutils-0.127/libelf/gelf_getsyminfo.c
@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo)
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
--- elfutils-0.127/libelf/gelf_getlib.c.robustify
+++ elfutils-0.127/libelf/gelf_getlib.c
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
   GElf_Lib *result = NULL;
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Lib)
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
--- elfutils-0.127/libelf/elf32_updatefile.c.robustify
+++ elfutils-0.127/libelf/elf32_updatefile.c
@@ -201,6 +201,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
   /* Write all the sections.  Well, only those which are modified.  */
   if (shnum > 0)
     {
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
+ 	return 1;
+
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
@@ -571,6 +574,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
   /* Write all the sections.  Well, only those which are modified.  */
   if (shnum > 0)
     {
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
+	return 1;
+
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
 #if EV_NUM != 2
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
--- elfutils-0.127/libelf/gelf_getversym.c.robustify
+++ elfutils-0.127/libelf/gelf_getversym.c
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Versym)
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       result = NULL;
--- elfutils-0.127/libelf/elf_getarsym.c.robustify
+++ elfutils-0.127/libelf/elf_getarsym.c
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
       size_t index_size = atol (tmpbuf);
 
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
+#if SIZE_MAX <= 4294967295U
+	  || n >= SIZE_MAX / sizeof (Elf_Arsym)
+#endif
 	  || n * sizeof (uint32_t) > index_size)
 	{
 	  /* This index table cannot be right since it does not fit into
--- elfutils-0.127/libelf/gelf_getrel.c.robustify
+++ elfutils-0.127/libelf/gelf_getrel.c
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  result = NULL;
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
 	 correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  result = NULL;
--- elfutils-0.127/libelf/gelf_update_versym.c.robustify
+++ elfutils-0.127/libelf/gelf_update_versym.c
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
+  if (INVALID_NDX (ndx, GElf_Versym)
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
--- elfutils-0.127/libelf/gelf_getdyn.c.robustify
+++ elfutils-0.127/libelf/gelf_getdyn.c
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
 	 table entries has to be adopted.  The user better has provided
 	 a buffer where we can store the information.  While copying the
 	 data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
 	 index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, GElf_Dyn)
+	  || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/gelf_update_syminfo.c.robustify
+++ elfutils-0.127/libelf/gelf_update_syminfo.c
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
     {
       /* The type of the data better should match.  */
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
   rwlock_wrlock (scn->elf->lock);
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo)
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
--- elfutils-0.127/libelf/gelf_update_rel.c.robustify
+++ elfutils-0.127/libelf/gelf_update_rel.c
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       /* The type of the data better should match.  */
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
 	}
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel)
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel)
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
 	{
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
 	  goto out;
--- elfutils-0.127/libelf/elf_newscn.c.robustify
+++ elfutils-0.127/libelf/elf_newscn.c
@@ -104,10 +104,18 @@ elf_newscn (elf)
   else
     {
       /* We must allocate a new element.  */
-      Elf_ScnList *newp;
+      Elf_ScnList *newp = NULL;
 
       assert (elf->state.elf.scnincr > 0);
 
+      if (
+#if SIZE_MAX <= 4294967295U
+	  likely (elf->state.elf.scnincr
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
+#else
+	  1
+#endif
+	  )
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
 				     + ((elf->state.elf.scnincr *= 2)
 					* sizeof (Elf_Scn)), 1);
--- elfutils-0.127/libelf/gelf_update_lib.c.robustify
+++ elfutils-0.127/libelf/gelf_update_lib.c
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
     {
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
 
   /* Check whether we have to resize the data buffer.  */
   int result = 0;
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, Elf64_Lib)
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
--- elfutils-0.127/libelf/elf_getshstrndx.c.robustify
+++ elfutils-0.127/libelf/elf_getshstrndx.c
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
 	      if (elf->map_address != NULL
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
 		  && (ALLOW_UNALIGNED
-		      || (((size_t) ((char *) elf->map_address + offset))
+		      || (((size_t) ((char *) elf->map_address
+			   + elf->start_offset + offset))
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
+		{
+		  /* First see whether the information in the ELF header is
+		     valid and it does not ask for too much.  */
+		  if (unlikely (offset + sizeof (Elf32_Shdr)
+				> elf->maximum_size))
+		    {
+		      /* Something is wrong.  */
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+		      result = -1;
+		      goto out;
+		    }
+
 		/* We can directly access the memory.  */
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
+					 + offset))->sh_link;
+		}
 	      else
 		{
 		  /* We avoid reading in all the section headers.  Just read
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
 	      if (elf->map_address != NULL
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
 		  && (ALLOW_UNALIGNED
-		      || (((size_t) ((char *) elf->map_address + offset))
+		      || (((size_t) ((char *) elf->map_address
+			   + elf->start_offset + offset))
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
+		{
+		  /* First see whether the information in the ELF header is
+		     valid and it does not ask for too much.  */
+		  if (unlikely (offset + sizeof (Elf64_Shdr)
+				> elf->maximum_size))
+		    {
+		      /* Something is wrong.  */
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+		      result = -1;
+		      goto out;
+		    }
+
 		/* We can directly access the memory.  */
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
+		  num = ((Elf64_Shdr *) (elf->map_address
+			 + elf->start_offset + offset))->sh_link;
+		}
 	      else
 		{
 		  /* We avoid reading in all the section headers.  Just read
--- elfutils-0.127/libelf/elf32_getshdr.c.robustify
+++ elfutils-0.127/libelf/elf32_getshdr.c
@@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
 	goto out;
 
       size_t shnum;
-      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
+      if (INTUSE (elf_getshnum) (elf, &shnum) != 0
+	  || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
 	goto out;
       size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
 
@@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
 
       if (elf->map_address != NULL)
 	{
+	  /* First see whether the information in the ELF header is
+	     valid and it does not ask for too much.  */
+	  if (unlikely (ehdr->e_shoff >= elf->maximum_size)
+	      || unlikely (ehdr->e_shoff + size > elf->maximum_size))
+	    {
+	      /* Something is wrong.  */
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+	      goto free_and_out;
+	    }
+
 	  ElfW2(LIBELFBITS,Shdr) *notcvt;
 
 	  /* All the data is already mapped.  If we could use it
--- elfutils-0.127/libelf/elf32_getphdr.c.robustify
+++ elfutils-0.127/libelf/elf32_getphdr.c
@@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
 
       if (elf->map_address != NULL)
 	{
+	  /* First see whether the information in the ELF header is
+	     valid and it does not ask for too much.  */
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
+	      || unlikely (ehdr->e_phoff + size > elf->maximum_size))
+	    {
+	      /* Something is wrong.  */
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
+	      goto out;
+	    }
+
 	  /* All the data is already mapped.  Use it.  */
 	  void *file_phdr = ((char *) elf->map_address
 			     + elf->start_offset + ehdr->e_phoff);



1.1                  src/patchsets/elfutils/0.131/elfutils-strip-copy-symtab.patch

file : http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-strip-copy-symtab.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/elfutils/0.131/elfutils-strip-copy-symtab.patch?rev=1.1&content-type=text/plain

Index: elfutils-strip-copy-symtab.patch
===================================================================
2006-09-19  Jakub Jelinek  <jakub@redhat.com>

	* strip.c (handle_elf): Formatting.  If any relocation sections
	stripped into separate debug info reference symtab that is kept,
	emit symtab/strtab also into the separate debug info file.

--- elfutils/src/strip.c
+++ elfutils/src/strip.c
@@ -399,6 +399,7 @@ handle_elf (int fd, Elf *elf, const char
     Elf_Scn *newscn;
     struct Ebl_Strent *se;
     Elf32_Word *newsymidx;
+    void *debug_data;
   } *shdr_info = NULL;
   Elf_Scn *scn;
   size_t cnt;
@@ -826,8 +827,39 @@ handle_elf (int fd, Elf *elf, const char
      The ones that are not removed in the stripped file are SHT_NOBITS.  */
   if (debug_fname != NULL)
     {
+      /* libbfd and apps using it don't cope with separate debuginfo objects
+	 with relocation sections against SHT_NOBITS .symtab/.strtab
+	 - libbfd isn't able to look up the .symtab/.strtab in the stripped
+	 object instead.  As a workaround, emit .symtab/.strtab in both
+	 places.  */
       for (cnt = 1; cnt < shnum; ++cnt)
 	{
+	  if (shdr_info[cnt].idx == 0
+	      && (shdr_info[cnt].shdr.sh_type == SHT_REL
+		  || shdr_info[cnt].shdr.sh_type == SHT_RELA)
+	      && (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
+	    {
+	      Elf32_Word symtabidx = shdr_info[cnt].old_sh_link;
+	      struct shdr_info *si = &shdr_info[symtabidx];
+	      si->debug_data = "";
+	      shdr_info[si->old_sh_link].debug_data = "";
+	      if (si->symtab_idx)
+		shdr_info[si->symtab_idx].debug_data = "";
+
+	      if (si->shdr.sh_type != SHT_SYMTAB
+		  || (si->shdr.sh_flags & SHF_ALLOC)
+		  || shdr_info[si->old_sh_link].shdr.sh_type != SHT_STRTAB
+		  || (shdr_info[si->old_sh_link].shdr.sh_flags & SHF_ALLOC)
+		  || (si->symtab_idx
+		      && (shdr_info[si->symtab_idx].shdr.sh_flags
+			  & SHF_ALLOC)))
+		error (EXIT_FAILURE, 0,
+		       gettext ("invalid symtab/strtab referenced by nonallocated section"));
+	    }
+	}
+
+      for (cnt = 1; cnt < shnum; ++cnt)
+	{
 	  scn = elf_newscn (debugelf);
 	  if (scn == NULL)
 	    error (EXIT_FAILURE, 0,
@@ -835,8 +867,9 @@ handle_elf (int fd, Elf *elf, const char
 		   elf_errmsg (-1));
 
 	  bool discard_section = (shdr_info[cnt].idx > 0
 				  && shdr_info[cnt].shdr.sh_type != SHT_NOTE
-				  && cnt != ehdr->e_shstrndx);
+				  && cnt != ehdr->e_shstrndx
+				  && shdr_info[cnt].debug_data == NULL);
 
 	  /* Set the section header in the new file.  */
 	  GElf_Shdr debugshdr = shdr_info[cnt].shdr;
@@ -864,6 +897,13 @@ handle_elf (int fd, Elf *elf, const char
 	  *debugdata = *shdr_info[cnt].data;
 	  if (discard_section)
 	    debugdata->d_buf = NULL;
+	  else if (shdr_info[cnt].debug_data != NULL)
+	    {
+	      shdr_info[cnt].debug_data = xmalloc (debugdata->d_size);
+	      memcpy (shdr_info[cnt].debug_data, debugdata->d_buf,
+		      debugdata->d_size);
+	      debugdata->d_buf = shdr_info[cnt].debug_data;
+	    }
 	}
 
       /* Finish the ELF header.  Fill in the fields not handled by
@@ -1055,7 +1095,7 @@ handle_elf (int fd, Elf *elf, const char
 	    shdr_info[shdr_info[cnt].shdr.sh_info].idx;
 
 	/* Get the data from the old file if necessary.  We already
-           created the data for the section header string table.  */
+	   created the data for the section header string table.  */
 	if (cnt < shnum)
 	  {
 	    if (shdr_info[cnt].data == NULL)
@@ -1264,6 +1304,13 @@ handle_elf (int fd, Elf *elf, const char
 	      if (shdr_info[shdr_info[cnt].old_sh_link].newsymidx == NULL)
 		continue;
 
+	      /* If the symbol table is not discarded, but additionally
+		 duplicated in separate debug file and this section
+		 is discarded, don't adjust anything.  */
+	      if (shdr_info[cnt].idx == 0
+		  && shdr_info[shdr_info[cnt].old_sh_link].debug_data != NULL)
+		continue;
+
 	      Elf32_Word *newsymidx
 		= shdr_info[shdr_info[cnt].old_sh_link].newsymidx;
 	      Elf_Data *d = elf_getdata (shdr_info[cnt].idx == 0
@@ -1322,6 +1369,13 @@ handle_elf (int fd, Elf *elf, const char
 	      if (shdr_info[symtabidx].newsymidx == NULL)
 		continue;
 
+	      /* If the symbol table is not discarded, but additionally
+		 duplicated in separate debug file and this section
+		 is discarded, don't adjust anything.  */
+	      if (shdr_info[cnt].idx == 0
+		  && shdr_info[symtabidx].debug_data != NULL)
+		continue;
+
 	      assert (shdr_info[cnt].idx > 0);
 
 	      /* The hash section in the new file.  */
@@ -1447,7 +1501,7 @@ handle_elf (int fd, Elf *elf, const char
 			  chain[hidx] = inner;
 			}
 		    }
-	        }
+		}
 	    }
 	  else if (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym)
 	    {
@@ -1460,6 +1514,13 @@ handle_elf (int fd, Elf *elf, const char
 	      if (shdr_info[symtabidx].newsymidx == NULL)
 		continue;
 
+	      /* If the symbol table is not discarded, but additionally
+		 duplicated in separate debug file and this section
+		 is discarded, don't adjust anything.  */
+	      if (shdr_info[cnt].idx == 0
+		  && shdr_info[symtabidx].debug_data != NULL)
+		continue;
+
 	      assert (shdr_info[cnt].idx > 0);
 
 	      /* The symbol version section in the new file.  */
@@ -1504,20 +1565,27 @@ handle_elf (int fd, Elf *elf, const char
 	  else if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
 	    {
 	      /* Check whether the associated symbol table changed.  */
-	      if (shdr_info[shdr_info[cnt].old_sh_link].newsymidx != NULL)
-		{
-		  /* Yes the symbol table changed.  Update the section
-		     header of the section group.  */
-		  scn = elf_getscn (newelf, shdr_info[cnt].idx);
-		  GElf_Shdr shdr_mem;
-		  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
-		  assert (shdr != NULL);
+	      if (shdr_info[shdr_info[cnt].old_sh_link].newsymidx == NULL)
+		continue;
 
-		  size_t stabidx = shdr_info[cnt].old_sh_link;
-		  shdr->sh_info = shdr_info[stabidx].newsymidx[shdr->sh_info];
+	      /* If the symbol table is not discarded, but additionally
+		 duplicated in separate debug file and this section
+		 is discarded, don't adjust anything.  */
+	      if (shdr_info[cnt].idx == 0
+		  && shdr_info[shdr_info[cnt].old_sh_link].debug_data != NULL)
+		continue;
 
-		  (void) gelf_update_shdr (scn, shdr);
-		}
+	      /* Yes the symbol table changed.  Update the section
+		 header of the section group.  */
+	      scn = elf_getscn (newelf, shdr_info[cnt].idx);
+	      GElf_Shdr shdr_mem;
+	      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+	      assert (shdr != NULL);
+
+	      size_t stabidx = shdr_info[cnt].old_sh_link;
+	      shdr->sh_info = shdr_info[stabidx].newsymidx[shdr->sh_info];
+
+	      (void) gelf_update_shdr (scn, shdr);
 	    }
 	}
     }
@@ -1647,7 +1715,10 @@ handle_elf (int fd, Elf *elf, const char
 	 table indices.  */
       if (any_symtab_changes)
 	for (cnt = 1; cnt <= shdridx; ++cnt)
-	  free (shdr_info[cnt].newsymidx);
+	  {
+	    free (shdr_info[cnt].newsymidx);
+	    free (shdr_info[cnt].debug_data);
+	  }
 
       /* Free the memory.  */
       if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)

--- elfutils-0.124/tests/run-strip-test5.sh.~1~
+++ elfutils-0.124/tests/run-strip-test5.sh
@@ -1,5 +1,5 @@
 original=testfile8
-stripped=testfile16
-debugfile=testfile16.debug
+stripped=testfile16.symtab
+debugfile=testfile16.symtab.debug
 
 . $srcdir/run-strip-test.sh




-- 
gentoo-commits@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gentoo-commits] gentoo commit in src/patchsets/elfutils/0.131: elfutils-0.118-PaX-support.patch elfutils-0.118-libelf-link.patch elfutils-portability.patch elfutils-robustify.patch elfutils-strip-copy-symtab.patch
@ 2011-10-03 22:45 Mike Frysinger (vapier)
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Frysinger (vapier) @ 2011-10-03 22:45 UTC (permalink / raw
  To: gentoo-commits

vapier      11/10/03 22:45:43

  Removed:              elfutils-0.118-PaX-support.patch
                        elfutils-0.118-libelf-link.patch
                        elfutils-portability.patch elfutils-robustify.patch
                        elfutils-strip-copy-symtab.patch
  Log:
  no longer used



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-10-03 22:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-30 18:59 [gentoo-commits] gentoo commit in src/patchsets/elfutils/0.131: elfutils-0.118-PaX-support.patch elfutils-0.118-libelf-link.patch elfutils-portability.patch elfutils-robustify.patch elfutils-strip-copy-symtab.patch Diego Petteno (flameeyes)
  -- strict thread matches above, loose matches on Subject: below --
2011-10-03 22:45 Mike Frysinger (vapier)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox