blob: 1a65ece7bf10e349e7a7f9e4a7d8436d35add61b [file] [log] [blame]
Andrew Geisslerc182c622020-05-15 14:13:32 -05001From bb1f359e34649516e61305e9748534cce7e0ee70 Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: Khem Raj <raj.khem@gmail.com>
3Date: Mon, 7 Dec 2015 23:39:54 +0000
Andrew Geisslerc182c622020-05-15 14:13:32 -05004Subject: [PATCH] handle sysroot support for nativesdk-gcc
Andrew Geissler82c905d2020-04-13 13:39:40 -05005
6Being able to build a nativesdk gcc is useful, particularly in cases
7where the host compiler may be of an incompatible version (or a 32
8bit compiler is needed).
9
10Sadly, building nativesdk-gcc is not straight forward. We install
11nativesdk-gcc into a relocatable location and this means that its
12library locations can change. "Normal" sysroot support doesn't help
13in this case since the values of paths like "libdir" change, not just
14base root directory of the system.
15
16In order to handle this we do two things:
17
18a) Add %r into spec file markup which can be used for injected paths
19 such as SYSTEMLIBS_DIR (see gcc_multilib_setup()).
20b) Add other paths which need relocation into a .gccrelocprefix section
21 which the relocation code will notice and adjust automatically.
22
23Upstream-Status: Inappropriate
24RP 2015/7/28
25
26Signed-off-by: Khem Raj <raj.khem@gmail.com>
27
28Added PREFIXVAR and EXEC_PREFIXVAR to support runtime relocation. Without
29these as part of the gccrelocprefix the system can't do runtime relocation
30if the executable is moved. (These paths were missed in the original
31implementation.)
32
33Signed-off-by: Mark Hatle <mark.hatle@kernel.crashing.org>
34---
Andrew Geisslerc182c622020-05-15 14:13:32 -050035 gcc/c-family/c-opts.c | 4 +--
36 gcc/cppdefault.c | 63 ++++++++++++++++++++++++++-----------------
37 gcc/cppdefault.h | 13 ++++-----
38 gcc/gcc.c | 20 +++++++++-----
39 gcc/incpath.c | 12 ++++-----
40 gcc/prefix.c | 6 +++--
41 6 files changed, 70 insertions(+), 48 deletions(-)
Andrew Geissler82c905d2020-04-13 13:39:40 -050042
Andrew Geisslerc182c622020-05-15 14:13:32 -050043diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
44index 58ba0948e79..806bbcfb7a5 100644
45--- a/gcc/c-family/c-opts.c
46+++ b/gcc/c-family/c-opts.c
47@@ -1409,8 +1409,8 @@ add_prefixed_path (const char *suffix, incpath_kind chain)
48 size_t prefix_len, suffix_len;
49
50 suffix_len = strlen (suffix);
51- prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
52- prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
53+ prefix = iprefix ? iprefix : GCC_INCLUDE_DIRVAR;
54+ prefix_len = iprefix ? strlen (iprefix) : strlen(GCC_INCLUDE_DIRVAR) - 7;
55
56 path = (char *) xmalloc (prefix_len + suffix_len + 1);
57 memcpy (path, prefix, prefix_len);
58diff --git a/gcc/cppdefault.c b/gcc/cppdefault.c
59index 2f43b88a0c3..6b6be04686c 100644
60--- a/gcc/cppdefault.c
61+++ b/gcc/cppdefault.c
Andrew Geissler82c905d2020-04-13 13:39:40 -050062@@ -35,6 +35,30 @@
63 # undef CROSS_INCLUDE_DIR
64 #endif
65
66+static char GPLUSPLUS_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_INCLUDE_DIR;
67+char GCC_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GCC_INCLUDE_DIR;
68+static char GPLUSPLUS_TOOL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_TOOL_INCLUDE_DIR;
69+static char GPLUSPLUS_BACKWARD_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = GPLUSPLUS_BACKWARD_INCLUDE_DIR;
70+static char STANDARD_STARTFILE_PREFIX_2VAR[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_STARTFILE_PREFIX_2 GCC_INCLUDE_SUBDIR_TARGET;
71+#ifdef LOCAL_INCLUDE_DIR
72+static char LOCAL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = LOCAL_INCLUDE_DIR;
73+#endif
74+#ifdef PREFIX_INCLUDE_DIR
75+static char PREFIX_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = PREFIX_INCLUDE_DIR;
76+#endif
77+#ifdef FIXED_INCLUDE_DIR
78+static char FIXED_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = FIXED_INCLUDE_DIR;
79+#endif
80+#ifdef CROSS_INCLUDE_DIR
81+static char CROSS_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = CROSS_INCLUDE_DIR;
82+#endif
83+#ifdef TOOL_INCLUDE_DIR
84+static char TOOL_INCLUDE_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = TOOL_INCLUDE_DIR;
85+#endif
86+#ifdef NATIVE_SYSTEM_HEADER_DIR
87+static char NATIVE_SYSTEM_HEADER_DIRVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = NATIVE_SYSTEM_HEADER_DIR;
88+#endif
89+
90 const struct default_include cpp_include_defaults[]
91 #ifdef INCLUDE_DEFAULTS
92 = INCLUDE_DEFAULTS;
Andrew Geisslerc182c622020-05-15 14:13:32 -050093@@ -42,38 +66,38 @@ const struct default_include cpp_include_defaults[]
Andrew Geissler82c905d2020-04-13 13:39:40 -050094 = {
95 #ifdef GPLUSPLUS_INCLUDE_DIR
96 /* Pick up GNU C++ generic include files. */
97- { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1,
98+ { GPLUSPLUS_INCLUDE_DIRVAR, "G++", 1, 1,
99 GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
100 #endif
101 #ifdef GPLUSPLUS_TOOL_INCLUDE_DIR
102 /* Pick up GNU C++ target-dependent include files. */
103- { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1,
104+ { GPLUSPLUS_TOOL_INCLUDE_DIRVAR, "G++", 1, 1,
105 GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 1 },
106 #endif
107 #ifdef GPLUSPLUS_BACKWARD_INCLUDE_DIR
108 /* Pick up GNU C++ backward and deprecated include files. */
109- { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1,
110+ { GPLUSPLUS_BACKWARD_INCLUDE_DIRVAR, "G++", 1, 1,
111 GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
112 #endif
113 #ifdef GCC_INCLUDE_DIR
114 /* This is the dir for gcc's private headers. */
115- { GCC_INCLUDE_DIR, "GCC", 0, 0, 0, 0 },
116+ { GCC_INCLUDE_DIRVAR, "GCC", 0, 0, 0, 0 },
117 #endif
118 #ifdef GCC_INCLUDE_SUBDIR_TARGET
119 /* This is the dir for gcc's private headers under the specified sysroot. */
120- { STANDARD_STARTFILE_PREFIX_2 GCC_INCLUDE_SUBDIR_TARGET, "GCC", 0, 0, 1, 0 },
121+ { STANDARD_STARTFILE_PREFIX_2VAR, "GCC", 0, 0, 1, 0 },
122 #endif
123 #ifdef LOCAL_INCLUDE_DIR
124 /* /usr/local/include comes before the fixincluded header files. */
125- { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 2 },
126- { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 0 },
127+ { LOCAL_INCLUDE_DIRVAR, 0, 0, 1, 1, 2 },
128+ { LOCAL_INCLUDE_DIRVAR, 0, 0, 1, 1, 0 },
129 #endif
130 #ifdef PREFIX_INCLUDE_DIR
131- { PREFIX_INCLUDE_DIR, 0, 0, 1, 0, 0 },
132+ { PREFIX_INCLUDE_DIRVAR, 0, 0, 1, 0, 0 },
133 #endif
134 #ifdef FIXED_INCLUDE_DIR
135 /* This is the dir for fixincludes. */
136- { FIXED_INCLUDE_DIR, "GCC", 0, 0, 0,
137+ { FIXED_INCLUDE_DIRVAR, "GCC", 0, 0, 0,
138 /* A multilib suffix needs adding if different multilibs use
139 different headers. */
140 #ifdef SYSROOT_HEADERS_SUFFIX_SPEC
Andrew Geisslerc182c622020-05-15 14:13:32 -0500141@@ -85,33 +109,24 @@ const struct default_include cpp_include_defaults[]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500142 #endif
143 #ifdef CROSS_INCLUDE_DIR
144 /* One place the target system's headers might be. */
145- { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0, 0 },
146+ { CROSS_INCLUDE_DIRVAR, "GCC", 0, 0, 0, 0 },
147 #endif
148 #ifdef TOOL_INCLUDE_DIR
149 /* Another place the target system's headers might be. */
150- { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0, 0 },
151+ { TOOL_INCLUDE_DIRVAR, "BINUTILS", 0, 1, 0, 0 },
152 #endif
153 #ifdef NATIVE_SYSTEM_HEADER_DIR
154 /* /usr/include comes dead last. */
155- { NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 2 },
156- { NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 0 },
157+ { NATIVE_SYSTEM_HEADER_DIRVAR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 2 },
158+ { NATIVE_SYSTEM_HEADER_DIRVAR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 0 },
159 #endif
160 { 0, 0, 0, 0, 0, 0 }
161 };
162 #endif /* no INCLUDE_DEFAULTS */
163
164-#ifdef GCC_INCLUDE_DIR
165-const char cpp_GCC_INCLUDE_DIR[] = GCC_INCLUDE_DIR;
166-const size_t cpp_GCC_INCLUDE_DIR_len = sizeof GCC_INCLUDE_DIR - 8;
167-#else
168-const char cpp_GCC_INCLUDE_DIR[] = "";
169-const size_t cpp_GCC_INCLUDE_DIR_len = 0;
170-#endif
171-
172 /* The configured prefix. */
173-const char cpp_PREFIX[] = PREFIX;
174-const size_t cpp_PREFIX_len = sizeof PREFIX - 1;
175-const char cpp_EXEC_PREFIX[] = STANDARD_EXEC_PREFIX;
176+char PREFIXVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = PREFIX;
177+char EXEC_PREFIXVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_EXEC_PREFIX;
178
179 /* This value is set by cpp_relocated at runtime */
180 const char *gcc_exec_prefix;
Andrew Geisslerc182c622020-05-15 14:13:32 -0500181diff --git a/gcc/cppdefault.h b/gcc/cppdefault.h
182index a681264f75e..5e10a2fa140 100644
183--- a/gcc/cppdefault.h
184+++ b/gcc/cppdefault.h
Andrew Geissler82c905d2020-04-13 13:39:40 -0500185@@ -33,7 +33,8 @@
186
187 struct default_include
188 {
189- const char *const fname; /* The name of the directory. */
190+ const char *fname; /* The name of the directory. */
191+
192 const char *const component; /* The component containing the directory
193 (see update_path in prefix.c) */
194 const char cplusplus; /* Only look here if we're compiling C++. */
195@@ -50,17 +51,13 @@ struct default_include
196 };
197
198 extern const struct default_include cpp_include_defaults[];
199-extern const char cpp_GCC_INCLUDE_DIR[];
200-extern const size_t cpp_GCC_INCLUDE_DIR_len;
201+extern char GCC_INCLUDE_DIRVAR[] __attribute__ ((section (".gccrelocprefix")));
202
203 /* The configure-time prefix, i.e., the value supplied as the argument
204 to --prefix=. */
205-extern const char cpp_PREFIX[];
206+extern char PREFIXVAR[] __attribute__ ((section (".gccrelocprefix")));
207 /* The length of the configure-time prefix. */
208-extern const size_t cpp_PREFIX_len;
209-/* The configure-time execution prefix. This is typically the lib/gcc
210- subdirectory of cpp_PREFIX. */
211-extern const char cpp_EXEC_PREFIX[];
212+extern char EXEC_PREFIXVAR[] __attribute__ ((section (".gccrelocprefix")));
213 /* The run-time execution prefix. This is typically the lib/gcc
214 subdirectory of the actual installation. */
215 extern const char *gcc_exec_prefix;
Andrew Geisslerc182c622020-05-15 14:13:32 -0500216diff --git a/gcc/gcc.c b/gcc/gcc.c
217index c87f603955f..535d5c3bb65 100644
218--- a/gcc/gcc.c
219+++ b/gcc/gcc.c
220@@ -252,6 +252,8 @@ FILE *report_times_to_file = NULL;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500221 #endif
222 static const char *target_system_root = DEFAULT_TARGET_SYSTEM_ROOT;
223
224+static char target_relocatable_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = SYSTEMLIBS_DIR;
225+
226 /* Nonzero means pass the updated target_system_root to the compiler. */
227
228 static int target_system_root_changed;
Andrew Geisslerc182c622020-05-15 14:13:32 -0500229@@ -526,6 +528,7 @@ or with constant text in a single argument.
Andrew Geissler82c905d2020-04-13 13:39:40 -0500230 %G process LIBGCC_SPEC as a spec.
231 %R Output the concatenation of target_system_root and
232 target_sysroot_suffix.
233+ %r Output the base path target_relocatable_prefix
234 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
235 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
236 %C process CPP_SPEC as a spec.
Andrew Geisslerc182c622020-05-15 14:13:32 -0500237@@ -1499,10 +1502,10 @@ static const char *gcc_libexec_prefix;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500238 gcc_exec_prefix is set because, in that case, we know where the
239 compiler has been installed, and use paths relative to that
240 location instead. */
241-static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
242-static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
243-static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
244-static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
245+static char standard_exec_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_EXEC_PREFIX;
246+static char standard_libexec_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_LIBEXEC_PREFIX;
247+static char standard_bindir_prefix[4096] __attribute__ ((section (".gccrelocprefix"))) = STANDARD_BINDIR_PREFIX;
248+static char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
249
250 /* For native compilers, these are well-known paths containing
251 components that may be provided by the system. For cross
Andrew Geisslerc182c622020-05-15 14:13:32 -0500252@@ -1510,9 +1513,9 @@ static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500253 static const char *md_exec_prefix = MD_EXEC_PREFIX;
254 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
255 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
256-static const char *const standard_startfile_prefix_1
257+static char standard_startfile_prefix_1[4096] __attribute__ ((section (".gccrelocprefix")))
258 = STANDARD_STARTFILE_PREFIX_1;
259-static const char *const standard_startfile_prefix_2
260+static char standard_startfile_prefix_2[4096] __attribute__ ((section (".gccrelocprefix")))
261 = STANDARD_STARTFILE_PREFIX_2;
262
263 /* A relative path to be used in finding the location of tools
Andrew Geisslerc182c622020-05-15 14:13:32 -0500264@@ -5952,6 +5955,11 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500265 }
266 break;
267
268+ case 'r':
269+ obstack_grow (&obstack, target_relocatable_prefix,
270+ strlen (target_relocatable_prefix));
271+ break;
272+
273 case 'S':
274 value = do_spec_1 (startfile_spec, 0, NULL);
275 if (value != 0)
Andrew Geisslerc182c622020-05-15 14:13:32 -0500276diff --git a/gcc/incpath.c b/gcc/incpath.c
277index 9098ab044ab..bfad4ebe382 100644
278--- a/gcc/incpath.c
279+++ b/gcc/incpath.c
280@@ -131,7 +131,7 @@ add_standard_paths (const char *sysroot, const char *iprefix,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500281 int relocated = cpp_relocated ();
282 size_t len;
283
284- if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
285+ if (iprefix && (len = strlen(GCC_INCLUDE_DIRVAR) - 7) != 0)
286 {
287 /* Look for directories that start with the standard prefix.
288 "Translate" them, i.e. replace /usr/local/lib/gcc... with
Andrew Geisslerc182c622020-05-15 14:13:32 -0500289@@ -145,7 +145,7 @@ add_standard_paths (const char *sysroot, const char *iprefix,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500290 now. */
291 if (sysroot && p->add_sysroot)
292 continue;
293- if (!filename_ncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
294+ if (!filename_ncmp (p->fname, GCC_INCLUDE_DIRVAR, len))
295 {
296 char *str = concat (iprefix, p->fname + len, NULL);
297 if (p->multilib == 1 && imultilib)
Andrew Geisslerc182c622020-05-15 14:13:32 -0500298@@ -185,7 +185,7 @@ add_standard_paths (const char *sysroot, const char *iprefix,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500299 free (sysroot_no_trailing_dir_separator);
300 }
301 else if (!p->add_sysroot && relocated
302- && !filename_ncmp (p->fname, cpp_PREFIX, cpp_PREFIX_len))
303+ && !filename_ncmp (p->fname, PREFIXVAR, strlen(PREFIXVAR)))
304 {
305 static const char *relocated_prefix;
306 char *ostr;
Andrew Geisslerc182c622020-05-15 14:13:32 -0500307@@ -202,12 +202,12 @@ add_standard_paths (const char *sysroot, const char *iprefix,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500308 dummy = concat (gcc_exec_prefix, "dummy", NULL);
309 relocated_prefix
310 = make_relative_prefix (dummy,
311- cpp_EXEC_PREFIX,
312- cpp_PREFIX);
313+ EXEC_PREFIXVAR,
314+ PREFIXVAR);
315 free (dummy);
316 }
317 ostr = concat (relocated_prefix,
318- p->fname + cpp_PREFIX_len,
319+ p->fname + strlen(PREFIXVAR),
320 NULL);
321 str = update_path (ostr, p->component);
322 free (ostr);
Andrew Geisslerc182c622020-05-15 14:13:32 -0500323diff --git a/gcc/prefix.c b/gcc/prefix.c
324index 1a403e535bd..3257ed3cd3e 100644
325--- a/gcc/prefix.c
326+++ b/gcc/prefix.c
327@@ -72,7 +72,9 @@ License along with GCC; see the file COPYING3. If not see
Andrew Geissler82c905d2020-04-13 13:39:40 -0500328 #include "prefix.h"
329 #include "common/common-target.h"
330
331-static const char *std_prefix = PREFIX;
332+static const char PREFIXVAR[4096] __attribute__ ((section (".gccrelocprefix"))) = PREFIX;
333+
334+static const char *std_prefix = PREFIXVAR;
335
336 static const char *get_key_value (char *);
337 static char *translate_name (char *);
338@@ -212,7 +214,7 @@ translate_name (char *name)
339 prefix = getenv (key);
340
341 if (prefix == 0)
342- prefix = PREFIX;
343+ prefix = PREFIXVAR;
344
345 /* We used to strip trailing DIR_SEPARATORs here, but that can
346 sometimes yield a result with no separator when one was coded