be6651
diff -rup binutils.orig/bfd/elf64-s390.c binutils-2.27/bfd/elf64-s390.c
be6651
--- binutils.orig/bfd/elf64-s390.c	2017-08-29 13:09:44.135143399 +0100
be6651
+++ binutils-2.27/bfd/elf64-s390.c	2017-08-29 13:10:37.272554164 +0100
be6651
@@ -25,6 +25,7 @@
be6651
 #include "libbfd.h"
be6651
 #include "elf-bfd.h"
be6651
 #include "elf/s390.h"
be6651
+#include "elf-s390.h"
be6651
 
be6651
 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
be6651
    from smaller values.  Start with zero, widen, *then* decrement.  */
be6651
@@ -660,6 +661,9 @@ struct elf_s390_link_hash_table
be6651
 
be6651
   /* Small local sym cache.  */
be6651
   struct sym_cache sym_cache;
be6651
+
be6651
+  /* Options passed from the linker.  */
be6651
+  struct s390_elf_params *params;
be6651
 };
be6651
 
be6651
 /* Get the s390 ELF linker hash table from a link_info structure.  */
be6651
@@ -3883,6 +3887,70 @@ elf64_s390_merge_private_bfd_data (bfd *
be6651
   return TRUE;
be6651
 }
be6651
 
be6651
+/* We may add a PT_S390_PGSTE program header.  */
be6651
+
be6651
+static int
be6651
+elf_s390_additional_program_headers (bfd *abfd ATTRIBUTE_UNUSED,
be6651
+				     struct bfd_link_info *info)
be6651
+{
be6651
+  struct elf_s390_link_hash_table *htab;
be6651
+
be6651
+  htab = elf_s390_hash_table (info);
be6651
+  return htab->params->pgste;
be6651
+}
be6651
+
be6651
+
be6651
+/* Add the PT_S390_PGSTE program header.  */
be6651
+
be6651
+static bfd_boolean
be6651
+elf_s390_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED,
be6651
+			     struct bfd_link_info *info)
be6651
+{
be6651
+  struct elf_s390_link_hash_table *htab;
be6651
+  struct elf_segment_map *m, *pm = NULL;
be6651
+
be6651
+  htab = elf_s390_hash_table (info);
be6651
+  if (!htab->params->pgste)
be6651
+    return TRUE;
be6651
+
be6651
+  /* If there is already a PT_S390_PGSTE header, avoid adding
be6651
+     another.  */
be6651
+  m = elf_seg_map (abfd);
be6651
+  while (m && m->p_type != PT_S390_PGSTE)
be6651
+    {
be6651
+      pm = m;
be6651
+      m = m->next;
be6651
+    }
be6651
+
be6651
+  if (m)
be6651
+    return TRUE;
be6651
+
be6651
+  m = (struct elf_segment_map *)
be6651
+    bfd_zalloc (abfd, sizeof (struct elf_segment_map));
be6651
+  if (m == NULL)
be6651
+    return FALSE;
be6651
+  m->p_type = PT_S390_PGSTE;
be6651
+  m->count = 0;
be6651
+  m->next = NULL;
be6651
+  if (pm)
be6651
+    pm->next = m;
be6651
+
be6651
+  return TRUE;
be6651
+}
be6651
+
be6651
+bfd_boolean
be6651
+bfd_elf_s390_set_options (struct bfd_link_info *info,
be6651
+			  struct s390_elf_params *params)
be6651
+{
be6651
+  struct elf_s390_link_hash_table *htab;
be6651
+
be6651
+  htab = elf_s390_hash_table (info);
be6651
+  htab->params = params;
be6651
+
be6651
+  return TRUE;
be6651
+}
be6651
+
be6651
+
be6651
 /* Why was the hash table entry size definition changed from
be6651
    ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
be6651
    this is the only reason for the s390_elf64_size_info structure.  */
be6651
@@ -3959,6 +4027,8 @@ const struct elf_size_info s390_elf64_si
be6651
 #define elf_backend_plt_sym_val		      elf_s390_plt_sym_val
be6651
 #define elf_backend_add_symbol_hook           elf_s390_add_symbol_hook
be6651
 #define elf_backend_sort_relocs_p             elf_s390_elf_sort_relocs_p
