Blame SOURCES/emacs-0004-ELF-unexec-Merge-Alpha-and-MIPS-COFF-debug-handling.patch

1db854
From 14e5f5be6a45f6d238a47b117d9dc9e3eb36a380 Mon Sep 17 00:00:00 2001
1db854
From: Alan Modra <amodra@gmail.com>
1db854
Date: Sun, 8 Nov 2015 09:28:59 -0800
1db854
Subject: [PATCH 04/11] ELF unexec: Merge Alpha and MIPS COFF debug handling
1db854
1db854
* unexelf.c (unexec): Merge Alpha and MIPS COFF debug handling.
1db854
Don't find .mdebug section index, find the section in the loop.
1db854
Allow for unlikely possibility that .mdebug is located at sh_offset
1db854
before bss segment, by calculating move from difference in
1db854
sh_offset rather than just assuming new_data2_size.  Simplify
1db854
cbLineOffset handling.
1db854
---
1db854
 src/unexelf.c | 96 ++++++++++++++++++-----------------------------------------
1db854
 1 file changed, 29 insertions(+), 67 deletions(-)
1db854
1db854
diff --git a/src/unexelf.c b/src/unexelf.c
1db854
index d6c6648..0065491 100644
1db854
--- a/src/unexelf.c
1db854
+++ b/src/unexelf.c
1db854
@@ -660,9 +660,6 @@ unexec (const char *new_name, const char *old_name)
1db854
   ptrdiff_t n, nn;
1db854
   ptrdiff_t old_bss_index, old_sbss_index, old_plt_index;
1db854
   ptrdiff_t old_data_index, new_data2_index;
1db854
-#if defined _SYSTYPE_SYSV || defined __sgi
1db854
-  ptrdiff_t old_mdebug_index;
1db854
-#endif
1db854
   struct stat stat_buf;
1db854
   off_t old_file_size;
1db854
 
1db854
@@ -706,13 +703,6 @@ unexec (const char *new_name, const char *old_name)
1db854
   old_section_names = (char *) old_base
1db854
     + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
1db854
 
1db854
-  /* Find the mdebug section, if any.  */
1db854
-
1db854
-#if defined _SYSTYPE_SYSV || defined __sgi
1db854
-  old_mdebug_index = find_section (".mdebug", old_section_names,
1db854
-				   old_name, old_file_h, old_section_h, 1);
1db854
-#endif
1db854
-
1db854
   /* Find the old .bss section.  Figure out parameters of the new
1db854
      data2 and bss sections.  */
1db854
 
1db854
@@ -1055,51 +1045,31 @@ temacs:
1db854
 
1db854
       memcpy (new_shdr->sh_offset + new_base, src, new_shdr->sh_size);
1db854
 
1db854
-#if defined __alpha__ && !defined __OpenBSD__
1db854
-      /* Update Alpha COFF symbol table: */
1db854
-      if (strcmp (old_section_names + old_shdr->sh_name, ".mdebug") == 0)
1db854
-	{
1db854
-	  pHDRR symhdr = (pHDRR) (new_shdr->sh_offset + new_base);
1db854
-
1db854
-	  symhdr->cbLineOffset += new_data2_size;
1db854
-	  symhdr->cbDnOffset += new_data2_size;
1db854
-	  symhdr->cbPdOffset += new_data2_size;
1db854
-	  symhdr->cbSymOffset += new_data2_size;
1db854
-	  symhdr->cbOptOffset += new_data2_size;
1db854
-	  symhdr->cbAuxOffset += new_data2_size;
1db854
-	  symhdr->cbSsOffset += new_data2_size;
1db854
-	  symhdr->cbSsExtOffset += new_data2_size;
1db854
-	  symhdr->cbFdOffset += new_data2_size;
1db854
-	  symhdr->cbRfdOffset += new_data2_size;
1db854
-	  symhdr->cbExtOffset += new_data2_size;
1db854
-	}
1db854
-#endif /* __alpha__ && !__OpenBSD__ */
1db854
-
1db854
-#if defined (_SYSTYPE_SYSV)
1db854
-      if (new_shdr->sh_type == SHT_MIPS_DEBUG
1db854
-	  && old_mdebug_index != -1)
1db854
+#if (defined __alpha__ && !defined __OpenBSD__) || defined _SYSTYPE_SYSV
1db854
+      /* Update Alpha and MIPS COFF debug symbol table.  */
1db854
+      if (strcmp (old_section_names + new_shdr->sh_name, ".mdebug") == 0
1db854
+	  && new_shdr->sh_offset - old_shdr->sh_offset != 0
1db854
+#if defined _SYSTYPE_SYSV
1db854
+	  && new_shdr->sh_type == SHT_MIPS_DEBUG
1db854
+#endif
1db854
+	  )
1db854
 	{
1db854
-	  ptrdiff_t new_offset = new_shdr->sh_offset;
1db854
-	  ptrdiff_t old_offset = OLD_SECTION_H (old_mdebug_index).sh_offset;
1db854
-	  ptrdiff_t diff = new_offset - old_offset;
1db854
+	  ptrdiff_t diff = new_shdr->sh_offset - old_shdr->sh_offset;
1db854
 	  HDRR *phdr = (HDRR *) (new_shdr->sh_offset + new_base);
1db854
 
1db854
-	  if (diff)
1db854
-	    {
1db854
-	      phdr->cbLineOffset += diff;
1db854
-	      phdr->cbDnOffset   += diff;
1db854
-	      phdr->cbPdOffset   += diff;
1db854
-	      phdr->cbSymOffset  += diff;
1db854
-	      phdr->cbOptOffset  += diff;
1db854
-	      phdr->cbAuxOffset  += diff;
1db854
-	      phdr->cbSsOffset   += diff;
1db854
-	      phdr->cbSsExtOffset += diff;
1db854
-	      phdr->cbFdOffset   += diff;
1db854
-	      phdr->cbRfdOffset  += diff;
1db854
-	      phdr->cbExtOffset  += diff;
1db854
-	    }
1db854
+	  phdr->cbLineOffset += diff;
1db854
+	  phdr->cbDnOffset += diff;
1db854
+	  phdr->cbPdOffset += diff;
1db854
+	  phdr->cbSymOffset += diff;
1db854
+	  phdr->cbOptOffset += diff;
1db854
+	  phdr->cbAuxOffset += diff;
1db854
+	  phdr->cbSsOffset += diff;
1db854
+	  phdr->cbSsExtOffset += diff;
1db854
+	  phdr->cbFdOffset += diff;
1db854
+	  phdr->cbRfdOffset += diff;
1db854
+	  phdr->cbExtOffset += diff;
1db854
 	}
