From 3bb74495e45e0e798daae5556497e688b8fc4eef Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Mon, 7 Dec 2015 23:39:54 +0000
Subject: [PATCH 29/39] handle sysroot support for nativesdk-gcc

Being able to build a nativesdk gcc is useful, particularly in cases
where the host compiler may be of an incompatible version (or a 32
bit compiler is needed).

Sadly, building nativesdk-gcc is not straight forward. We install
nativesdk-gcc into a relocatable location and this means that its
library locations can change. "Normal" sysroot support doesn't help
in this case since the values of paths like "libdir" change, not just
base root directory of the system.

In order to handle this we do two things:

a) Add %r into spec file markup which can be used for injected paths
   such as SYSTEMLIBS_DIR (see gcc_multilib_setup()).
b) Add other paths which need relocation into a .gccrelocprefix section
   which the relocation code will notice and adjust automatically.

Upstream-Status: Inappropriate
RP 2015/7/28

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 gcc/cppdefault.c | 50 +++++++++++++++++++++++++++++++++++-------------
 gcc/cppdefault.h |  3 ++-
 gcc/gcc.c        | 20 +++++++++++++------
 3 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/gcc/cppdefault.c b/gcc/cppdefault.c
index e2e187dedaf..2faba2b1d63 100644
--- a/gcc/cppdefault.c
+++ b/gcc/cppdefault.c
@@ -35,6 +35,30 @@
 # undef CROSS_INCLUDE_DIR
 #endif
 
