adfca8
From 8c30fba366c6814c7bb1795fdda16265eb48efb0 Mon Sep 17 00:00:00 2001
adfca8
From: Alan Modra <amodra@gmail.com>
adfca8
Date: Sun, 8 Nov 2015 09:29:00 -0800
adfca8
Subject: [PATCH 06/11] ELF unexec: _OBJC_ symbols in bss sections
adfca8
adfca8
This code assumed that there was only one bss section.  Rather than
adfca8
checking for a particular index, check the section type.  Also, handle
adfca8
the possibility that the section was SHT_NOBITS originally and is
adfca8
unchanged, in which case no clearing is needed (and sh_offset isn't
adfca8
necessarily valid, which can lead to a wild memset).
adfca8
adfca8
* unexelf.c (unexec): Properly handle _OBJC_ symbols in bss sections.
adfca8
---
adfca8
 src/unexelf.c | 31 ++++++++++++++++++-------------
adfca8
 1 file changed, 18 insertions(+), 13 deletions(-)
adfca8
adfca8
diff --git a/src/unexelf.c b/src/unexelf.c
adfca8
index 286ba2e..df99f92 100644
adfca8
--- a/src/unexelf.c
adfca8
+++ b/src/unexelf.c
adfca8
@@ -1176,20 +1176,25 @@ temacs:
adfca8
 		       "_OBJC_", sizeof ("_OBJC_") - 1) == 0)
adfca8
 	    {
adfca8
 	      ElfW (Shdr) *new_shdr = &NEW_SECTION_H (symp->st_shndx);
adfca8
-	      ptrdiff_t reladdr = symp->st_value - new_shdr->sh_addr;
adfca8
-	      ptrdiff_t newoff = reladdr + new_shdr->sh_offset;
adfca8
-
adfca8
-	      /* "Unpatch" index.  */
adfca8
-	      nn = symp->st_shndx;
adfca8
-	      if (nn > old_bss_index)
adfca8
-		nn--;
adfca8
-	      if (nn == old_bss_index)
adfca8
-		memset (new_base + newoff, 0, symp->st_size);
adfca8
-	      else
adfca8
+	      if (new_shdr->sh_type != SHT_NOBITS)
adfca8
 		{
adfca8
-		  ElfW (Shdr) *old_shdr = &OLD_SECTION_H (nn);
adfca8
-		  ptrdiff_t oldoff = reladdr + old_shdr->sh_offset;
adfca8
-		  memcpy (new_base + newoff, old_base + oldoff, symp->st_size);
adfca8
+		  ElfW (Shdr) *old_shdr;
adfca8
+		  ptrdiff_t reladdr = symp->st_value - new_shdr->sh_addr;
adfca8
+		  ptrdiff_t newoff = reladdr + new_shdr->sh_offset;
adfca8
+
adfca8
+		  /* "Unpatch" index.  */
adfca8
+		  nn = symp->st_shndx;
adfca8
+		  if (nn > old_bss_index)
adfca8
+		    nn--;
adfca8
+		  old_shdr = &OLD_SECTION_H (nn);
adfca8
+		  if (old_shdr->sh_type == SHT_NOBITS)
adfca8
+		    memset (new_base + newoff, 0, symp->st_size);
adfca8
+		  else
adfca8
+		    {
adfca8
+		      ptrdiff_t oldoff = reladdr + old_shdr->sh_offset;
adfca8
+		      memcpy (new_base + newoff, old_base + oldoff,
adfca8
+			      symp->st_size);
adfca8
+		    }
adfca8
 		}
adfca8
 	    }
adfca8
 #endif
adfca8
-- 
adfca8
2.7.4
adfca8