be6651
+#define elf_backend_additional_program_headers elf_s390_additional_program_headers
be6651
+#define elf_backend_modify_segment_map	      elf_s390_modify_segment_map
be6651
 
be6651
 #define bfd_elf64_mkobject		elf_s390_mkobject
be6651
 #define elf_backend_object_p		elf_s390_object_p
be6651
diff -rup binutils.orig/binutils/readelf.c binutils-2.27/binutils/readelf.c
be6651
--- binutils.orig/binutils/readelf.c	2017-08-29 13:09:44.138143366 +0100
be6651
+++ binutils-2.27/binutils/readelf.c	2017-08-29 13:09:53.384040839 +0100
be6651
@@ -3610,6 +3610,16 @@ get_arm_segment_type (unsigned long type
be6651
 }
be6651
 
be6651
 static const char *
be6651
+get_s390_segment_type (unsigned long type)
be6651
+{
be6651
+  switch (type)
be6651
+    {
be6651
+    case PT_S390_PGSTE: return "S390_PGSTE";
be6651
+    default:            return NULL;
be6651
+    }
be6651
+}
be6651
+
be6651
+static const char *
be6651
 get_mips_segment_type (unsigned long type)
be6651
 {
be6651
   switch (type)
be6651
@@ -3755,6 +3765,10 @@ get_segment_type (unsigned long p_type)
be6651
 	    case EM_TI_C6000:
be6651
 	      result = get_tic6x_segment_type (p_type);
be6651
 	      break;
be6651
+	    case EM_S390:
be6651
+	    case EM_S390_OLD:
be6651
+	      result = get_s390_segment_type (p_type);
be6651
+	      break;
be6651
 	    default:
be6651
 	      result = NULL;
be6651
 	      break;
be6651
diff -rup binutils.orig/elfcpp/elfcpp.h binutils-2.27/elfcpp/elfcpp.h
be6651
--- binutils.orig/elfcpp/elfcpp.h	2017-08-29 13:09:44.182142878 +0100
be6651
+++ binutils-2.27/elfcpp/elfcpp.h	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -514,7 +514,9 @@ enum PT
be6651
   // Platform architecture compatibility information
be6651
   PT_AARCH64_ARCHEXT = 0x70000000,
be6651
   // Exception unwind tables
be6651
-  PT_AARCH64_UNWIND = 0x70000001
be6651
+  PT_AARCH64_UNWIND = 0x70000001,
be6651
+  // 4k page table size
be6651
+  PT_S390_PGSTE = 0x70000000,
be6651
 };
be6651
 
be6651
 // The valid bit flags found in the Phdr p_flags field.
be6651
diff -rup binutils.orig/include/elf/s390.h binutils-2.27/include/elf/s390.h
be6651
--- binutils.orig/include/elf/s390.h	2017-08-29 13:09:44.434140083 +0100
be6651
+++ binutils-2.27/include/elf/s390.h	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -37,6 +37,9 @@
be6651
 
be6651
 #define EF_S390_HIGH_GPRS        0x00000001
be6651
 
be6651
+/* Request 4k page table size.  */
be6651
+#define PT_S390_PGSTE (PT_LOPROC + 0)
be6651
+
be6651
 /* Relocation types.  */
be6651
 
be6651
 START_RELOC_NUMBERS (elf_s390_reloc_type)
be6651
diff -rup binutils.orig/ld/emulparams/elf64_s390.sh binutils-2.27/ld/emulparams/elf64_s390.sh
be6651
--- binutils.orig/ld/emulparams/elf64_s390.sh	2017-08-29 13:09:44.441140006 +0100
be6651
+++ binutils-2.27/ld/emulparams/elf64_s390.sh	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -12,6 +12,7 @@ TEMPLATE_NAME=elf32
be6651
 GENERATE_SHLIB_SCRIPT=yes
be6651
 GENERATE_PIE_SCRIPT=yes
be6651
 NO_SMALL_DATA=yes
be6651
+EXTRA_EM_FILE=s390
be6651
 IREL_IN_PLT=
be6651
 
be6651
 # Treat a host that matches the target with the possible exception of "x"