+static char GPLUSPLUS_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_INCLUDE_DIR;
+static char GCC_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GCC_INCLUDE_DIR;
+static char GPLUSPLUS_TOOL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_TOOL_INCLUDE_DIR;
+static char GPLUSPLUS_BACKWARD_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_BACKWARD_INCLUDE_DIR;
+static char STANDARD_STARTFILE_PREFIX_2VAR[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_STARTFILE_PREFIX_2 GCC_INCLUDE_SUBDIR_TARGET;
+#ifdef LOCAL_INCLUDE_DIR
+static char LOCAL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = LOCAL_INCLUDE_DIR;
+#endif
+#ifdef PREFIX_INCLUDE_DIR
+static char PREFIX_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = PREFIX_INCLUDE_DIR;
+#endif
+#ifdef FIXED_INCLUDE_DIR
+static char FIXED_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = FIXED_INCLUDE_DIR;
+#endif
+#ifdef CROSS_INCLUDE_DIR
+static char CROSS_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = CROSS_INCLUDE_DIR;
+#endif
+#ifdef TOOL_INCLUDE_DIR
+static char TOOL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = TOOL_INCLUDE_DIR;
+#endif
+#ifdef NATIVE_SYSTEM_HEADER_DIR
+static char NATIVE_SYSTEM_HEADER_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = NATIVE_SYSTEM_HEADER_DIR;
+#endif
+
 const struct default_include cpp_include_defaults[]
 #ifdef INCLUDE_DEFAULTS
 = INCLUDE_DEFAULTS;
@@ -42,38 +66,38 @@ const struct default_include cpp_include_defaults[]
 = {
 #ifdef GPLUSPLUS_INCLUDE_DIR
     /* Pick up GNU C++ generic include files.  */
-    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1,
+    { GPLUSPLUS_INCLUDE_DIRVAR, "G++", 1, 1,
       GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
 #endif
 #ifdef GPLUSPLUS_TOOL_INCLUDE_DIR
     /* Pick up GNU C++ target-dependent include files.  */
-    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1,
+    { GPLUSPLUS_TOOL_INCLUDE_DIRVAR, "G++", 1, 1,
       GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 1 },
 #endif
 #ifdef GPLUSPLUS_BACKWARD_INCLUDE_DIR
     /* Pick up GNU C++ backward and deprecated include files.  */
-    { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1,
+    { GPLUSPLUS_BACKWARD_INCLUDE_DIRVAR, "G++", 1, 1,
       GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
 #endif
 #ifdef GCC_INCLUDE_DIR
     /* This is the dir for gcc's private headers.  */
-    { GCC_INCLUDE_DIR, "GCC", 0, 0, 0, 0 },
+    { GCC_INCLUDE_DIRVAR, "GCC", 0, 0, 0, 0 },
 #endif
 #ifdef GCC_INCLUDE_SUBDIR_TARGET
     /* This is the dir for gcc's private headers under the specified sysroot.  */
-    { STANDARD_STARTFILE_PREFIX_2 GCC_INCLUDE_SUBDIR_TARGET, "GCC", 0, 0, 1, 0 },
+    { STANDARD_STARTFILE_PREFIX_2VAR, "GCC", 0, 0, 1, 0 },
 #endif
 #ifdef LOCAL_INCLUDE_DIR
     /* /usr/local/include comes before the fixincluded header files.  */
-    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 2 },
-    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 0 },
+    { LOCAL_INCLUDE_DIRVAR, 0, 0, 1, 1, 2 },
+    { LOCAL_INCLUDE_DIRVAR, 0, 0, 1, 1, 0 },
 #endif
 #ifdef PREFIX_INCLUDE_DIR
-    { PREFIX_INCLUDE_DIR, 0, 0, 1, 0, 0 },
+    { PREFIX_INCLUDE_DIRVAR, 0, 0, 1, 0, 0 },
 #endif
 #ifdef FIXED_INCLUDE_DIR
     /* This is the dir for fixincludes.  */
-    { FIXED_INCLUDE_DIR, "GCC", 0, 0, 0,
+    { FIXED_INCLUDE_DIRVAR, "GCC", 0, 0, 0,
       /* A multilib suffix needs adding if different multilibs use
 	 different headers.  */
 #ifdef SYSROOT_HEADERS_SUFFIX_SPEC
@@ -85,16 +109,16 @@ const struct default_include cpp_include_defaults[]
 #endif
 #ifdef CROSS_INCLUDE_DIR
     /* One place the target system's headers might be.  */
-    { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0, 0 },
+    { CROSS_INCLUDE_DIRVAR, "GCC", 0, 0, 0, 0 },
 #endif
 #ifdef TOOL_INCLUDE_DIR
     /* Another place the target system's headers might be.  */
-    { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0, 0 },
+    { TOOL_INCLUDE_DIRVAR, "BINUTILS", 0, 1, 0, 0 },
 #endif
 #ifdef NATIVE_SYSTEM_HEADER_DIR
     /* /usr/include comes dead last.  */
-    { NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 2 },
-    { NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 0 },
+    { NATIVE_SYSTEM_HEADER_DIRVAR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 2 },
+    { NATIVE_SYSTEM_HEADER_DIRVAR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 0 },
 #endif
     { 0, 0, 0, 0, 0, 0 }
   };
diff --git a/gcc/cppdefault.h b/gcc/cppdefault.h
index 4a0186d1cf2..29e5d9da0dc 100644
--- a/gcc/cppdefault.h
+++ b/gcc/cppdefault.h
@@ -33,7 +33,8 @@
 
 struct default_include
 {
-  const char *const fname;	/* The name of the directory.  */
+  const char *fname;     /* The name of the directory.  */
+
   const char *const component;	/* The component containing the directory
 				   (see update_path in prefix.c) */
   const char cplusplus;		/* Only look here if we're compiling C++.  */
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 3fb64d453f1..cd0c7fbe961 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -248,6 +248,8 @@ FILE *report_times_to_file = NULL;
 #endif
 static const char *target_system_root = DEFAULT_TARGET_SYSTEM_ROOT;
 
+static char target_relocatable_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = SYSTEMLIBS_DIR;
+
 /* Nonzero means pass the updated target_system_root to the compiler.  */
 
 static int target_system_root_changed;
@@ -519,6 +521,7 @@ or with constant text in a single argument.
  %G     process LIBGCC_SPEC as a spec.
  %R     Output the concatenation of target_system_root and
         target_sysroot_suffix.
+ %r     Output the base path target_relocatable_prefix
  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
  %C     process CPP_SPEC as a spec.
@@ -1487,10 +1490,10 @@ static const char *gcc_libexec_prefix;
    gcc_exec_prefix is set because, in that case, we know where the
    compiler has been installed, and use paths relative to that
    location instead.  */
-static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
-static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
-static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
-static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
+static char standard_exec_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_EXEC_PREFIX;
+static char standard_libexec_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_LIBEXEC_PREFIX;
+static char standard_bindir_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_BINDIR_PREFIX;
+static char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
 
 /* For native compilers, these are well-known paths containing
    components that may be provided by the system.  For cross
@@ -1498,9 +1501,9 @@ static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
 static const char *md_exec_prefix = MD_EXEC_PREFIX;
 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
-static const char *const standard_startfile_prefix_1
+static char standard_startfile_prefix_1[4096] __attribute__ ((section (".gccrelocprefix")))
   = STANDARD_STARTFILE_PREFIX_1;
-static const char *const standard_startfile_prefix_2
+static char standard_startfile_prefix_2[4096] __attribute__ ((section (".gccrelocprefix")))
   = STANDARD_STARTFILE_PREFIX_2;
 
 /* A relative path to be used in finding the location of tools
@@ -5849,6 +5852,11 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 	      }
 	    break;
 
+          case 'r':
+              obstack_grow (&obstack, target_relocatable_prefix,
+		      strlen (target_relocatable_prefix));
+            break;
+
 	  case 'S':
 	    value = do_spec_1 (startfile_spec, 0, NULL);
 	    if (value != 0)
-- 
2.17.0

