Blame SOURCES/emacs-0001-ppc64-fixes-prepatch.patch

1db854
From 7ffe5bd31f9cb53a37f91c5fd1a9b6ce0df51c81 Mon Sep 17 00:00:00 2001
1db854
From: Jan Synacek <jsynacek@redhat.com>
1db854
Date: Tue, 24 May 2016 09:39:42 +0200
1db854
Subject: [PATCH 01/11] ppc64 fixes prepatch
1db854
1db854
---
1db854
 src/unexelf.c | 210 +++++++++++++++++++++++++++++-----------------------------
1db854
 1 file changed, 105 insertions(+), 105 deletions(-)
1db854
1db854
diff --git a/src/unexelf.c b/src/unexelf.c
1db854
index d365940..483da6e 100644
1db854
--- a/src/unexelf.c
1db854
+++ b/src/unexelf.c
1db854
@@ -1,4 +1,4 @@
1db854
-/* Copyright (C) 1985-1988, 1990, 1992, 1999-2013 Free Software
1db854
+/* Copyright (C) 1985-1988, 1990, 1992, 1999-2015 Free Software
1db854
    Foundation, Inc.
1db854
 
1db854
 This file is part of GNU Emacs.
1db854
@@ -386,18 +386,19 @@ temacs:
1db854
    Instead we read the whole file, modify it, and write it out.  */
1db854
 
1db854
 #include <config.h>
1db854
-#include <unexec.h>
1db854
+#include "unexec.h"
1db854
+#include "lisp.h"
1db854
 
1db854
-extern void fatal (const char *msgid, ...);
1db854
-
1db854
-#include <sys/types.h>
1db854
+#include <errno.h>
1db854
+#include <fcntl.h>
1db854
+#include <limits.h>
1db854
+#include <memory.h>
1db854
 #include <stdint.h>
1db854
 #include <stdio.h>
1db854
 #include <sys/stat.h>
1db854
-#include <memory.h>
1db854
-#include <errno.h>
1db854
+#include <sys/types.h>
1db854
 #include <unistd.h>
1db854
-#include <fcntl.h>
1db854
+
1db854
 #if !defined (__NetBSD__) && !defined (__OpenBSD__)
1db854
 #include <elf.h>
1db854
 #endif /* not __NetBSD__ and not __OpenBSD__ */
