Blame SOURCES/gdb-6.6-bfd-vdso8k.patch

01917d
2007-09-23  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	* elfcode.h (NAME(_bfd_elf,bfd_from_remote_memory)): New variables
01917d
	X_SHDR_SHSTRTAB and I_SHDR_SHSTRTAB.  Fixed the CONTENTS_SIZE trimming
01917d
	check for its aligned size between the last segment and still before
01917d
	the section header end.  Added variables check to cover also the
01917d
	section header string table.
01917d
01917d
--- gdb-7.4.50.20120120-orig/bfd/elfcode.h	2012-02-29 09:17:08.000000000 +0100
01917d
+++ gdb-7.4.50.20120120/bfd/elfcode.h	2012-02-29 10:23:03.000000000 +0100
01917d
@@ -1621,6 +1621,8 @@ NAME(_bfd_elf,bfd_from_remote_memory)
01917d
   Elf_Internal_Ehdr i_ehdr;	/* Elf file header, internal form */
01917d
   Elf_External_Phdr *x_phdrs;
01917d
   Elf_Internal_Phdr *i_phdrs, *last_phdr;
01917d
+  Elf_External_Shdr *x_shdrs;
01917d
+  Elf_Internal_Shdr *i_shdrs;
01917d
   bfd *nbfd;
01917d
   struct bfd_in_memory *bim;
01917d
   int contents_size;
01917d
@@ -1740,24 +1742,46 @@ NAME(_bfd_elf,bfd_from_remote_memory)
01917d
 
01917d
   /* Trim the last segment so we don't bother with zeros in the last page
01917d
      that are off the end of the file.  However, if the extra bit in that
01917d
-     page includes the section headers, keep them.  */
01917d
-  if ((bfd_vma) contents_size > last_phdr->p_offset + last_phdr->p_filesz
01917d
-      && (bfd_vma) contents_size >= (i_ehdr.e_shoff
01917d
-				     + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01917d
+     page includes the section headers os the section header string table,
01917d
+     keep them.  */
01917d
+  if ((bfd_vma) contents_size > last_phdr->p_offset + last_phdr->p_filesz)
01917d
+    contents_size = last_phdr->p_offset + last_phdr->p_filesz;
01917d
+
01917d
+  if ((bfd_vma) contents_size < i_ehdr.e_shoff
01917d
+				+ i_ehdr.e_shnum * i_ehdr.e_shentsize)
01917d
+    contents_size = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
01917d
+
01917d
+  /* Verify also all the sections fit into CONTENTS_SIZE.  */
01917d
+
01917d
+  x_shdrs = bfd_malloc (i_ehdr.e_shnum * (sizeof *x_shdrs + sizeof *i_shdrs));
01917d
+  if (x_shdrs == NULL)
01917d
     {
01917d
-      contents_size = last_phdr->p_offset + last_phdr->p_filesz;
01917d
-      if ((bfd_vma) contents_size < (i_ehdr.e_shoff
01917d
-				     + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01917d
-	contents_size = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
01917d
+      free (x_phdrs);
01917d
+      bfd_set_error (bfd_error_no_memory);
01917d
+      return NULL;
01917d
     }
01917d
+  err = target_read_memory (ehdr_vma + i_ehdr.e_shoff, (bfd_byte *) x_shdrs,
01917d
+			    i_ehdr.e_shnum * sizeof *x_shdrs);
01917d
+  if (err)
01917d
+    i_shdrs = NULL;
01917d
   else
01917d
-    contents_size = last_phdr->p_offset + last_phdr->p_filesz;
01917d
+    {
01917d
+      i_shdrs = (Elf_Internal_Shdr *) &x_shdrs[i_ehdr.e_shnum];
01917d
+      for (i = 0; i < i_ehdr.e_shnum; ++i)
01917d
+	{
01917d
+	  elf_swap_shdr_in (templ, &x_shdrs[i], &i_shdrs[i]);
01917d
+
01917d
+	  if ((bfd_vma) contents_size < i_shdrs[i].sh_offset + i_shdrs[i].sh_size)
01917d
+	    contents_size = i_shdrs[i].sh_offset + i_shdrs[i].sh_size;
01917d
+	}
01917d
+    }
01917d
 
01917d
   /* Now we know the size of the whole image we want read in.  */
01917d
   contents = (bfd_byte *) bfd_zmalloc (contents_size);
01917d
   if (contents == NULL)
01917d
     {
01917d
       free (x_phdrs);
01917d
+      free (x_shdrs);
01917d
       bfd_set_error (bfd_error_no_memory);
01917d
       return NULL;
01917d
     }
01917d
@@ -1776,6 +1800,7 @@ NAME(_bfd_elf,bfd_from_remote_memory)
01917d
 	if (err)
01917d
 	  {
01917d
 	    free (x_phdrs);
01917d
+	    free (x_shdrs);
01917d
 	    free (contents);
01917d
 	    bfd_set_error (bfd_error_system_call);
01917d
 	    errno = err;
01917d
@@ -1784,10 +1809,32 @@ NAME(_bfd_elf,bfd_from_remote_memory)
01917d
       }
01917d
   free (x_phdrs);
01917d
 
01917d
-  /* If the segments visible in memory didn't include the section headers,
01917d
+  if (i_shdrs)
01917d
+    {
01917d
+      memcpy (contents + i_ehdr.e_shoff, x_shdrs,
01917d
+	      i_ehdr.e_shnum * sizeof *x_shdrs);
01917d
+
01917d
+      for (i = 0; i < i_ehdr.e_shnum; ++i)
01917d
+	{
01917d
+	  bfd_vma start = i_shdrs[i].sh_offset;
01917d
+	  bfd_vma end = i_shdrs[i].sh_offset + i_shdrs[i].sh_size;
01917d
+
01917d
+	  if (end > (bfd_vma) contents_size)
01917d
+	    end = contents_size;
01917d
+	  err = target_read_memory (ehdr_vma + start, contents + start,
01917d
+				    end - start);
01917d
+	  if (err)
01917d
+	    {
01917d
+	      i_shdrs = NULL;
01917d
+	      break;
01917d
+	    }
01917d
+	}
01917d
+    }
01917d
+  free (x_shdrs);
01917d
+
01917d
+  /* If the segments readable in memory didn't include the section headers,
01917d
      then clear them from the file header.  */
01917d
-  if ((bfd_vma) contents_size < (i_ehdr.e_shoff
01917d
-				 + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01917d
+  if (i_shdrs == NULL)
01917d
     {
01917d
       memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
01917d
       memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);