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