1db854
@@ -519,6 +520,18 @@ typedef struct {
1db854
 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
1db854
 #endif
1db854
 
1db854
+/* The code often converts ElfW (Half) values like e_shentsize to ptrdiff_t;
1db854
+   check that this doesn't lose information.  */
1db854
+#include <intprops.h>
1db854
+#include <verify.h>
1db854
+verify ((! TYPE_SIGNED (ElfW (Half))
1db854
+	 || PTRDIFF_MIN <= TYPE_MINIMUM (ElfW (Half)))
1db854
+	&& TYPE_MAXIMUM (ElfW (Half)) <= PTRDIFF_MAX);
1db854
+
1db854
+#ifdef UNEXELF_DEBUG
1db854
+# define DEBUG_LOG(expr) fprintf (stderr, #expr " 0x%jx\n", (uintmax_t) (expr))
1db854
+#endif
1db854
+
1db854
 /* Get the address of a particular section or program header entry,
1db854
  * accounting for the size of the entries.
1db854
  */
1db854
@@ -546,17 +559,21 @@ typedef struct {
1db854
    Apr 23, 1996
1db854
    */
1db854
 
1db854
+static void *
1db854
+entry_address (void *section_h, ptrdiff_t idx, ptrdiff_t entsize)
1db854
+{
1db854
+  char *h = section_h;
1db854
+  return h + idx * entsize;
1db854
+}
1db854
+
1db854
 #define OLD_SECTION_H(n) \
1db854
-     (*(ElfW (Shdr) *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
1db854
+  (*(ElfW (Shdr) *) entry_address (old_section_h, n, old_file_h->e_shentsize))
1db854
 #define NEW_SECTION_H(n) \
1db854
-     (*(ElfW (Shdr) *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
1db854
+  (*(ElfW (Shdr) *) entry_address (new_section_h, n, new_file_h->e_shentsize))
1db854
 #define NEW_PROGRAM_H(n) \
1db854
-     (*(ElfW (Phdr) *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
1db854
+  (*(ElfW (Phdr) *) entry_address (new_program_h, n, new_file_h->e_phentsize))
1db854
 
1db854
-#define PATCH_INDEX(n) \
1db854
-  do { \
1db854
-	 if ((int) (n) >= old_bss_index) \
1db854
-	   (n)++; } while (0)
1db854
+#define PATCH_INDEX(n) ((n) += old_bss_index <= (n))
1db854
 typedef unsigned char byte;
1db854
 
1db854
 /* Round X up to a multiple of Y.  */
1db854
@@ -564,7 +581,7 @@ typedef unsigned char byte;
1db854
 static ElfW (Addr)
1db854
 round_up (ElfW (Addr) x, ElfW (Addr) y)
1db854
 {
1db854
-  int rem = x % y;
1db854
+  ElfW (Addr) rem = x % y;
1db854
   if (rem == 0)
1db854
     return x;
1db854
   return x - rem + y;
1db854
@@ -575,33 +592,28 @@ round_up (ElfW (Addr) x, ElfW (Addr) y)
1db854
    about the file we are looking in.
1db854
 
1db854
    If we don't find the section NAME, that is a fatal error
1db854
-   if NOERROR is 0; we return -1 if NOERROR is nonzero.  */
1db854
+   if NOERROR is false; return -1 if NOERROR is true.  */
1db854
 
1db854
-static int
1db854
+static ptrdiff_t
1db854
 find_section (const char *name, const char *section_names, const char *file_name,
1db854
-	      ElfW (Ehdr) *old_file_h, ElfW (Shdr) *old_section_h, int noerror)
1db854
+	      ElfW (Ehdr) *old_file_h, ElfW (Shdr) *old_section_h,
1db854
+	      bool noerror)
1db854
 {
1db854
-  int idx;
1db854
+  ptrdiff_t idx;
1db854
 
1db854
   for (idx = 1; idx < old_file_h->e_shnum; idx++)
1db854
     {
1db854
-#ifdef DEBUG
1db854
-      fprintf (stderr, "Looking for %s - found %s\n", name,
1db854
-	       section_names + OLD_SECTION_H (idx).sh_name);
1db854
+      char const *found_name = section_names + OLD_SECTION_H (idx).sh_name;
1db854
+#ifdef UNEXELF_DEBUG
1db854
+      fprintf (stderr, "Looking for %s - found %s\n", name, found_name);
1db854
 #endif
1db854
-      if (!strcmp (section_names + OLD_SECTION_H (idx).sh_name,
1db854
-		   name))
1db854
-	break;
1db854
-    }
1db854
-  if (idx == old_file_h->e_shnum)
1db854
-    {
1db854
-      if (noerror)
1db854
-	return -1;
1db854
-      else
1db854
-	fatal ("Can't find %s in %s.\n", name, file_name);
1db854
+      if (strcmp (name, found_name) == 0)
1db854
+	return idx;
1db854
     }
1db854
 
1db854
-  return idx;
1db854
+  if (! noerror)
1db854
+    fatal ("Can't find %s in %s", name, file_name);
1db854
+  return -1;
1db854
 }
1db854
 
1db854
 /* ****************************************************************
1db854
@@ -616,11 +628,9 @@ find_section (const char *name, const char *section_names, const char *file_name
1db854
 void
1db854
 unexec (const char *new_name, const char *old_name)
1db854
 {
1db854
-  int new_file, old_file, new_file_size;
1db854
-
1db854
-#if defined (emacs) || !defined (DEBUG)
1db854
+  int new_file, old_file;
1db854
+  off_t new_file_size;
1db854
   void *new_break;
1db854
-#endif
1db854
 
1db854
   /* Pointers to the base of the image of the two files.  */
1db854
   caddr_t old_base, new_base;
1db854
@@ -647,30 +657,30 @@ unexec (const char *new_name, const char *old_name)
1db854
   ElfW (Off)  old_bss_offset;
1db854
   ElfW (Word) new_data2_incr;
1db854
 
1db854
-  int n, nn;
1db854
-  int old_bss_index, old_sbss_index, old_plt_index;
1db854
-  int old_data_index, new_data2_index;
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
-  int old_mdebug_index;
1db854
+  ptrdiff_t old_mdebug_index;
1db854
 #endif
1db854
   struct stat stat_buf;
1db854
-  int old_file_size;
1db854
+  off_t old_file_size;
1db854
 
1db854
   /* Open the old file, allocate a buffer of the right size, and read
1db854
      in the file contents.  */
1db854
 
1db854
-  old_file = open (old_name, O_RDONLY);
1db854
+  old_file = emacs_open (old_name, O_RDONLY, 0);
1db854
 
1db854
   if (old_file < 0)
1db854
-    fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
1db854
+    fatal ("Can't open %s for reading: %s", old_name, strerror (errno));
1db854
 
1db854
-  if (fstat (old_file, &stat_buf) == -1)
1db854
-    fatal ("Can't fstat (%s): errno %d\n", old_name, errno);
1db854
+  if (fstat (old_file, &stat_buf) != 0)
1db854
+    fatal ("Can't fstat (%s): %s", old_name, strerror (errno));
1db854
 
1db854
 #if MAP_ANON == 0
1db854
-  mmap_fd = open ("/dev/zero", O_RDONLY);
1db854
+  mmap_fd = emacs_open ("/dev/zero", O_RDONLY, 0);
1db854
   if (mmap_fd < 0)
1db854
-    fatal ("Can't open /dev/zero for reading: errno %d\n", errno, 0);
1db854
+    fatal ("Can't open /dev/zero for reading: %s", strerror (errno));
1db854
 #endif
1db854
 
1db854
   /* We cannot use malloc here because that may use sbrk.  If it does,
1db854
@@ -678,13 +688,15 @@ unexec (const char *new_name, const char *old_name)
1db854
      extra careful to use the correct value of sbrk(0) after
1db854
      allocating all buffers in the code below, which we aren't.  */
1db854
   old_file_size = stat_buf.st_size;
1db854
+  if (! (0 <= old_file_size && old_file_size <= SIZE_MAX))
1db854
+    fatal ("File size out of range");
1db854
   old_base = mmap (NULL, old_file_size, PROT_READ | PROT_WRITE,
1db854
 		   MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
1db854
   if (old_base == MAP_FAILED)
1db854
-    fatal ("Can't allocate buffer for %s\n", old_name, 0);
1db854
+    fatal ("Can't allocate buffer for %s: %s", old_name, strerror (errno));
1db854
 
1db854
-  if (read (old_file, old_base, stat_buf.st_size) != stat_buf.st_size)
1db854
-    fatal ("Didn't read all of %s: errno %d\n", old_name, errno);
1db854
+  if (read (old_file, old_base, old_file_size) != old_file_size)
1db854
+    fatal ("Didn't read all of %s: %s", old_name, strerror (errno));
1db854
 
1db854
   /* Get pointers to headers & section names */
1db854
 
1db854
@@ -755,12 +767,8 @@ unexec (const char *new_name, const char *old_name)
1db854
   old_data_index = find_section (".data", old_section_names,
1db854
 				 old_name, old_file_h, old_section_h, 0);
1db854
 
1db854
-#if defined (emacs) || !defined (DEBUG)
1db854
   new_break = sbrk (0);
1db854
   new_bss_addr = (ElfW (Addr)) new_break;
1db854
-#else
1db854
-  new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
1db854
-#endif
1db854
   new_data2_addr = old_bss_addr;
1db854
   new_data2_size = new_bss_addr - old_bss_addr;
1db854
   new_data2_offset = OLD_SECTION_H (old_data_index).sh_offset
1db854
@@ -771,38 +779,38 @@ unexec (const char *new_name, const char *old_name)
1db854
      section) was unaligned.  */
1db854
   new_data2_incr = new_data2_size + (new_data2_offset - old_bss_offset);
1db854
 
1db854
-#ifdef DEBUG
1db854
-  fprintf (stderr, "old_bss_index %d\n", old_bss_index);
1db854
-  fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
1db854
-  fprintf (stderr, "old_bss_size %x\n", old_bss_size);
1db854
-  fprintf (stderr, "old_bss_offset %x\n", old_bss_offset);
1db854
-  fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
1db854
-  fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
1db854
-  fprintf (stderr, "new_data2_size %x\n", new_data2_size);
1db854
-  fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
1db854
-  fprintf (stderr, "new_data2_incr %x\n", new_data2_incr);
1db854
+#ifdef UNEXELF_DEBUG
1db854
+  fprintf (stderr, "old_bss_index %td\n", old_bss_index);
1db854
+  DEBUG_LOG (old_bss_addr);
1db854
+  DEBUG_LOG (old_bss_size);
1db854
+  DEBUG_LOG (old_bss_offset);
1db854
+  DEBUG_LOG (new_bss_addr);
1db854
+  DEBUG_LOG (new_data2_addr);
1db854
+  DEBUG_LOG (new_data2_size);
1db854
+  DEBUG_LOG (new_data2_offset);
1db854
+  DEBUG_LOG (new_data2_incr);
1db854
 #endif
1db854
 
1db854
-  if ((uintptr_t) new_bss_addr < (uintptr_t) old_bss_addr + old_bss_size)
1db854
-    fatal (".bss shrank when undumping???\n", 0, 0);
1db854
+  if (new_bss_addr < old_bss_addr + old_bss_size)
1db854
+    fatal (".bss shrank when undumping");
1db854
 
1db854
   /* Set the output file to the right size.  Allocate a buffer to hold
1db854
      the image of the new file.  Set pointers to various interesting
1db854
-     objects.  stat_buf still has old_file data.  */
1db854
+     objects.  */
1db854
 
1db854
-  new_file = open (new_name, O_RDWR | O_CREAT, 0666);
1db854
+  new_file = emacs_open (new_name, O_RDWR | O_CREAT, 0777);
1db854
   if (new_file < 0)
1db854
-    fatal ("Can't creat (%s): errno %d\n", new_name, errno);
1db854
+    fatal ("Can't creat (%s): %s", new_name, strerror (errno));
1db854
 
1db854
-  new_file_size = stat_buf.st_size + old_file_h->e_shentsize + new_data2_incr;
1db854
+  new_file_size = old_file_size + old_file_h->e_shentsize + new_data2_incr;
1db854
 
1db854
   if (ftruncate (new_file, new_file_size))
1db854
-    fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
1db854
+    fatal ("Can't ftruncate (%s): %s", new_name, strerror (errno));
1db854
 
1db854
   new_base = mmap (NULL, new_file_size, PROT_READ | PROT_WRITE,
1db854
 		   MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
1db854
   if (new_base == MAP_FAILED)
1db854
-    fatal ("Can't allocate buffer for %s\n", old_name, 0);
1db854
+    fatal ("Can't allocate buffer for %s: %s", old_name, strerror (errno));
1db854
 
1db854
   new_file_h = (ElfW (Ehdr) *) new_base;
1db854
   new_program_h = (ElfW (Phdr) *) ((byte *) new_base + old_file_h->e_phoff);
1db854
@@ -825,11 +833,11 @@ unexec (const char *new_name, const char *old_name)
1db854
   new_file_h->e_shoff += new_data2_incr;
1db854
   new_file_h->e_shnum += 1;
1db854
 
1db854
-#ifdef DEBUG
1db854
-  fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
1db854
-  fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
1db854
-  fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
1db854
-  fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
1db854
+#ifdef UNEXELF_DEBUG
1db854
+  DEBUG_LOG (old_file_h->e_shoff);
1db854
+  fprintf (stderr, "Old section count %td\n", (ptrdiff_t) old_file_h->e_shnum);
1db854
+  DEBUG_LOG (new_file_h->e_shoff);
1db854
+  fprintf (stderr, "New section count %td\n", (ptrdiff_t) new_file_h->e_shnum);
1db854
 #endif
1db854
 
1db854
   /* Fix up a new program header.  Extend the writable data segment so
1db854
@@ -839,7 +847,7 @@ unexec (const char *new_name, const char *old_name)
1db854
      to adjust the offset and address of any segment that is above
1db854
      data2, just in case we decide to allow this later.  */
1db854
 
1db854
-  for (n = new_file_h->e_phnum - 1; n >= 0; n--)
1db854
+  for (n = new_file_h->e_phnum; --n >= 0; )
1db854
     {
1db854
       /* Compute maximum of all requirements for alignment of section.  */
1db854
       ElfW (Word) alignment = (NEW_PROGRAM_H (n)).p_align;
1db854
@@ -857,7 +865,7 @@ unexec (const char *new_name, const char *old_name)
1db854
 	  > (old_sbss_index == -1
1db854
 	     ? old_bss_addr
1db854
 	     : round_up (old_bss_addr, alignment)))
1db854
-	  fatal ("Program segment above .bss in %s\n", old_name, 0);
1db854
+	  fatal ("Program segment above .bss in %s", old_name);
1db854
 
1db854
       if (NEW_PROGRAM_H (n).p_type == PT_LOAD
1db854
 	  && (round_up ((NEW_PROGRAM_H (n)).p_vaddr
1db854
@@ -867,7 +875,7 @@ unexec (const char *new_name, const char *old_name)
1db854
 	break;
1db854
     }
1db854
   if (n < 0)
1db854
-    fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0);
1db854
+    fatal ("Couldn't find segment next to .bss in %s", old_name);
1db854
 
1db854
   /* Make sure that the size includes any padding before the old .bss
1db854
      section.  */
1db854
@@ -875,7 +883,7 @@ unexec (const char *new_name, const char *old_name)
1db854
   NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz;
1db854
 
1db854
 #if 0 /* Maybe allow section after data2 - does this ever happen? */
1db854
-  for (n = new_file_h->e_phnum - 1; n >= 0; n--)
1db854
+  for (n = new_file_h->e_phnum; --n >= 0; )
1db854
     {
1db854
       if (NEW_PROGRAM_H (n).p_vaddr
1db854
 	  && NEW_PROGRAM_H (n).p_vaddr >= new_data2_addr)
1db854
@@ -894,7 +902,7 @@ unexec (const char *new_name, const char *old_name)
1db854
 
1db854
   /* Walk through all section headers, insert the new data2 section right
1db854
      before the new bss section. */
1db854
-  for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++)
1db854
+  for (n = 1, nn = 1; n < old_file_h->e_shnum; n++, nn++)
1db854
     {
1db854
       caddr_t src;
1db854
       /* If it is (s)bss section, insert the new data2 section before it.  */
1db854
@@ -1076,8 +1084,9 @@ temacs:
1db854
       if (NEW_SECTION_H (nn).sh_type == SHT_MIPS_DEBUG
1db854
 	  && old_mdebug_index != -1)
1db854
 	{
1db854
-	  int diff = NEW_SECTION_H (nn).sh_offset
1db854
-		- OLD_SECTION_H (old_mdebug_index).sh_offset;
1db854
+	  ptrdiff_t new_offset = NEW_SECTION_H (nn).sh_offset;
1db854
+	  ptrdiff_t old_offset = OLD_SECTION_H (old_mdebug_index).sh_offset;
1db854
+	  ptrdiff_t diff = new_offset - old_offset;
1db854
 	  HDRR *phdr = (HDRR *)(NEW_SECTION_H (nn).sh_offset + new_base);
1db854
 
1db854
 	  if (diff)
1db854
@@ -1157,7 +1166,7 @@ temacs:
1db854
 	  || NEW_SECTION_H (nn).sh_type == SHT_DYNSYM)
1db854
 	{
1db854
 	  ElfW (Shdr) *spt = &NEW_SECTION_H (nn);
1db854
-	  unsigned int num = spt->sh_size / spt->sh_entsize;
1db854
+	  ptrdiff_t num = spt->sh_size / spt->sh_entsize;
1db854
 	  ElfW (Sym) * sym = (ElfW (Sym) *) (NEW_SECTION_H (nn).sh_offset +
1db854
 					   new_base);
1db854
 	  for (; num--; sym++)
1db854
@@ -1173,7 +1182,7 @@ temacs:
1db854
     }
1db854
 
1db854
   /* Update the symbol values of _edata and _end.  */
1db854
-  for (n = new_file_h->e_shnum - 1; n; n--)
1db854
+  for (n = new_file_h->e_shnum; 0 < --n; )
1db854
     {
1db854
       byte *symnames;
1db854
       ElfW (Sym) *symp, *symendp;
1db854
@@ -1233,7 +1242,7 @@ temacs:
1db854
 
1db854
   /* This loop seeks out relocation sections for the data section, so
1db854
      that it can undo relocations performed by the runtime linker.  */
1db854
-  for (n = new_file_h->e_shnum - 1; n; n--)
1db854
+  for (n = new_file_h->e_shnum; 0 < --n; )
1db854
     {
1db854
       ElfW (Shdr) section = NEW_SECTION_H (n);
1db854
 
1db854
@@ -1293,29 +1302,20 @@ temacs:
1db854
   /* Write out new_file, and free the buffers.  */
1db854
 
1db854
   if (write (new_file, new_base, new_file_size) != new_file_size)
1db854
-    fatal ("Didn't write %d bytes to %s: errno %d\n",
1db854
-	   new_file_size, new_name, errno);
1db854
+    fatal ("Didn't write %lu bytes to %s: %s",
1db854
+	   (unsigned long) new_file_size, new_name, strerror (errno));
1db854
   munmap (old_base, old_file_size);
1db854
   munmap (new_base, new_file_size);
1db854
 
1db854
   /* Close the files and make the new file executable.  */
1db854
 
1db854
 #if MAP_ANON == 0
1db854
-  close (mmap_fd);
1db854
+  emacs_close (mmap_fd);
1db854
 #endif
1db854
 
1db854
-  if (close (old_file))
1db854
-    fatal ("Can't close (%s): errno %d\n", old_name, errno);
1db854
-
1db854
-  if (close (new_file))
1db854
-    fatal ("Can't close (%s): errno %d\n", new_name, errno);
1db854
-
1db854
-  if (stat (new_name, &stat_buf) == -1)
1db854
-    fatal ("Can't stat (%s): errno %d\n", new_name, errno);
1db854
+  if (emacs_close (old_file) != 0)
1db854
+    fatal ("Can't close (%s): %s", old_name, strerror (errno));
1db854
 
1db854
-  n = umask (777);
1db854
-  umask (n);
1db854
-  stat_buf.st_mode |= 0111 & ~n;
1db854
-  if (chmod (new_name, stat_buf.st_mode) == -1)
1db854
-    fatal ("Can't chmod (%s): errno %d\n", new_name, errno);
1db854
+  if (emacs_close (new_file) != 0)
1db854
+    fatal ("Can't close (%s): %s", new_name, strerror (errno));
1db854
 }
1db854
-- 
1db854
2.7.4
1db854