be6651
diff -rup binutils.orig/ld/gen-doc.texi binutils-2.27/ld/gen-doc.texi
be6651
--- binutils.orig/ld/gen-doc.texi	2017-08-29 13:09:44.445139961 +0100
be6651
+++ binutils-2.27/ld/gen-doc.texi	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -21,6 +21,7 @@
be6651
 @set POWERPC
be6651
 @set POWERPC64
be6651
 @set Renesas
be6651
+@set S/390
be6651
 @set SPU
be6651
 @set TICOFF
be6651
 @set WIN32
be6651
diff -rup binutils.orig/ld/ld.texinfo binutils-2.27/ld/ld.texinfo
be6651
--- binutils.orig/ld/ld.texinfo	2017-08-29 13:09:44.445139961 +0100
be6651
+++ binutils-2.27/ld/ld.texinfo	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -34,6 +34,7 @@
be6651
 @set POWERPC
be6651
 @set POWERPC64
be6651
 @set Renesas
be6651
+@set S/390
be6651
 @set SPU
be6651
 @set TICOFF
be6651
 @set WIN32
be6651
@@ -158,6 +159,9 @@ in the section entitled ``GNU Free Docum
be6651
 @ifset POWERPC64
be6651
 * PowerPC64 ELF64::             ld and PowerPC64 64-bit ELF Support
be6651
 @end ifset
be6651
+@ifset S/390
be6651
+* S/390 ELF::			ld and S/390 ELF Support
be6651
+@end ifset
be6651
 @ifset SPU
be6651
 * SPU ELF::			ld and SPU ELF Support
be6651
 @end ifset
be6651
@@ -6380,6 +6384,9 @@ functionality are not listed.
be6651
 @ifset POWERPC64
be6651
 * PowerPC64 ELF64::		@command{ld} and PowerPC64 64-bit ELF Support
be6651
 @end ifset
be6651
+@ifset S/390
be6651
+* S/390 ELF::			@command{ld} and S/390 ELF Support
be6651
+@end ifset
be6651
 @ifset SPU
be6651
 * SPU ELF::			@command{ld} and SPU ELF Support
be6651
 @end ifset
be6651
@@ -7373,6 +7380,30 @@ default behaviour.
be6651
 @end table
be6651
 
be6651
 @ifclear GENERIC
be6651
+@lowersections
be6651
+@end ifclear
be6651
+@end ifset
be6651
+
be6651
+@ifset S/390
be6651
+@ifclear GENERIC
be6651
+@raisesections
be6651
+@end ifclear
be6651
+
be6651
+@node S/390 ELF
be6651
+@section @command{ld} and S/390 ELF Support
be6651
+
be6651
+@cindex S/390 ELF options
be6651
+@table @option
be6651
+
be6651
+@cindex S/390
be6651
+@kindex --s390-pgste
be6651
+@item --s390-pgste
be6651
+This option marks the result file with a @code{PT_S390_PGSTE}
be6651
+segment.  The Linux kernel is supposed to allocate 4k page tables for
be6651
+binaries marked that way.
be6651
+@end table
be6651
+
be6651
+@ifclear GENERIC
be6651
 @lowersections
be6651
 @end ifclear
be6651
 @end ifset
be6651
diff -rup binutils.orig/ld/Makefile.in binutils-2.27/ld/Makefile.in
be6651
--- binutils.orig/ld/Makefile.in	2017-08-29 13:09:44.446139950 +0100
be6651
+++ binutils-2.27/ld/Makefile.in	2017-08-29 13:09:53.385040828 +0100
be6651
@@ -3490,6 +3490,7 @@ eelf64_ia64_vms.c: $(srcdir)/emulparams/
be6651
   $(srcdir)/scripttempl/ia64vms.sc ${GEN_DEPENDS}
be6651
 
be6651
 eelf64_s390.c: $(srcdir)/emulparams/elf64_s390.sh \
be6651
+  $(srcdir)/emultempl/s390.em \
be6651
   $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
be6651
 
be6651
 eelf64_sparc.c: $(srcdir)/emulparams/elf64_sparc.sh \