1db854
-#endif /* _SYSTYPE_SYSV */
1db854
+#endif /* __alpha__ || _SYSTYPE_SYSV */
1db854
 
1db854
 #if __sgi
1db854
       /* Adjust  the HDRR offsets in .mdebug and copy the
1db854
@@ -1110,7 +1080,8 @@ temacs:
1db854
 	 the ld bug that gets the line table in a hole in the
1db854
 	 elf file rather than in the .mdebug section proper.
1db854
 	 David Anderson. davea@sgi.com  Jan 16,1994.  */
1db854
-      if (n == old_mdebug_index)
1db854
+      if (strcmp (old_section_names + new_shdr->sh_name, ".mdebug") == 0
1db854
+	  && new_shdr->sh_offset - old_shdr->sh_offset != 0)
1db854
 	{
1db854
 #define MDEBUGADJUST(__ct,__fileaddr)		\
1db854
   if (n_phdrr->__ct > 0)			\
1db854
@@ -1120,7 +1091,7 @@ temacs:
1db854
 
1db854
 	  HDRR *o_phdrr = (HDRR *) ((byte *) old_base + old_shdr->sh_offset);
1db854
 	  HDRR *n_phdrr = (HDRR *) ((byte *) new_base + new_shdr->sh_offset);
1db854
-	  unsigned movement = new_data2_size;
1db854
+	  ptrdiff_t movement = new_shdr->sh_offset - old_shdr->sh_offset;
1db854
 
1db854
 	  MDEBUGADJUST (idnMax, cbDnOffset);
1db854
 	  MDEBUGADJUST (ipdMax, cbPdOffset);
1db854
@@ -1136,22 +1107,13 @@ temacs:
1db854
 	     requires special handling.  */
1db854
 	  if (n_phdrr->cbLine > 0)
1db854
 	    {
1db854
+	      n_phdrr->cbLineOffset += movement;
1db854
+
1db854
 	      if (o_phdrr->cbLineOffset > (old_shdr->sh_offset
1db854
 					   + old_shdr->sh_size))
1db854
-		{
1db854
-		  /* line data is in a hole in elf. do special copy and adjust
1db854
-		     for this ld mistake.
1db854
-		     */
1db854
-		  n_phdrr->cbLineOffset += movement;
1db854
-
1db854
-		  memcpy (n_phdrr->cbLineOffset + new_base,
1db854
-			  o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
1db854
-		}
1db854
-	      else
1db854
-		{
1db854
-		  /* somehow line data is in .mdebug as it is supposed to be.  */
1db854
-		  MDEBUGADJUST (cbLine, cbLineOffset);
1db854
-		}
1db854
+		/* If not covered by section, it hasn't yet been copied.  */
1db854
+		memcpy (n_phdrr->cbLineOffset + new_base,
1db854
+			o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
1db854
 	    }
1db854
 	}
1db854
 #endif /* __sgi */
1db854
-- 
1db854
2.7.4
1db854