5c16b08
--- elfutils/libelf/ChangeLog
5c16b08
+++ elfutils/libelf/ChangeLog
f790d25
@@ -643,10 +643,53 @@
5c16b08
 	If section content hasn't been read yet, do it before looking for the
5c16b08
 	block size.  If no section data present, infer size of section header.
5c16b08
 
5c16b08
+2005-05-14  Jakub Jelinek  <jakub@redhat.com>
5c16b08
+
5c16b08
+	* libelfP.h (INVALID_NDX): Define.
5c16b08
+	* gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
5c16b08
+	* gelf_getlib.c (gelf_getlib): Likewise.
5c16b08
+	* gelf_getmove.c (gelf_getmove): Likewise.
5c16b08
+	* gelf_getrel.c (gelf_getrel): Likewise.
5c16b08
+	* gelf_getrela.c (gelf_getrela): Likewise.
5c16b08
+	* gelf_getsym.c (gelf_getsym): Likewise.
5c16b08
+	* gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
5c16b08
+	* gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
5c16b08
+	* gelf_getversym.c (gelf_getversym): Likewise.
5c16b08
+	* gelf_update_dyn.c (gelf_update_dyn): Likewise.
5c16b08
+	* gelf_update_lib.c (gelf_update_lib): Likewise.
5c16b08
+	* gelf_update_move.c (gelf_update_move): Likewise.
5c16b08
+	* gelf_update_rel.c (gelf_update_rel): Likewise.
5c16b08
+	* gelf_update_rela.c (gelf_update_rela): Likewise.
5c16b08
+	* gelf_update_sym.c (gelf_update_sym): Likewise.
5c16b08
+	* gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
5c16b08
+	* gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
5c16b08
+	* gelf_update_versym.c (gelf_update_versym): Likewise.
5c16b08
+	* elf_newscn.c (elf_newscn): Check for overflow.
5c16b08
+	* elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
5c16b08
+	(__elfw2(LIBELFBITS,updatefile)): Likewise.
5c16b08
+	* elf_begin.c (file_read_elf): Likewise.
5c16b08
+	* elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
5c16b08
+	* elf_getarsym.c (elf_getarsym): Likewise.
5c16b08
+	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
5c16b08
 2005-05-11  Ulrich Drepper  <drepper@redhat.com>
5c16b08
 
5c16b08
 	* elf.h: Update again.
f790d25
 
f790d25
+2005-05-17  Jakub Jelinek  <jakub@redhat.com>
f790d25
+
f790d25
+	* elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
f790d25
+	table fits into object's bounds.
f790d25
+	* elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
f790d25
+	elf->map_address.  Check if first section header fits into object's
f790d25
+	bounds.
f790d25
+	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
f790d25
+	Check if section header table fits into object's bounds.
f790d25
+	* elf_begin.c (get_shnum): Ensure section headers fits into
f790d25
+	object's bounds.
f790d25
+	(file_read_elf): Make sure scncnt is small enough to allocate both
f790d25
+	ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
f790d25
+	tables fit into object's bounds.  Avoid memory leak on failure.
f790d25
+
f790d25
 2005-05-09  Ulrich Drepper  <drepper@redhat.com>
f790d25
 
f790d25
 	* elf.h: Update from glibc.
5c16b08
--- elfutils/libelf/elf32_getphdr.c
5c16b08
+++ elfutils/libelf/elf32_getphdr.c
f790d25
@@ -114,6 +114,16 @@ __elfw2(LIBELFBITS,getphdr_wrlock) (elf)
840723d
 
44874cc
       if (elf->map_address != NULL)
44874cc
 	{
44874cc
+	  /* First see whether the information in the ELF header is
44874cc
+	     valid and it does not ask for too much.  */
44874cc
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
e4d1f5c
+	      || unlikely (elf->maximum_size - ehdr->e_phoff < size))
44874cc
+	    {
44874cc
+	      /* Something is wrong.  */
44874cc
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
44874cc
+	      goto out;
44874cc
+	    }
840723d
+
44874cc
 	  /* All the data is already mapped.  Use it.  */
44874cc
 	  void *file_phdr = ((char *) elf->map_address
44874cc
 			     + elf->start_offset + ehdr->e_phoff);
5c16b08
--- elfutils/libelf/elf32_getshdr.c
5c16b08
+++ elfutils/libelf/elf32_getshdr.c
44874cc
@@ -1,5 +1,5 @@
44874cc
 /* Return section header.
d36848b
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2009 Red Hat, Inc.
d36848b
+   Copyright (C) 1998-2009 Red Hat, Inc.
44874cc
    This file is part of Red Hat elfutils.
44874cc
    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
44874cc
 
5c16b08
@@ -81,7 +81,8 @@ load_shdr_wrlock (Elf_Scn *scn)
44874cc
     goto out;
44874cc
 
44874cc
   size_t shnum;
d36848b
-  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0)
d36848b
+  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
44874cc
+      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
44874cc
     goto out;
44874cc
   size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
44874cc
 
5c16b08
@@ -98,6 +99,16 @@ load_shdr_wrlock (Elf_Scn *scn)
44874cc
 
44874cc
   if (elf->map_address != NULL)
44874cc
     {
44874cc
+      /* First see whether the information in the ELF header is
44874cc
+	 valid and it does not ask for too much.  */
44874cc
+      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
e4d1f5c
+	  || unlikely (elf->maximum_size - ehdr->e_shoff < size))
44874cc
+	{
44874cc
+	  /* Something is wrong.  */
44874cc
+	  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
44874cc
+	  goto free_and_out;
44874cc
+	}
840723d
+
44874cc
       ElfW2(LIBELFBITS,Shdr) *notcvt;
840723d
 
44874cc
       /* All the data is already mapped.  If we could use it
5c16b08
--- elfutils/libelf/elf32_newphdr.c
5c16b08
+++ elfutils/libelf/elf32_newphdr.c
8cafad5
@@ -135,6 +135,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
8cafad5
 	   || count == PN_XNUM
44874cc
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
44874cc
     {
44874cc
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
44874cc
+	{
44874cc
+	  result = NULL;
44874cc
+	  goto out;
44874cc
+	}
44874cc
+
44874cc
       /* Allocate a new program header with the appropriate number of
44874cc
 	 elements.  */
44874cc
       result = (ElfW2(LIBELFBITS,Phdr) *)
5c16b08
--- elfutils/libelf/elf32_updatefile.c
5c16b08
+++ elfutils/libelf/elf32_updatefile.c
8cafad5
@@ -223,6 +223,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
44874cc
   /* Write all the sections.  Well, only those which are modified.  */
44874cc
   if (shnum > 0)
