Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 802930 - sys-devel/gcc-8: powerpcspe TEXTREL patches
Summary: sys-devel/gcc-8: powerpcspe TEXTREL patches
Status: RESOLVED OBSOLETE
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: PPC Linux
: Normal minor (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks:
 
Reported: 2021-07-19 15:17 UTC by Luke McHale
Modified: 2023-08-23 17:36 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
powerpcspe TEXTREL patch (0001-Bring-powerpcspe-STARTFILE_LINUX_SPEC-up-to-date-wit.patch,3.27 KB, patch)
2021-07-19 20:24 UTC, Luke McHale
Details | Diff
powerpcspe march patch (0002-powerpcspe-arch-is-missing-AT_PLATFORM-translation-t.patch,4.66 KB, patch)
2021-07-19 20:24 UTC, Luke McHale
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Luke McHale 2021-07-19 15:17:41 UTC
This report only concerns the powerpcspe variant of the gcc toolchain.  gcc-8 deprecated powerpcspe, removing it thereafter due to lack of a maintainer.  Slyfox may vaguely remember assisting me with discovering and fixing the TEXTREL issue.  Thanks again for your help, slyfox!

First, I would like to humbly request that gcc-8 is kept around a bit longer than usual as it is the last version to support this architecture.

Second, I wanted to forward a couple quality of life patches I've been using locally since incorporating last year sometime.  Both patches have been tested on gcc-8.4 and gcc-8.5 since August.

0001 fixes TEXTREL due to improper linking within the powerpcspe gcc code.  I simply mimicked ppc's handling of *_LINUX_SPEC.

0002 intends to fix -mcpu=native detection.  This was also inspired by how the ppc code handles detection.  However, I don't remember if there were any notable divergences.  This is obviously more of a convenience and potentially more fragile to other powerpcspe variants.

I might be one of the last souls running Freescale e500v2 cores in a modern system, but it has been educational keeping it alive.

Regards,
Luke


###### 0001-Bring-powerpcspe-STARTFILE_LINUX_SPEC-up-to-date-wit.patch

From 4e542386b97b648fd844e6b9d380c193fd662098 Mon Sep 17 00:00:00 2001
From: Luke McHale <luke.mchale@protonmail.com>
Date: Fri, 7 Aug 2020 16:40:11 +0000
Subject: [PATCH] Bring powerpcspe STARTFILE_LINUX_SPEC up to date with the
 newer PIE_SPEC decision tree from reference GNU_USER_TARGET_STARTFILE_SPEC.
 Bring powerpcspe ENDFILE_LINUX_SPEC up to date with the newer PIE_SPEC
 decision tree from reference GNU_USER_TARGET_STARTFILE_SPEC.

Fixes issue where the incorrect start/end crti files are chosen when gcc is compiled with --enable-default-pie.
---
 gcc/config/powerpcspe/sysv4.h | 65 +++++++++++++++++++++++++++--------
 1 file changed, 50 insertions(+), 15 deletions(-)

diff --git a/gcc/config/powerpcspe/sysv4.h b/gcc/config/powerpcspe/sysv4.h
index 4f35274..c3b5643 100644
--- a/gcc/config/powerpcspe/sysv4.h
+++ b/gcc/config/powerpcspe/sysv4.h
@@ -757,24 +757,59 @@ ENDIAN_SELECT(" -mbig", " -mlittle", DEFAULT_ASM_ENDIAN)
 #define CRTOFFLOADEND ""
 #endif
 
-#ifdef HAVE_LD_PIE
-#define	STARTFILE_LINUX_SPEC "\
-%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
-%{mnewlib:ecrti.o%s;:crti.o%s} \
-%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
-" CRTOFFLOADBEGIN
+/* STARTFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_STARTFILE_SPEC
+   but with the mnewlib ecrti.o%s selection substituted for crti.o%s.  */
+#if defined HAVE_LD_PIE
+#define	STARTFILE_LINUX_SPEC \
+  "%{shared:; \
+     pg|p|profile:%{static-pie:grcrt1.o%s;:gcrt1.o%s}; \
+     static:crt1.o%s; \
+     static-pie:rcrt1.o%s; \
+     " PIE_SPEC ":Scrt1.o%s; \
+     :crt1.o%s} \
+   %{mnewlib:ecrti.o%s;:crti.o%s} \
+   %{static:crtbeginT.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
+     :crtbegin.o%s} \
+   %{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_start_preinit.o%s; \
+     fvtable-verify=std:vtv_start.o%s} \
+   " CRTOFFLOADBEGIN
 #else
-#define	STARTFILE_LINUX_SPEC "\
-%{!shared: %{pg|p|profile:gcrt1.o%s;:crt1.o%s}} \
-%{mnewlib:ecrti.o%s;:crti.o%s} \
-%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
-" CRTOFFLOADBEGIN
+#define	STARTFILE_LINUX_SPEC \
+  "%{shared:; \
+     pg|p|profile:gcrt1.o%s; \
+     :crt1.o%s} \
+   %{mnewlib:ecrti.o%s;:crti.o%s} \
+   %{static:crtbeginT.o%s; \
+     shared|pie|static-pie:crtbeginS.o%s; \
+     :crtbegin.o%s} \
+   " CRTOFFLOADBEGIN
 #endif
 
