From e22430578c7bc54a36eb0a8cc0a083f5b4826998 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Wed, 19 Jan 2005 11:42:49 +0000 Subject: [PATCH] * ldmain.h (ld_sysroot): Change type to a constant string. * ldmain.c (ld_sysroot): Likewise. (get_relative_sysroot, get_sysroot): New functions, adding command-line support for changing the sysroot. (main): Call the new functions. * lexsup.c (OPTION_SYSROOT): New. (ld_options): Add --sysroot. (parse_args): Add a dummy handler for it. * ld.texinfo (--sysroot): Document. * NEWS: Mention the new --sysroot option. --- ld/ChangeLog | 13 +++++++ ld/NEWS | 4 +++ ld/ld.texinfo | 6 ++++ ld/ldmain.c | 94 ++++++++++++++++++++++++++++++--------------------- ld/ldmain.h | 2 +- ld/lexsup.c | 6 ++++ 6 files changed, 86 insertions(+), 39 deletions(-) diff --git a/ld/ChangeLog b/ld/ChangeLog index 27b59bdb52..093912e55b 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,16 @@ +2005-01-19 Richard Sandiford + + * ldmain.h (ld_sysroot): Change type to a constant string. + * ldmain.c (ld_sysroot): Likewise. + (get_relative_sysroot, get_sysroot): New functions, adding command-line + support for changing the sysroot. + (main): Call the new functions. + * lexsup.c (OPTION_SYSROOT): New. + (ld_options): Add --sysroot. + (parse_args): Add a dummy handler for it. + * ld.texinfo (--sysroot): Document. + * NEWS: Mention the new --sysroot option. + 2005-01-18 Alan Modra * ldlang.c (section_already_linked): Adjust bfd_link_just_syms call. diff --git a/ld/NEWS b/ld/NEWS index 42a0e09678..8bfe5a8ceb 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -1,5 +1,9 @@ -*- text -*- +* A new command-line option, --sysroot, can be used to override the + default sysroot location. It only applies to toolchains that were + configured using --with-sysroot. + * New linker script functions: ORIGIN() and LENGTH() which return information about a specified memory region. diff --git a/ld/ld.texinfo b/ld/ld.texinfo index 3e80ac3636..171ea4047b 100644 --- a/ld/ld.texinfo +++ b/ld/ld.texinfo @@ -1504,6 +1504,12 @@ many relocations. @var{count} defaults to a value of 32768. Compute and display statistics about the operation of the linker, such as execution time and memory usage. +@kindex --sysroot +@item --sysroot=@var{directory} +Use @var{directory} as the location of the sysroot, overriding the +configure-time default. This option is only supported by linkers +that were configured using @option{--with-sysroot}. + @kindex --traditional-format @cindex traditional format @item --traditional-format diff --git a/ld/ldmain.c b/ld/ldmain.c index ca532515a4..2b58d63129 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -68,7 +68,7 @@ const char *output_filename = "a.out"; char *program_name; /* The prefix for system library directories. */ -char *ld_sysroot; +const char *ld_sysroot; /* The canonical representation of ld_sysroot. */ char * ld_canon_sysroot; @@ -110,6 +110,8 @@ ld_config_type config; sort_type sort_section; +static const char *get_sysroot + (int, char **); static char *get_emulation (int, char **); static void set_scripts_dir @@ -201,47 +203,18 @@ main (int argc, char **argv) xatexit (remove_output); -#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE - ld_sysroot = make_relative_prefix (program_name, BINDIR, - TARGET_SYSTEM_ROOT); - - if (ld_sysroot) + /* Set up the sysroot directory. */ + ld_sysroot = get_sysroot (argc, argv); + if (*ld_sysroot) { - struct stat s; - int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); - - if (!res) + if (*TARGET_SYSTEM_ROOT == 0) { - free (ld_sysroot); - ld_sysroot = NULL; + einfo ("%P%F: this linker was not configured to use sysroots"); + ld_sysroot = ""; } + else + ld_canon_sysroot = lrealpath (ld_sysroot); } - - if (! ld_sysroot) - { - ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR, - TARGET_SYSTEM_ROOT); - - if (ld_sysroot) - { - struct stat s; - int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); - - if (!res) - { - free (ld_sysroot); - ld_sysroot = NULL; - } - } - } - - if (! ld_sysroot) -#endif - ld_sysroot = TARGET_SYSTEM_ROOT; - - if (ld_sysroot && *ld_sysroot) - ld_canon_sysroot = lrealpath (ld_sysroot); - if (ld_canon_sysroot) ld_canon_sysroot_len = strlen (ld_canon_sysroot); else @@ -587,6 +560,51 @@ main (int argc, char **argv) return 0; } +/* If the configured sysroot is relocatable, try relocating it based on + default prefix FROM. Return the relocated directory if it exists, + otherwise return null. */ + +static char * +get_relative_sysroot (const char *from ATTRIBUTE_UNUSED) +{ +#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE + char *path; + struct stat s; + + path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT); + if (path) + { + if (stat (path, &s) == 0 && S_ISDIR (s.st_mode)) + return path; + free (path); + } +#endif + return 0; +} + +/* Return the sysroot directory. Return "" if no sysroot is being used. */ + +static const char * +get_sysroot (int argc, char **argv) +{ + int i; + const char *path; + + for (i = 1; i < argc; i++) + if (strncmp (argv[i], "--sysroot=", strlen ("--sysroot=")) == 0) + return argv[i] + strlen ("--sysroot="); + + path = get_relative_sysroot (BINDIR); + if (path) + return path; + + path = get_relative_sysroot (TOOLBINDIR); + if (path) + return path; + + return TARGET_SYSTEM_ROOT; +} + /* We need to find any explicitly given emulation in order to initialize the state that's needed by the lex&yacc argument parser (parse_args). */ diff --git a/ld/ldmain.h b/ld/ldmain.h index 976810ebd1..6fba64688f 100644 --- a/ld/ldmain.h +++ b/ld/ldmain.h @@ -23,7 +23,7 @@ #define LDMAIN_H extern char *program_name; -extern char *ld_sysroot; +extern const char *ld_sysroot; extern char *ld_canon_sysroot; extern int ld_canon_sysroot_len; extern bfd *output_bfd; diff --git a/ld/lexsup.c b/ld/lexsup.c index d85d912b2e..21eb1dc51c 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -71,6 +71,7 @@ enum option_values OPTION_DEFSYM, OPTION_DEMANGLE, OPTION_DYNAMIC_LINKER, + OPTION_SYSROOT, OPTION_EB, OPTION_EL, OPTION_EMBEDDED_RELOCS, @@ -233,6 +234,8 @@ static const struct ld_option ld_options[] = { {"library-path", required_argument, NULL, 'L'}, 'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"), TWO_DASHES }, + { {"sysroot=", required_argument, NULL, OPTION_SYSROOT}, + '\0', NULL, N_("Override the default sysroot location"), TWO_DASHES }, { {NULL, required_argument, NULL, '\0'}, 'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH }, { {"print-map", no_argument, NULL, 'M'}, @@ -747,6 +750,9 @@ parse_args (unsigned argc, char **argv) case OPTION_DYNAMIC_LINKER: command_line.interpreter = optarg; break; + case OPTION_SYSROOT: + /* Already handled in ldmain.c. */ + break; case OPTION_EB: command_line.endian = ENDIAN_BIG; break;