be6651
be6651
--- /dev/null	2017-08-29 08:03:33.387685165 +0100
be6651
+++ binutils-2.27/bfd/elf-s390.h	2017-08-29 13:19:31.705627899 +0100
be6651
@@ -0,0 +1,29 @@
be6651
+/* S/390-specific support for ELF.
be6651
+   Copyright (C) 2017 Free Software Foundation, Inc.
be6651
+
be6651
+   This file is part of BFD, the Binary File Descriptor library.
be6651
+
be6651
+   This program is free software; you can redistribute it and/or modify
be6651
+   it under the terms of the GNU General Public License as published by
be6651
+   the Free Software Foundation; either version 3 of the License, or
be6651
+   (at your option) any later version.
be6651
+
be6651
+   This program is distributed in the hope that it will be useful,
be6651
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
be6651
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
be6651
+   GNU General Public License for more details.
be6651
+
be6651
+   You should have received a copy of the GNU General Public License
be6651
+   along with this program; if not, write to the Free Software
be6651
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
be6651
+   MA 02110-1301, USA.  */
be6651
+
be6651
+/* Used to pass info between ld and bfd.  */
be6651
+struct s390_elf_params
be6651
+{
be6651
+  /* Tell the kernel to allocate 4k page tables.  */
be6651
+  int pgste;
be6651
+};
be6651
+
be6651
+bfd_boolean bfd_elf_s390_set_options (struct bfd_link_info *info,
be6651
+				      struct s390_elf_params *params);
be6651
--- /dev/null	2017-08-29 08:03:33.387685165 +0100
be6651
+++ binutils-2.27/ld/emultempl/s390.em	2017-08-29 13:26:01.797302217 +0100
be6651
@@ -0,0 +1,64 @@
be6651
+# This shell script emits a C file. -*- C -*-
be6651
+# Copyright (C) 2017 Free Software Foundation, Inc.
be6651
+#
be6651
+# This file is part of the GNU Binutils.
be6651
+#
be6651
+# This program is free software; you can redistribute it and/or modify
be6651
+# it under the terms of the GNU General Public License as published by
be6651
+# the Free Software Foundation; either version 3 of the license, or
be6651
+# (at your option) any later version.
be6651
+#
be6651
+# This program is distributed in the hope that it will be useful,
be6651
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
be6651
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
be6651
+# GNU General Public License for more details.
be6651
+#
be6651
+# You should have received a copy of the GNU General Public License
be6651
+# along with this program; see the file COPYING3. If not,
be6651
+# see <http://www.gnu.org/licenses/>.
be6651
+#
be6651
+
be6651
+# This file is sourced from elf-generic.em, and defines S/390
be6651
+# specific routines.
be6651
+#
be6651
+fragment <
be6651
+
be6651
+#include "ldctor.h"
be6651
+#include "elf-s390.h"
be6651
+
be6651
+static struct s390_elf_params params = { 0 };
be6651
+
be6651
+/* This is a convenient point to tell BFD about target specific flags.
be6651
+   After the output has been created, but before inputs are read.  */
be6651
+static void
be6651
+s390_elf_create_output_section_statements (void)
be6651
+{
be6651
+  if (!bfd_elf_s390_set_options (&link_info, &params))
be6651
+    einfo ("%F%P: can not init BFD: %E\n");
be6651
+}
be6651
+
be6651
+EOF
be6651
+
be6651
+# Define some shell vars to insert bits of code into the standard elf
be6651
+# parse_args and list_options functions.
be6651
+#
be6651
+PARSE_AND_LIST_PROLOGUE='
be6651
+#define OPTION_PGSTE	301
be6651
+'
be6651
+
be6651
+PARSE_AND_LIST_LONGOPTS='
be6651
+  { "s390-pgste", no_argument, NULL, OPTION_PGSTE},
be6651
+'
be6651
+
be6651
+PARSE_AND_LIST_OPTIONS='
be6651
+  fprintf (file, _("  --s390-pgste                Tell the kernel to "
be6651
+		   "allocate 4k page tables\n"));
be6651
+'
be6651
+
be6651
+PARSE_AND_LIST_ARGS_CASES='
be6651
+    case OPTION_PGSTE:
be6651
+      params.pgste = 1;
be6651
+      break;
be6651
+'
be6651
+
be6651
+LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=s390_elf_create_output_section_statements