-#define	ENDFILE_LINUX_SPEC "\
-%{shared|pie:crtendS.o%s;:crtend.o%s} \
-%{mnewlib:ecrtn.o%s;:crtn.o%s} \
-" CRTOFFLOADEND
+/* ENDFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_ENDFILE_SPEC
+   but with the mnewlib ecrtn.o%s selection substituted for crtn.o%s.  */
+#if defined HAVE_LD_PIE
+#define ENDFILE_LINUX_SPEC \
+  "%{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_end_preinit.o%s; \
+     fvtable-verify=std:vtv_end.o%s} \
+   %{static:crtend.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
+     :crtend.o%s} \
+   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
+   " CRTOFFLOADEND
+#else
+#define ENDFILE_LINUX_SPEC \
+  "%{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_end_preinit.o%s; \
+     fvtable-verify=std:vtv_end.o%s} \
+   %{static:crtend.o%s; \
+     shared|pie|static-pie:crtendS.o%s; \
+     :crtend.o%s} \
+   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
+   " CRTOFFLOADEND
+#endif
 
 #define LINK_START_LINUX_SPEC ""
 
-- 
2.27.0


###### 0002-Bring-powerpcspe-STARTFILE_LINUX_SPEC-up-to-date-wit.patch

From 5bf03bd8c8a97fb30bc7e146216396bbe9f6a36d Mon Sep 17 00:00:00 2001
From: Luke McHale <luke.mchale@protonmail.com>
Date: Fri, 7 Aug 2020 16:46:16 +0000
Subject: [PATCH] powerpcspe arch is missing AT_PLATFORM translation table
 present in the regular powerpc (rs6000) arch. Fixes -mcpu=native when run on
 e500v2 to correctly drop the ppc from AT_PLATFORM ppc8548 to GCC's canonical
 8548.

---
 gcc/config/powerpcspe/driver-powerpcspe.c | 97 +++++++++++++++++++++--
 1 file changed, 89 insertions(+), 8 deletions(-)

diff --git a/gcc/config/powerpcspe/driver-powerpcspe.c b/gcc/config/powerpcspe/driver-powerpcspe.c
index cf3ef94..bd33865 100644
--- a/gcc/config/powerpcspe/driver-powerpcspe.c
+++ b/gcc/config/powerpcspe/driver-powerpcspe.c
@@ -23,6 +23,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
+#include "diagnostic.h"
+#include "opts.h"
 #include <stdlib.h>
 
 #ifdef _AIX
@@ -38,6 +40,44 @@ along with GCC; see the file COPYING3.  If not see
 # include <sys/sysctl.h>
 #endif
 
+#ifdef __linux__
+/* Canonical GCC cpu name table.  */
+static const char *rs6000_supported_cpu_names[] =
+{
+#define RS6000_CPU(NAME, CPU, FLAGS) NAME,
+#include "powerpcspe-cpus.def"
+#undef RS6000_CPU
+};
+
+/* This table holds a list of cpus where their Linux AT_PLATFORM name differs
+   from their GCC canonical name.  The first column in a row contains the GCC
+   canonical cpu name and the other columns in that row contain AT_PLATFORM
+   names that should be mapped to the canonical name.  */
+
+static const char *linux_cpu_translation_table[][4] = {
+  { "403", "ppc403", NULL },
+  { "405", "ppc405", NULL },
+  { "440", "ppc440", "ppc440gp", NULL },
+  { "476", "ppc470", NULL },
+  { "601", "ppc601", NULL },
+  { "603", "ppc603", NULL },
+  { "604", "ppc604", NULL },
+  { "7400", "ppc7400", NULL },
+  { "7450", "ppc7450", NULL },
+  { "750", "ppc750", NULL },
+  { "823", "ppc823", NULL },
+  { "8540", "ppc8540", NULL },
+  { "8548", "ppc8548", NULL },
+  { "970", "ppc970", NULL },
+  { "cell", "ppc-cell-be", NULL },
+  { "e500mc", "ppce500mc", NULL },
+  { "e5500", "ppce5500", NULL },
+  { "e6500", "ppce6500", NULL },
+  { "power7", "power7+", NULL },
+  { NULL } /* End of table sentinel.  */
+};
+#endif
+
 const char *host_detect_local_cpu (int argc, const char **argv);
 
 #if GCC_VERSION >= 0
@@ -158,14 +198,19 @@ detect_processor_freebsd (void)
 
 #ifdef __linux__
 