44874cc
     {
44874cc
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
44874cc
+ 	return 1;
44874cc
+
44874cc
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
44874cc
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
44874cc
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
8cafad5
@@ -645,6 +648,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
44874cc
   /* Write all the sections.  Well, only those which are modified.  */
44874cc
   if (shnum > 0)
44874cc
     {
44874cc
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
44874cc
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
44874cc
+	return 1;
44874cc
+
44874cc
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
44874cc
 #if EV_NUM != 2
44874cc
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
5c16b08
--- elfutils/libelf/elf_begin.c
5c16b08
+++ elfutils/libelf/elf_begin.c
5c16b08
@@ -165,7 +165,8 @@ get_shnum (void *map_address, unsigned c
840723d
 
44874cc
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
44874cc
 	{
44874cc
-	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
44874cc
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
e4d1f5c
+	      || unlikely (maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr)))
44874cc
 	    /* Cannot read the first section header.  */
44874cc
 	    return 0;
840723d
 
5c16b08
@@ -213,7 +214,8 @@ get_shnum (void *map_address, unsigned c
840723d
 
44874cc
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
840723d
 	{
44874cc
-	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
44874cc
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
44874cc
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
44874cc
 	    /* Cannot read the first section header.  */
44874cc
 	    return 0;
840723d
 
5c16b08
@@ -285,6 +287,15 @@ file_read_elf (int fildes, void *map_add
44874cc
     /* Could not determine the number of sections.  */
44874cc
     return NULL;
840723d
 
44874cc
+  /* Check for too many sections.  */
44874cc
+  if (e_ident[EI_CLASS] == ELFCLASS32)
44874cc
+    {
44874cc
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
44874cc
+	return NULL;
44874cc
+    }
44874cc
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
44874cc
+    return NULL;
840723d
+
8cafad5
   /* We can now allocate the memory.  Even if there are no section headers,
8cafad5
      we allocate space for a zeroth section in case we need it later.  */
8cafad5
   const size_t scnmax = (scncnt ?: (cmd == ELF_C_RDWR || cmd == ELF_C_RDWR_MMAP)
f790d25
@@ -324,6 +335,16 @@ file_read_elf (int fildes, void *map_add
840723d
 	{
44874cc
 	  /* We can use the mmapped memory.  */
44874cc
 	  elf->state.elf32.ehdr = ehdr;
840723d
+
44874cc
+	  if (unlikely (ehdr->e_shoff >= maxsize)
e4d1f5c
+	      || unlikely (maxsize - ehdr->e_shoff
e4d1f5c
+			   < scncnt * sizeof (Elf32_Shdr)))
44874cc
+	    {
44874cc
+	    free_and_out:
44874cc
+	      free (elf);
44874cc
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
44874cc
+	      return NULL;
44874cc
+	    }
44874cc
 	  elf->state.elf32.shdr
44874cc
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
840723d
 
f790d25
@@ -410,6 +431,11 @@ file_read_elf (int fildes, void *map_add
44874cc
 	{
44874cc
 	  /* We can use the mmapped memory.  */
44874cc
 	  elf->state.elf64.ehdr = ehdr;
44874cc
+
44874cc
+	  if (unlikely (ehdr->e_shoff >= maxsize)
44874cc
+	      || unlikely (ehdr->e_shoff
44874cc
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
44874cc
+	    goto free_and_out;
44874cc
 	  elf->state.elf64.shdr
44874cc
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
840723d
 
5c16b08
--- elfutils/libelf/elf_getarsym.c
5c16b08
+++ elfutils/libelf/elf_getarsym.c
44874cc
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
44874cc
       size_t index_size = atol (tmpbuf);
840723d
 
44874cc
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
44874cc
+#if SIZE_MAX <= 4294967295U
44874cc
+	  || n >= SIZE_MAX / sizeof (Elf_Arsym)
44874cc
+#endif
44874cc
 	  || n * sizeof (uint32_t) > index_size)
44874cc
 	{
44874cc
 	  /* This index table cannot be right since it does not fit into
d36848b
--- elfutils/libelf/elf_getshdrstrndx.c
d36848b
+++ elfutils/libelf/elf_getshdrstrndx.c
d36848b
@@ -125,10 +125,25 @@ elf_getshdrstrndx (elf, dst)
44874cc
 	      if (elf->map_address != NULL
44874cc
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
44874cc
 		  && (ALLOW_UNALIGNED
44874cc
-		      || (((size_t) ((char *) elf->map_address + offset))
44874cc
+		      || (((size_t) ((char *) elf->map_address
44874cc
+			   + elf->start_offset + offset))
44874cc
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
e4d1f5c
-		/* We can directly access the memory.  */
e4d1f5c
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
44874cc
+		{
44874cc
+		  /* First see whether the information in the ELF header is
44874cc
+		     valid and it does not ask for too much.  */
e4d1f5c
+		  if (unlikely (elf->maximum_size - offset
e4d1f5c
+				< sizeof (Elf32_Shdr)))
44874cc
+		    {
44874cc
+		      /* Something is wrong.  */
44874cc
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
44874cc
+		      result = -1;
44874cc
+		      goto out;
44874cc
+		    }
44874cc
+
e4d1f5c
+		  /* We can directly access the memory.  */
44874cc
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
44874cc
+					 + offset))->sh_link;
44874cc
+		}
44874cc
 	      else
44874cc
 		{
44874cc
 		  /* We avoid reading in all the section headers.  Just read
d36848b
@@ -163,10 +178,25 @@ elf_getshdrstrndx (elf, dst)
44874cc
 	      if (elf->map_address != NULL
44874cc
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
44874cc
 		  && (ALLOW_UNALIGNED
44874cc
-		      || (((size_t) ((char *) elf->map_address + offset))
44874cc
+		      || (((size_t) ((char *) elf->map_address
44874cc
+			   + elf->start_offset + offset))
44874cc
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
e4d1f5c
-		/* We can directly access the memory.  */
e4d1f5c
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
44874cc
+		{
44874cc
+		  /* First see whether the information in the ELF header is
44874cc
+		     valid and it does not ask for too much.  */
e4d1f5c
+		  if (unlikely (elf->maximum_size - offset
e4d1f5c
+				< sizeof (Elf64_Shdr)))
44874cc
+		    {
44874cc
+		      /* Something is wrong.  */
44874cc
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
44874cc
+		      result = -1;
44874cc
+		      goto out;
44874cc
+		    }
44874cc
+
e4d1f5c
+		  /* We can directly access the memory.  */
e4d1f5c
+		  num = ((Elf64_Shdr *) (elf->map_address + elf->start_offset
e4d1f5c
+					 + offset))->sh_link;
44874cc
+		}
44874cc
 	      else
44874cc
 		{
44874cc
 		  /* We avoid reading in all the section headers.  Just read
5c16b08
--- elfutils/libelf/elf_newscn.c
5c16b08
+++ elfutils/libelf/elf_newscn.c
44874cc
@@ -104,10 +104,18 @@ elf_newscn (elf)
44874cc
   else
840723d
     {
44874cc
       /* We must allocate a new element.  */
44874cc
-      Elf_ScnList *newp;
44874cc
+      Elf_ScnList *newp = NULL;
840723d
 
44874cc
       assert (elf->state.elf.scnincr > 0);
840723d
 
44874cc
+      if (
44874cc
+#if SIZE_MAX <= 4294967295U
44874cc
+	  likely (elf->state.elf.scnincr
44874cc
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
44874cc
+#else
44874cc
+	  1
44874cc
+#endif
44874cc
+	  )
44874cc
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
44874cc
 				     + ((elf->state.elf.scnincr *= 2)
44874cc
 					* sizeof (Elf_Scn)), 1);
5c16b08
--- elfutils/libelf/gelf_getdyn.c
5c16b08
+++ elfutils/libelf/gelf_getdyn.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get information from dynamic table at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
e4d1f5c
@@ -93,7 +93,7 @@ gelf_getdyn (data, ndx, dst)
44874cc
 	 table entries has to be adopted.  The user better has provided
44874cc
 	 a buffer where we can store the information.  While copying the
44874cc
 	 data we are converting the format.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -114,7 +114,7 @@ gelf_getdyn (data, ndx, dst)
840723d
 
44874cc
       /* The data is already in the correct form.  Just make sure the
44874cc
 	 index is OK.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, GElf_Dyn, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_getlib.c
5c16b08
+++ elfutils/libelf/gelf_getlib.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get library from table at the given index.
e4d1f5c
-   Copyright (C) 2004 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2004-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2004.
e4d1f5c
 
e4d1f5c
@@ -86,7 +86,7 @@ gelf_getlib (data, ndx, dst)
44874cc
   /* The data is already in the correct form.  Just make sure the
44874cc
      index is OK.  */
44874cc
   GElf_Lib *result = NULL;
44874cc
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Lib, data))
44874cc
     __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
   else
840723d
     {
5c16b08
--- elfutils/libelf/gelf_getmove.c
5c16b08
+++ elfutils/libelf/gelf_getmove.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get move structure at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
e4d1f5c
@@ -83,7 +83,7 @@ gelf_getmove (data, ndx, dst)
840723d
 
44874cc
   /* The data is already in the correct form.  Just make sure the
44874cc
      index is OK.  */
44874cc
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Move, data))
840723d
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
       goto out;
5c16b08
--- elfutils/libelf/gelf_getrela.c
5c16b08
+++ elfutils/libelf/gelf_getrela.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get RELA relocation information at given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
44874cc
   if (data_scn == NULL)
44874cc
     return NULL;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return NULL;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
e4d1f5c
@@ -93,7 +87,7 @@ gelf_getrela (data, ndx, dst)
44874cc
   if (scn->elf->class == ELFCLASS32)
44874cc
     {
44874cc
       /* We have to convert the data.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  result = NULL;
e4d1f5c
@@ -114,7 +108,7 @@ gelf_getrela (data, ndx, dst)
44874cc
     {
44874cc
       /* Simply copy the data after we made sure we are actually getting
44874cc
 	 correct data.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  result = NULL;
5c16b08
--- elfutils/libelf/gelf_getrel.c
5c16b08
+++ elfutils/libelf/gelf_getrel.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get REL relocation information at given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
44874cc
   if (data_scn == NULL)
44874cc
     return NULL;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return NULL;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
e4d1f5c
@@ -93,7 +87,7 @@ gelf_getrel (data, ndx, dst)
44874cc
   if (scn->elf->class == ELFCLASS32)
44874cc
     {
44874cc
       /* We have to convert the data.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  result = NULL;
e4d1f5c
@@ -113,7 +107,7 @@ gelf_getrel (data, ndx, dst)
44874cc
     {
44874cc
       /* Simply copy the data after we made sure we are actually getting
44874cc
 	 correct data.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  result = NULL;
5c16b08
--- elfutils/libelf/gelf_getsym.c
5c16b08
+++ elfutils/libelf/gelf_getsym.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get symbol information from symbol table at the given index.
e4d1f5c
-   Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 1999-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 1999.
e4d1f5c
 
e4d1f5c
@@ -90,7 +90,7 @@ gelf_getsym (data, ndx, dst)
44874cc
 	 table entries has to be adopted.  The user better has provided
44874cc
 	 a buffer where we can store the information.  While copying the
44874cc
 	 data we are converting the format.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Sym, data))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -119,7 +119,7 @@ gelf_getsym (data, ndx, dst)
6b412c1
 
44874cc
       /* The data is already in the correct form.  Just make sure the
44874cc
 	 index is OK.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, GElf_Sym, data))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_getsyminfo.c
5c16b08
+++ elfutils/libelf/gelf_getsyminfo.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get additional symbol information from symbol table at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
e4d1f5c
@@ -84,7 +84,7 @@ gelf_getsyminfo (data, ndx, dst)
bf5b25b
 
44874cc
   /* The data is already in the correct form.  Just make sure the
44874cc
      index is OK.  */
44874cc
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Syminfo, data))
bf5b25b
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
       goto out;
5c16b08
--- elfutils/libelf/gelf_getsymshndx.c
5c16b08
+++ elfutils/libelf/gelf_getsymshndx.c
e4d1f5c
@@ -1,6 +1,6 @@
e4d1f5c
 /* Get symbol information and separate section index from symbol table
e4d1f5c
    at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
e4d1f5c
@@ -90,7 +90,7 @@ gelf_getsymshndx (symdata, shndxdata, nd
44874cc
      section index table.  */
44874cc
   if (likely (shndxdata_scn != NULL))
44874cc
     {
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Word, &shndxdata_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -110,7 +110,7 @@ gelf_getsymshndx (symdata, shndxdata, nd
44874cc
 	 table entries has to be adopted.  The user better has provided
44874cc
 	 a buffer where we can store the information.  While copying the
44874cc
 	 data we are converting the format.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Sym, symdata))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -139,7 +139,7 @@ gelf_getsymshndx (symdata, shndxdata, nd
bf5b25b
 
44874cc
       /* The data is already in the correct form.  Just make sure the
44874cc
 	 index is OK.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, GElf_Sym, symdata))
bf5b25b
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_getversym.c
5c16b08
+++ elfutils/libelf/gelf_getversym.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Get symbol version information at the given index.
e4d1f5c
-   Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 1999-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 1999.
e4d1f5c
 
e4d1f5c
@@ -92,7 +92,7 @@ gelf_getversym (data, ndx, dst)
bf5b25b
 
44874cc
   /* The data is already in the correct form.  Just make sure the
44874cc
      index is OK.  */
44874cc
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Versym, data))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
       result = NULL;
5c16b08
--- elfutils/libelf/gelf_update_dyn.c
5c16b08
+++ elfutils/libelf/gelf_update_dyn.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update information in dynamic table at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
44874cc
   if (data == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -102,7 +96,7 @@ gelf_update_dyn (data, ndx, src)
6b412c1
 	}
bf5b25b
 
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d))
bf5b25b
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -116,7 +110,7 @@ gelf_update_dyn (data, ndx, src)
44874cc
   else
44874cc
     {
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Dyn, &data_scn->d))
bf5b25b
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_update_lib.c
5c16b08
+++ elfutils/libelf/gelf_update_lib.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update library in table at the given index.
e4d1f5c
-   Copyright (C) 2004 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2004-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2004.
e4d1f5c
 
44874cc
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
44874cc
   if (data == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
44874cc
     {
e4d1f5c
@@ -87,7 +81,7 @@ gelf_update_lib (data, ndx, src)
bf5b25b
 
44874cc
   /* Check whether we have to resize the data buffer.  */
44874cc
   int result = 0;
44874cc
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, Elf64_Lib, &data_scn->d))
44874cc
     __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
   else
44874cc
     {
5c16b08
--- elfutils/libelf/gelf_update_move.c
5c16b08
+++ elfutils/libelf/gelf_update_move.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update move structure at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
e4d1f5c
@@ -75,8 +75,7 @@ gelf_update_move (data, ndx, src)
44874cc
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
bf5b25b
 
44874cc
   /* Check whether we have to resize the data buffer.  */
44874cc
-  if (unlikely (ndx < 0)
e4d1f5c
-      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Move, &data_scn->d))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
e4d1f5c
       return 0;
5c16b08
--- elfutils/libelf/gelf_update_rela.c
5c16b08
+++ elfutils/libelf/gelf_update_rela.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update RELA relocation information at given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
44874cc
   if (dst == NULL)
44874cc
     return 0;
44874cc
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -101,7 +95,7 @@ gelf_update_rela (Elf_Data *dst, int ndx
44874cc
 	}
44874cc
 
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -117,7 +111,7 @@ gelf_update_rela (Elf_Data *dst, int ndx
44874cc
   else
44874cc
     {
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_update_rel.c
5c16b08
+++ elfutils/libelf/gelf_update_rel.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update REL relocation information at given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
44874cc
   if (dst == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -99,7 +93,7 @@ gelf_update_rel (Elf_Data *dst, int ndx,
44874cc
 	}
bf5b25b
 
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -114,7 +108,7 @@ gelf_update_rel (Elf_Data *dst, int ndx,
44874cc
   else
44874cc
     {
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_update_sym.c
5c16b08
+++ elfutils/libelf/gelf_update_sym.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update symbol information in symbol table at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
44874cc
   if (data == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -102,7 +96,7 @@ gelf_update_sym (data, ndx, src)
44874cc
 	}
bf5b25b
 
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Sym, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -125,7 +119,7 @@ gelf_update_sym (data, ndx, src)
44874cc
   else
44874cc
     {
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Sym, &data_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_update_syminfo.c
5c16b08
+++ elfutils/libelf/gelf_update_syminfo.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update additional symbol information in symbol table at the given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
44874cc
   if (data == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -93,7 +87,7 @@ gelf_update_syminfo (data, ndx, src)
44874cc
   rwlock_wrlock (scn->elf->lock);
bf5b25b
 
44874cc
   /* Check whether we have to resize the data buffer.  */
44874cc
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Syminfo, &data_scn->d))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
       goto out;
5c16b08
--- elfutils/libelf/gelf_update_symshndx.c
5c16b08
+++ elfutils/libelf/gelf_update_symshndx.c
e4d1f5c
@@ -1,6 +1,6 @@
e4d1f5c
 /* Update symbol information and section index in symbol table at the
e4d1f5c
    given index.
e4d1f5c
-   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2000-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
e4d1f5c
 
44874cc
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
44874cc
   if (symdata == NULL)
44874cc
     return 0;
bf5b25b
 
44874cc
-  if (unlikely (ndx < 0))
44874cc
-    {
44874cc
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
-      return 0;
44874cc
-    }
44874cc
-
44874cc
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
44874cc
     {
44874cc
       /* The type of the data better should match.  */
e4d1f5c
@@ -128,7 +122,7 @@ gelf_update_symshndx (symdata, shndxdata
44874cc
 	}
bf5b25b
 
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf32_Sym, &symdata_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
e4d1f5c
@@ -151,7 +145,7 @@ gelf_update_symshndx (symdata, shndxdata
44874cc
   else
44874cc
     {
44874cc
       /* Check whether we have to resize the data buffer.  */
44874cc
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
e4d1f5c
+      if (INVALID_NDX (ndx, Elf64_Sym, &symdata_scn->d))
44874cc
 	{
44874cc
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
44874cc
 	  goto out;
5c16b08
--- elfutils/libelf/gelf_update_versym.c
5c16b08
+++ elfutils/libelf/gelf_update_versym.c
e4d1f5c
@@ -1,5 +1,5 @@
e4d1f5c
 /* Update symbol version information.
e4d1f5c
-   Copyright (C) 2001, 2002 Red Hat, Inc.
e4d1f5c
+   Copyright (C) 2001-2009 Red Hat, Inc.
e4d1f5c
    This file is part of Red Hat elfutils.
e4d1f5c
    Written by Ulrich Drepper <drepper@redhat.com>, 2001.
e4d1f5c
 
e4d1f5c
@@ -75,8 +75,7 @@ gelf_update_versym (data, ndx, src)
44874cc
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
bf5b25b
 
44874cc
   /* Check whether we have to resize the data buffer.  */
44874cc
-  if (unlikely (ndx < 0)
e4d1f5c
-      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
e4d1f5c
+  if (INVALID_NDX (ndx, GElf_Versym, &data_scn->d))
44874cc
     {
44874cc
       __libelf_seterrno (ELF_E_INVALID_INDEX);
e4d1f5c
       return 0;
5c16b08
--- elfutils/libelf/libelfP.h
5c16b08
+++ elfutils/libelf/libelfP.h
8cafad5
@@ -608,4 +608,8 @@ extern uint32_t __libelf_crc32 (uint32_t
44874cc
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
44874cc
 #define NOTE_ALIGN(n)	(((n) + 3) & -4U)
bf5b25b
 
e4d1f5c
+/* Convenience macro.  */
e4d1f5c
+#define INVALID_NDX(ndx, type, data) \
e4d1f5c
+  unlikely ((data)->d_size / sizeof (type) <= (unsigned int) (ndx))
44874cc
+
44874cc
 #endif  /* libelfP.h */
5c16b08
--- elfutils/src/ChangeLog
5c16b08
+++ elfutils/src/ChangeLog
f790d25
@@ -1548,6 +1548,16 @@
5c16b08
 	object symbols or symbols with unknown type.
5c16b08
 	(check_rel): Likewise.
5c16b08
 
5c16b08
+2005-06-09  Roland McGrath  <roland@redhat.com>
5c16b08
+
5c16b08
+	* readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
5c16b08
+	(handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
5c16b08
+	(handle_scngrp): Check for bogus sh_info.
5c16b08
+
5c16b08
+	* strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
5c16b08
+	st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
5c16b08
+	Don't use assert on input values, instead bail with "illformed" error.
5c16b08
+
5c16b08
 2005-06-08  Roland McGrath  <roland@redhat.com>
5c16b08
 
5c16b08
 	* readelf.c (print_ops): Add consts.
f790d25
@@ -1593,6 +1603,19 @@
5c16b08
 
5c16b08
 	* readelf.c (dwarf_tag_string): Add new tags.
5c16b08
 
5c16b08
+2005-05-17  Jakub Jelinek  <jakub@redhat.com>
5c16b08
+
5c16b08
+	* elflint.c (check_hash): Don't check entries beyond end of section.
5c16b08
+	(check_note): Don't crash if gelf_rawchunk fails.
5c16b08
+	(section_name): Return <invalid> if gelf_getshdr returns NULL.
5c16b08
+
5c16b08
+2005-05-14  Jakub Jelinek  <jakub@redhat.com>
5c16b08
+
5c16b08
+	* elflint.c (section_name): Return "<invalid>" instead of
5c16b08
+	crashing on invalid section name.
5c16b08
+	(check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
5c16b08
+	check_symtab_shndx, check_hash, check_versym): Robustify.
5c16b08
+
5c16b08
 2005-05-08  Roland McGrath  <roland@redhat.com>
5c16b08
 
5c16b08
 	* strip.c (handle_elf): Don't translate hash and versym data formats,
5c16b08
--- elfutils/src/elflint.c
5c16b08
+++ elfutils/src/elflint.c
ba5f203
@@ -131,6 +131,10 @@ static uint32_t shstrndx;
44874cc
 /* Array to count references in section groups.  */
44874cc
 static int *scnref;
bf5b25b
 
8cafad5
+/* Numbers of sections and program headers.  */
44874cc
+static unsigned int shnum;
8cafad5
+static unsigned int phnum;
44874cc
+
bf5b25b
 
44874cc
 int
44874cc
 main (int argc, char *argv[])
ba5f203
@@ -319,10 +323,19 @@ section_name (Ebl *ebl, int idx)
44874cc
 {
44874cc
   GElf_Shdr shdr_mem;
44874cc
   GElf_Shdr *shdr;
44874cc
+  const char *ret;
44874cc
+
44874cc
+  if ((unsigned int) idx > shnum)
44874cc
+    return "<invalid>";
bf5b25b
 
44874cc
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
44874cc
+  if (shdr == NULL)
44874cc
+    return "<invalid>";
bf5b25b
 
44874cc
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
44874cc
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
44874cc
+  if (ret == NULL)
44874cc
+    return "<invalid>";
44874cc
+  return ret;
44874cc
 }
bf5b25b
 
54edc11
 
ba5f203
@@ -344,11 +357,6 @@ static const int valid_e_machine[] =
44874cc
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
54edc11
 
54edc11
 
8cafad5
-/* Numbers of sections and program headers.  */
44874cc
-static unsigned int shnum;
8cafad5
-static unsigned int phnum;
44874cc
-
44874cc
-
44874cc
 static void
44874cc
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
44874cc
 {
ba5f203
@@ -632,7 +640,8 @@ section [%2d] '%s': symbol table cannot 
44874cc
 	  }
44874cc
       }
54edc11
 
44874cc
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
44874cc
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
44874cc
+  if (shdr->sh_entsize != sh_entsize)
44874cc
     ERROR (gettext ("\
44874cc
 section [%2u] '%s': entry size is does not match ElfXX_Sym\n"),
44874cc
 	   idx, section_name (ebl, idx));
ba5f203
@@ -670,7 +679,7 @@ section [%2d] '%s': XINDEX for zeroth en
44874cc
 	       xndxscnidx, section_name (ebl, xndxscnidx));
44874cc
     }
54edc11
 
44874cc
-  for (size_t cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
44874cc
+  for (size_t cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
44874cc
     {
44874cc
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
44874cc
       if (sym == NULL)
ba5f203
@@ -690,7 +699,8 @@ section [%2d] '%s': symbol %zu: invalid 
44874cc
       else
bf5b25b
 	{
44874cc
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
44874cc
-	  assert (name != NULL);
44874cc
+	  assert (name != NULL
44874cc
+		  || strshdr->sh_type != SHT_STRTAB);
44874cc
 	}
bf5b25b
 
44874cc
       if (sym->st_shndx == SHN_XINDEX)
ba5f203
@@ -1038,9 +1048,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
44874cc
     {
44874cc
       GElf_Shdr rcshdr_mem;
44874cc
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
44874cc
-      assert (rcshdr != NULL);
44874cc
 
44874cc
-      if (rcshdr->sh_type == SHT_DYNAMIC)
44874cc
+      if (rcshdr == NULL)
44874cc
+	break;
44874cc
+
44874cc
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
bf5b25b
 	{
44874cc
 	  /* Found the dynamic section.  Look through it.  */
44874cc
 	  Elf_Data *d = elf_getdata (scn, NULL);
ba5f203
@@ -1050,7 +1062,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
44874cc
 	    {
44874cc
 	      GElf_Dyn dyn_mem;
44874cc
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
44874cc
-	      assert (dyn != NULL);
44874cc
+
44874cc
+	      if (dyn == NULL)
44874cc
+		break;
bf5b25b
 
44874cc
 	      if (dyn->d_tag == DT_RELCOUNT)
44874cc
 		{
ba5f203
@@ -1064,7 +1078,9 @@ section [%2d] '%s': DT_RELCOUNT used for
44874cc
 		      /* Does the number specified number of relative
44874cc
 			 relocations exceed the total number of
44874cc
 			 relocations?  */
44874cc
-		      if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
44874cc
+		      if (shdr->sh_entsize != 0
44874cc
+			  && dyn->d_un.d_val > (shdr->sh_size
44874cc
+						/ shdr->sh_entsize))
44874cc
 			ERROR (gettext ("\
44874cc
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
44874cc
 			       idx, section_name (ebl, idx),
ba5f203
@@ -1224,7 +1240,8 @@ section [%2d] '%s': no relocations for m
44874cc
 	}
44874cc
     }
44874cc
 
44874cc
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
44874cc
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
44874cc
+  if (shdr->sh_entsize != sh_entsize)
44874cc
     ERROR (gettext (reltype == ELF_T_RELA ? "\
44874cc
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
44874cc
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
ba5f203
@@ -1447,7 +1464,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
44874cc
   Elf_Data *symdata = elf_getdata (symscn, NULL);
44874cc
   enum load_state state = state_undecided;
44874cc
 
44874cc
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
44874cc
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
44874cc
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25b
     {
44874cc
       GElf_Rela rela_mem;
44874cc
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
ba5f203
@@ -1497,7 +1515,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
44874cc
   Elf_Data *symdata = elf_getdata (symscn, NULL);
44874cc
   enum load_state state = state_undecided;
44874cc
 
44874cc
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
44874cc
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
44874cc
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25b
     {
44874cc
       GElf_Rel rel_mem;
44874cc
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
ba5f203
@@ -1600,7 +1619,8 @@ section [%2d] '%s': referenced as string
44874cc
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
44874cc
 	   idx, section_name (ebl, idx));
bf5b25b
 
44874cc
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
44874cc
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
44874cc
+  if (shdr->sh_entsize != sh_entsize)
44874cc
     ERROR (gettext ("\
44874cc
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
44874cc
 	   idx, section_name (ebl, idx));
ba5f203
@@ -1610,7 +1630,7 @@ section [%2d] '%s': section entry size d
44874cc
 	   idx, section_name (ebl, idx));
44874cc
 
44874cc
   bool non_null_warned = false;
44874cc
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
44874cc
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25b
     {
44874cc
       GElf_Dyn dyn_mem;
44874cc
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
ba5f203
@@ -1891,6 +1911,8 @@ section [%2d] '%s': entry size does not 
44874cc
 	   idx, section_name (ebl, idx));
44874cc
 
44874cc
   if (symshdr != NULL
44874cc
+      && shdr->sh_entsize
44874cc
+      && symshdr->sh_entsize
44874cc
       && (shdr->sh_size / shdr->sh_entsize
44874cc
 	  < symshdr->sh_size / symshdr->sh_entsize))
44874cc
     ERROR (gettext ("\
ba5f203
@@ -1917,6 +1939,12 @@ section [%2d] '%s': extended section ind
44874cc
     }
44874cc
 
44874cc
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
44874cc
+  if (data == NULL)
44874cc
+    {
44874cc
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
44874cc
+ 	     idx, section_name (ebl, idx));
44874cc
+      return;
44874cc
+    }
44874cc
 
44874cc
   if (*((Elf32_Word *) data->d_buf) != 0)
44874cc
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
ba5f203
@@ -1959,7 +1987,7 @@ section [%2d] '%s': hash table section i
44874cc
 
44874cc
   size_t maxidx = nchain;
44874cc
 
44874cc
-  if (symshdr != NULL)
44874cc
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
e76067d
     {
44874cc
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
44874cc
 
ba5f203
@@ -1970,18 +1998,28 @@ section [%2d] '%s': hash table section i
44874cc
       maxidx = symsize;
44874cc
     }
44874cc
 
44874cc
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
44874cc
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
44874cc
   size_t cnt;
44874cc
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
44874cc
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
44874cc
+    {
44874cc
+      if (buf + cnt >= end)
44874cc
+	break;
44874cc
+      else if (buf[cnt] >= maxidx)
44874cc
       ERROR (gettext ("\
44874cc
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
44874cc
 	     idx, section_name (ebl, idx), cnt - 2);
44874cc
+    }
bf5b25b
 
44874cc
   for (; cnt < 2 + nbucket + nchain; ++cnt)
44874cc
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
44874cc
+    {
44874cc
+      if (buf + cnt >= end)
44874cc
+	break;
44874cc
+      else if (buf[cnt] >= maxidx)
44874cc
       ERROR (gettext ("\
44874cc
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
44874cc
 	     idx, section_name (ebl, idx), cnt - 2 - nbucket);
44874cc
+    }
44874cc
 }
bf5b25b
 
bf5b25b
 
ba5f203
@@ -2011,18 +2049,28 @@ section [%2d] '%s': hash table section i
44874cc
       maxidx = symsize;
44874cc
     }
44874cc
 
44874cc
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
44874cc
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
44874cc
   size_t cnt;
44874cc
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
44874cc
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
44874cc
+    {
44874cc
+      if (buf + cnt >= end)
44874cc
+	break;
44874cc
+      else if (buf[cnt] >= maxidx)
44874cc
       ERROR (gettext ("\
44874cc
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
44874cc
 	     idx, section_name (ebl, idx), cnt - 2);
44874cc
+    }
bf5b25b
 
44874cc
   for (; cnt < 2 + nbucket + nchain; ++cnt)
44874cc
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
44874cc
+    {
44874cc
+      if (buf + cnt >= end)
44874cc
+	break;
44874cc
+      else if (buf[cnt] >= maxidx)
44874cc
       ERROR (gettext ("\
44874cc
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
44874cc
-	     idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
44874cc
+	       idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
44874cc
+    }
44874cc
 }
840723d
 
bf5b25b
 
ba5f203
@@ -2047,7 +2095,7 @@ section [%2d] '%s': bitmask size not pow
44874cc
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
bf5b25b
     {
44874cc
       ERROR (gettext ("\
44874cc
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
44874cc
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
44874cc
 	     idx, section_name (ebl, idx), (long int) shdr->sh_size,
44874cc
 	     (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
44874cc
       return;
ba5f203
@@ -2719,8 +2767,9 @@ section [%2d] '%s' refers in sh_link to 
bf5b25b
 
44874cc
   /* The number of elements in the version symbol table must be the
44874cc
      same as the number of symbols.  */
44874cc
-  if (shdr->sh_size / shdr->sh_entsize
44874cc
-      != symshdr->sh_size / symshdr->sh_entsize)
44874cc
+  if (shdr->sh_entsize && symshdr->sh_entsize
44874cc
+      && (shdr->sh_size / shdr->sh_entsize
44874cc
+	  != symshdr->sh_size / symshdr->sh_entsize))
44874cc
     ERROR (gettext ("\
44874cc
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
44874cc
 	   idx, section_name (ebl, idx),
5c16b08
--- elfutils/src/readelf.c
5c16b08
+++ elfutils/src/readelf.c
f790d25
@@ -1171,6 +1171,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
44874cc
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
bf5b25b
 
44874cc
   GElf_Sym sym_mem;
44874cc
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
840723d
+
44874cc
   printf ((grpref[0] & GRP_COMDAT)
44874cc
 	  ? ngettext ("\
44874cc
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
f790d25
@@ -1183,8 +1185,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
44874cc
 		      data->d_size / sizeof (Elf32_Word) - 1),
44874cc
 	  elf_ndxscn (scn),
44874cc
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
44874cc
-	  elf_strptr (ebl->elf, symshdr->sh_link,
44874cc
-		      gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
44874cc
+	  (sym == NULL ? NULL
44874cc
+	   : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
44874cc
 	  ?: gettext ("<INVALID SYMBOL>"),
44874cc
 	  data->d_size / sizeof (Elf32_Word) - 1);
840723d
 
f790d25
@@ -1335,7 +1337,8 @@ static void
44874cc
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
44874cc
 {
44874cc
   int class = gelf_getclass (ebl->elf);
44874cc
-  GElf_Shdr glink;
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink;
44874cc
   Elf_Data *data;
44874cc
   size_t cnt;
44874cc
   size_t shstrndx;
f790d25
@@ -1350,6 +1353,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
44874cc
     error (EXIT_FAILURE, 0,
44874cc
 	   gettext ("cannot get section header string table index"));
bf5b25b
 
44874cc
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	   elf_ndxscn (scn));
840723d
+
44874cc
   printf (ngettext ("\
44874cc
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
44874cc
 		    "\
f790d25
@@ -1359,9 +1367,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
44874cc
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
44874cc
 	  shdr->sh_offset,
44874cc
 	  (int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
44874cc
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
44874cc
 
44874cc
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f790d25
@@ -1944,6 +1950,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
44874cc
     error (EXIT_FAILURE, 0,
44874cc
 	   gettext ("cannot get section header string table index"));
44874cc
 
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
+				   &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	   elf_ndxscn (scn));
840723d
+
44874cc
   /* Now we can compute the number of entries in the section.  */
44874cc
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
44874cc
 				       ? sizeof (Elf32_Sym)
f790d25
@@ -1954,15 +1967,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
44874cc
 		    nsyms),
44874cc
 	  (unsigned int) elf_ndxscn (scn),
44874cc
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
44874cc
-  GElf_Shdr glink;
44874cc
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
44874cc
 		    " %lu local symbols  String table: [%2u] '%s'\n",
44874cc
 		    shdr->sh_info),
44874cc
 	  (unsigned long int) shdr->sh_info,
44874cc
 	  (unsigned int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
6b412c1
 
44874cc
   fputs_unlocked (class == ELFCLASS32
44874cc
 		  ? gettext ("\
f790d25
@@ -2198,7 +2208,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
44874cc
     error (EXIT_FAILURE, 0,
44874cc
 	   gettext ("cannot get section header string table index"));
bf5b25b
 
44874cc
-  GElf_Shdr glink;
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
+				   &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	   elf_ndxscn (scn));
44874cc
+
44874cc
   printf (ngettext ("\
44874cc
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
44874cc
 		    "\
f790d25
@@ -2209,9 +2225,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
44874cc
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
44874cc
 	  shdr->sh_offset,
44874cc
 	  (unsigned int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
840723d
 
44874cc
   unsigned int offset = 0;
44874cc
   for (int cnt = shdr->sh_info; --cnt >= 0; )
f790d25
@@ -2264,8 +2278,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
44874cc
     error (EXIT_FAILURE, 0,
44874cc
 	   gettext ("cannot get section header string table index"));
44874cc
 
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
+				   &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	   elf_ndxscn (scn));
840723d
+
44874cc
   int class = gelf_getclass (ebl->elf);
44874cc
-  GElf_Shdr glink;
44874cc
   printf (ngettext ("\
44874cc
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
44874cc
 		    "\
f790d25
@@ -2277,9 +2297,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
44874cc
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
44874cc
 	  shdr->sh_offset,
44874cc
 	  (unsigned int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
44874cc
 
44874cc
   unsigned int offset = 0;
44874cc
   for (int cnt = shdr->sh_info; --cnt >= 0; )
f790d25
@@ -2541,8 +2559,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
44874cc
       filename = NULL;
44874cc
     }
44874cc
 
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
+				   &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	   elf_ndxscn (scn));
840723d
+
44874cc
   /* Print the header.  */
44874cc
-  GElf_Shdr glink;
44874cc
   printf (ngettext ("\
44874cc
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
44874cc
 		    "\
f790d25
@@ -2554,9 +2578,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
44874cc
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
44874cc
 	  shdr->sh_offset,
44874cc
 	  (unsigned int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25b
 
44874cc
   /* Now we can finally look at the actual contents of this section.  */
44874cc
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f790d25
@@ -2608,7 +2630,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
44874cc
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
44874cc
     ++counts[lengths[cnt]];
bf5b25b
 
44874cc
-  GElf_Shdr glink;
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
44874cc
+					       shdr->sh_link),
44874cc
+				   &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    {
44874cc
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	     elf_ndxscn (scn));
44874cc
+      return;
44874cc
+    }
44874cc
+
44874cc
   printf (ngettext ("\
44874cc
 \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",
44874cc
 		    "\
f790d25
@@ -2621,9 +2653,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
44874cc
 	  shdr->sh_addr,
44874cc
 	  shdr->sh_offset,
44874cc
 	  (unsigned int) shdr->sh_link,
44874cc
-	  elf_strptr (ebl->elf, shstrndx,
44874cc
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
44874cc
-				    &glink)->sh_name));
44874cc
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
840723d
 
44874cc
   if (extrastr != NULL)
44874cc
     fputs (extrastr, stdout);
f790d25
@@ -4265,6 +4295,16 @@ print_debug_aranges_section (Dwfl_Module
44874cc
       return;
44874cc
     }
44874cc
 
44874cc
+  GElf_Shdr glink_mem;
44874cc
+  GElf_Shdr *glink;
44874cc
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
44874cc
+  if (glink == NULL)
44874cc
+    {
44874cc
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
44874cc
+	     elf_ndxscn (scn));
44874cc
+      return;
44874cc
+    }
44874cc
+
44874cc
   printf (ngettext ("\
5c16b08
 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
44874cc
 		    "\
5c16b08
--- elfutils/src/strip.c
5c16b08
+++ elfutils/src/strip.c
ba5f203
@@ -545,6 +545,11 @@ handle_elf (int fd, Elf *elf, const char
44874cc
       goto fail_close;
44874cc
     }
bf5b25b
 
44874cc
+  if (shstrndx >= shnum)
44874cc
+    goto illformed;
44874cc
+
44874cc
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
44874cc
+
44874cc
   /* Storage for section information.  We leave room for two more
44874cc
      entries since we unconditionally create a section header string
44874cc
      table.  Maybe some weird tool created an ELF file without one.
ba5f203
@@ -566,7 +571,7 @@ handle_elf (int fd, Elf *elf, const char
6b412c1
     {
44874cc
       /* This should always be true (i.e., there should not be any
44874cc
 	 holes in the numbering).  */
44874cc
-      assert (elf_ndxscn (scn) == cnt);
44874cc
+      elf_assert (elf_ndxscn (scn) == cnt);
bf5b25b
 
44874cc
       shdr_info[cnt].scn = scn;
bf5b25b
 
ba5f203
@@ -579,6 +584,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 					shdr_info[cnt].shdr.sh_name);
44874cc
       if (shdr_info[cnt].name == NULL)
6b412c1
 	{
44874cc
+	illformed:
44874cc
 	  error (0, 0, gettext ("illformed file '%s'"), fname);
44874cc
 	  goto fail_close;
44874cc
 	}
ba5f203
@@ -588,6 +594,8 @@ handle_elf (int fd, Elf *elf, const char
bf5b25b
 
44874cc
       /* Remember the shdr.sh_link value.  */
44874cc
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
44874cc
+      if (shdr_info[cnt].old_sh_link >= shnum)
44874cc
+	goto illformed;
840723d
 
44874cc
       /* Sections in files other than relocatable object files which
44874cc
 	 are not loaded can be freely moved by us.  In relocatable
ba5f203
@@ -600,7 +608,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	 appropriate reference.  */
44874cc
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
44874cc
 	{
44874cc
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
44874cc
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
44874cc
 	  shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
840723d
 	}
44874cc
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
ba5f203
@@ -617,7 +625,12 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	  for (inner = 1;
44874cc
 	       inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
44874cc
 	       ++inner)
44874cc
+	    {
44874cc
+	      if (grpref[inner] < shnum)
44874cc
 	    shdr_info[grpref[inner]].group_idx = cnt;
44874cc
+	      else
44874cc
+		goto illformed;
44874cc
+	    }
bf5b25b
 
44874cc
 	  if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
44874cc
 	    /* If the section group contains only one element and this
ba5f203
@@ -628,7 +641,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	}
44874cc
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
840723d
 	{
44874cc
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
44874cc
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
44874cc
 	  shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
44874cc
 	}
bf5b25b
 
ba5f203
@@ -636,7 +649,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	 discarded right away.  */
44874cc
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
840723d
 	{
44874cc
-	  assert (shdr_info[cnt].group_idx != 0);
44874cc
+	  elf_assert (shdr_info[cnt].group_idx != 0);
840723d
 
44874cc
 	  if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
44874cc
 	    {
ba5f203
@@ -711,11 +724,15 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	    {
44874cc
 	      /* If a relocation section is marked as being removed make
44874cc
 		 sure the section it is relocating is removed, too.  */
44874cc
-	      if ((shdr_info[cnt].shdr.sh_type == SHT_REL
44874cc
+	      if (shdr_info[cnt].shdr.sh_type == SHT_REL
44874cc
 		   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
44874cc
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
44874cc
+		{
44874cc
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
44874cc
+		    goto illformed;
44874cc
+		  else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
44874cc
 		shdr_info[cnt].idx = 1;
44874cc
 	    }
44874cc
+	    }
840723d
 
44874cc
 	  if (shdr_info[cnt].idx == 1)
44874cc
 	    {
ba5f203
@@ -742,7 +759,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		  if (shdr_info[cnt].symtab_idx != 0
44874cc
 		      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
44874cc
 		    {
44874cc
-		      assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
44874cc
+		      elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
44874cc
 
44874cc
 		      shdr_info[shdr_info[cnt].symtab_idx].data
44874cc
 			= elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
ba5f203
@@ -782,6 +799,9 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		      else if (scnidx == SHN_XINDEX)
44874cc
 			scnidx = xndx;
44874cc
 
44874cc
+		      if (scnidx >= shnum)
44874cc
+			goto illformed;
44874cc
+
44874cc
 		      if (shdr_info[scnidx].idx == 0)
44874cc
 			/* This symbol table has a real symbol in
44874cc
 			   a discarded section.  So preserve the
ba5f203
@@ -812,12 +832,16 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		}
44874cc
 
44874cc
 	      /* Handle references through sh_info.  */
44874cc
-	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
44874cc
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
44874cc
+	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
44874cc
+		{
44874cc
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
44874cc
+		    goto illformed;
44874cc
+		  else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
44874cc
 		{
44874cc
 		  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
44874cc
 		  changes |= shdr_info[cnt].shdr.sh_info < cnt;
44874cc
 		}
44874cc
+		}
44874cc
 
44874cc
 	      /* Mark the section as investigated.  */
44874cc
 	      shdr_info[cnt].idx = 2;
ba5f203
@@ -956,7 +980,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	  error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
44874cc
 		 elf_errmsg (-1));
44874cc
 
44874cc
-	assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
44874cc
+	elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
44874cc
 
44874cc
 	/* Add this name to the section header string table.  */
44874cc
 	shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
ba5f203
@@ -993,7 +1017,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	error (EXIT_FAILURE, 0,
44874cc
 	       gettext ("while create section header section: %s"),
44874cc
 	       elf_errmsg (-1));
44874cc
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
44874cc
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
44874cc
 
44874cc
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
44874cc
       if (shdr_info[cnt].data == NULL)
ba5f203
@@ -1049,7 +1073,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
     error (EXIT_FAILURE, 0,
44874cc
 	   gettext ("while create section header section: %s"),
44874cc
 	   elf_errmsg (-1));
44874cc
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
44874cc
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
44874cc
 
44874cc
   /* Finalize the string table and fill in the correct indices in the
44874cc
      section headers.  */
ba5f203
@@ -1139,20 +1163,20 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
44874cc
 					     NULL);
840723d
 
44874cc
-		    assert ((versiondata->d_size / sizeof (Elf32_Word))
44874cc
+		    elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
44874cc
 			    >= shdr_info[cnt].data->d_size / elsize);
44874cc
 		  }
840723d
 
44874cc
 		if (shdr_info[cnt].version_idx != 0)
44874cc
 		  {
44874cc
-		    assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
44874cc
+		    elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
44874cc
 		    /* This section has associated version
44874cc
 		       information.  We have to modify that
44874cc
 		       information, too.  */
44874cc
 		    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
44874cc
 					       NULL);
840723d
 
44874cc
-		    assert ((versiondata->d_size / sizeof (GElf_Versym))
44874cc
+		    elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
44874cc
 			    >= shdr_info[cnt].data->d_size / elsize);
44874cc
 		  }
bf5b25b
 
ba5f203
@@ -1207,7 +1231,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		      sec = shdr_info[sym->st_shndx].idx;
44874cc
 		    else
44874cc
 		      {
44874cc
-			assert (shndxdata != NULL);
44874cc
+			elf_assert (shndxdata != NULL);
840723d
 
44874cc
 			sec = shdr_info[xshndx].idx;
44874cc
 		      }
ba5f203
@@ -1228,7 +1252,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 			    nxshndx = sec;
44874cc
 			  }
840723d
 
44874cc
-			assert (sec < SHN_LORESERVE || shndxdata != NULL);
44874cc
+			elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
840723d
 
44874cc
 			if ((inner != destidx || nshndx != sym->st_shndx
44874cc
 			     || (shndxdata != NULL && nxshndx != xshndx))
ba5f203
@@ -1252,7 +1276,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 			     || shdr_info[cnt].debug_data == NULL)
44874cc
 		      /* This is a section symbol for a section which has
44874cc
 			 been removed.  */
44874cc
-		      assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
44874cc
+		      elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
44874cc
 		  }
840723d
 
44874cc
 		if (destidx != inner)
ba5f203
@@ -1439,11 +1463,11 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		  {
44874cc
 		    GElf_Sym sym_mem;
44874cc
 		    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
44874cc
-		    assert (sym != NULL);
44874cc
+		    elf_assert (sym != NULL);
bf5b25b
 
44874cc
 		    const char *name = elf_strptr (elf, strshndx,
44874cc
 						   sym->st_name);
44874cc
-		    assert (name != NULL);
44874cc
+		    elf_assert (name != NULL);
44874cc
 		    size_t hidx = elf_hash (name) % nbucket;
bf5b25b
 
44874cc
 		    if (bucket[hidx] == 0)
ba5f203
@@ -1462,7 +1486,7 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 	    else
44874cc
 	      {
44874cc
 		/* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
44874cc
-		assert (shdr_info[cnt].shdr.sh_entsize
44874cc
+		elf_assert (shdr_info[cnt].shdr.sh_entsize
44874cc
 			== sizeof (Elf64_Xword));
bf5b25b
 
44874cc
 		Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
ba5f203
@@ -1493,11 +1517,11 @@ handle_elf (int fd, Elf *elf, const char
44874cc
 		  {
44874cc
 		    GElf_Sym sym_mem;
44874cc
 		    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
44874cc
-		    assert (sym != NULL);
44874cc
+		    elf_assert (sym != NULL);
bf5b25b
 
44874cc
 		    const char *name = elf_strptr (elf, strshndx,
44874cc
 						   sym->st_name);
44874cc
-		    assert (name != NULL);
44874cc
+		    elf_assert (name != NULL);
44874cc
 		    size_t hidx = elf_hash (name) % nbucket;
44874cc
 
44874cc
 		    if (bucket[hidx] == 0)