-/* Returns AT_PLATFORM if present, otherwise generic PowerPC.  */
+/* Returns the canonical AT_PLATFORM if present, otherwise NULL.  */
 
 static const char *
 elf_platform (void)
 {
-  int fd;
+  /* Used to cache the result we determine below.  */
+  static const char *cpu = NULL;
 
-  fd = open ("/proc/self/auxv", O_RDONLY);
+  /* Use the cached AT_PLATFORM cpu name if we've already determined it.  */
+  if (cpu != NULL)
+    return cpu;
+
+  int fd = open ("/proc/self/auxv", O_RDONLY);
 
   if (fd != -1)
     {
@@ -179,15 +224,51 @@ elf_platform (void)
       if (n > 0)
 	{
 	  for (av = (ElfW(auxv_t) *) buf; av->a_type != AT_NULL; ++av)
-	    switch (av->a_type)
+	    if (av->a_type == AT_PLATFORM)
 	      {
-	      case AT_PLATFORM:
-		return (const char *) av->a_un.a_val;
-
-	      default:
+		/* Cache the result.  */
+		cpu = (const char *) av->a_un.a_val;
 		break;
 	      }
 	}
+
+      /* Verify that CPU is either a valid -mcpu=<cpu> option name, or is a
+	 valid alternative name.  If it is a valid alternative name, then use
+	 the canonical name.  */
+      if (cpu != NULL)
+	{
+	  size_t i, j;
+	  char *s;
+
+	  /* Check if AT_PLATFORM is a GCC canonical cpu name.  */
+	  for (i = 0; i < ARRAY_SIZE (rs6000_supported_cpu_names); i++)
+	    if (!strcmp (cpu, rs6000_supported_cpu_names[i]))
+	      return cpu;
+
+	  /* Check if AT_PLATFORM can be translated to a canonical cpu name.  */
+	  for (i = 0; linux_cpu_translation_table[i][0] != NULL; i++)
+	    {
+	      const char *canonical = linux_cpu_translation_table[i][0];
+	      for (j = 1; linux_cpu_translation_table[i][j] != NULL; j++)
+		if (!strcmp (cpu, linux_cpu_translation_table[i][j]))
+		  {
+		    /* Cache the result.  */
+		    cpu = canonical;
+		    return cpu;
+		  }
+	    }
+
+	  /* The kernel returned an AT_PLATFORM name we do not support.  */
+	  auto_vec <const char *> candidates;
+	  for (i = 0; i < ARRAY_SIZE (rs6000_supported_cpu_names); i++)
+	    candidates.safe_push (rs6000_supported_cpu_names[i]);
+	  candidates_list_and_hint (cpu, s, candidates);
+	  fatal_error (
+	    input_location,
+	    "Unsupported cpu name returned from kernel for -mcpu=native: %s\n"
+	    "Please use an explicit cpu name.  Valid cpu names are: %s",
+	    cpu, s);
+	}
     }
   return NULL;
 }
-- 
2.27.0
Comment 1 Sergei Trofimovich (RETIRED) gentoo-dev 2021-07-19 17:44:13 UTC
The patches look ok. We can apply everything to powerpcspe/ not being afraid of breaking other targets. Can you attach your patches as attachments to the bug? Otherwise I'm afraid I won't win whitespace battle.

I will push then to gcc-patches as is.
Comment 2 Luke McHale 2021-07-19 20:24:10 UTC
Created attachment 725200 [details, diff]
powerpcspe TEXTREL patch
Comment 3 Luke McHale 2021-07-19 20:24:46 UTC
Created attachment 725203 [details, diff]
powerpcspe march patch
Comment 4 Luke McHale 2021-07-19 20:26:14 UTC
(In reply to Sergei Trofimovich from comment #1)
> The patches look ok. We can apply everything to powerpcspe/ not being afraid
> of breaking other targets. Can you attach your patches as attachments to the
> bug? Otherwise I'm afraid I won't win whitespace battle.
> 
> I will push then to gcc-patches as is.

Certainly, thanks!
Comment 5 Larry the Git Cow gentoo-dev 2021-07-19 22:31:29 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/proj/gcc-patches.git/commit/?id=5013b9e50a671edefe6b0ce6c103dbeb621344c3

commit 5013b9e50a671edefe6b0ce6c103dbeb621344c3
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2021-07-19 22:30:28 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2021-07-19 22:30:28 +0000

    8.5.0: apply powerpcspe pie and march patches
    
    Two new patches:
    + 31_all_powerpcspe-pie-crt.patch
    + 32_all_powerpcspe-march-PLATFORM.patch
    
    Fix-by: Luke McHale
    Bug: https://bugs.gentoo.org/802930
    Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>

 8.5.0/gentoo/31_all_powerpcspe-pie-crt.patch       |  95 +++++++++++++
 .../gentoo/32_all_powerpcspe-march-PLATFORM.patch  | 153 +++++++++++++++++++++
 8.5.0/gentoo/README.history                        |   4 +
 3 files changed, 252 insertions(+)
Comment 6 Sergei Trofimovich (RETIRED) gentoo-dev 2021-07-19 22:32:29 UTC
Applied as it against 8.5.0 gcc-patches branch. New patchset should be cut in a few weeks.

Thank you!