From d723cd17d5f3e2db2f32bee905f0632fba4389cf Mon Sep 17 00:00:00 2001 From: David MacKenzie Date: Mon, 12 Jul 1993 19:34:04 +0000 Subject: [PATCH] * ldlang.c (lang_process): Use sizeof instead of magic constant. * ldmain.c (get_emulation, check_for_scripts_dir, set_scripts_dir): New functions. (main): Call them. --- ld/ChangeLog | 8 +++ ld/ldlang.c | 3 +- ld/ldmain.c | 175 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 131 insertions(+), 55 deletions(-) diff --git a/ld/ChangeLog b/ld/ChangeLog index 4178568b661..7445fe316c5 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,11 @@ +Mon Jul 12 11:45:48 1993 David J. Mackenzie (djm@thepub.cygnus.com) + + * ldlang.c (lang_process): Use sizeof instead of magic constant. + + * ldmain.c (get_emulation, check_for_scripts_dir, + set_scripts_dir): New functions. + (main): Call them. + Mon Jul 12 10:57:03 1993 Ken Raeburn (raeburn@deneb.cygnus.com) * scripttempl/elf.sc: Include .init, .fini, .rodata sections. diff --git a/ld/ldlang.c b/ld/ldlang.c index b53a2fab3d9..e2e8d5f7970 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -2449,7 +2449,8 @@ lang_process () { /* Read the emulation's appropriate default script. */ char *scriptname = ldemul_get_script (); - size_t size = strlen (scriptname) + 13; + /* sizeof counts the terminating NUL. */ + size_t size = strlen (scriptname) + sizeof ("-Tldscripts/"); char *buf = (char *) ldmalloc(size); sprintf (buf, "-Tldscripts/%s", scriptname); diff --git a/ld/ldmain.c b/ld/ldmain.c index 0ee740cc29a..111edea5a0f 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -37,6 +37,16 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "ldctor.h" #include "lderror.h" +#include +#if !defined(S_ISDIR) && defined(S_IFDIR) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + +#include + +static char *get_emulation (); +static void set_scripts_dir (); + /* IMPORTS */ extern boolean lang_has_input_file; extern boolean trace_files; @@ -125,56 +135,11 @@ main (argc, argv) int argc; { char *emulation; - int i; program_name = argv[0]; bfd_init (); - /* We need to find any explicitly given emulation before we initialize the - state that's needed by the lex&yacc argument parser (parse_args). */ - -#ifdef GNU960 - check_v960 (argc, argv); - emulation = "gld960"; - for (i = 1; i < argc; i++) - { - if (!strcmp (argv[i], "-Fcoff")) - { - emulation = "lnk960"; - output_flavor = BFD_COFF_FORMAT; - break; - } - } -#else - emulation = (char *) getenv (EMULATION_ENVIRON); -#endif - - for (i = 1; i < argc; i++) - { - if (!strncmp (argv[i], "-m", 2)) - { - if (argv[i][2] == '\0') - { - /* -m EMUL */ - if (i < argc - 1) - { - emulation = argv[i + 1]; - i++; - } - else - { - einfo("%P%F missing argument to -m\n"); - } - } - else - { - /* -mEMUL */ - emulation = &argv[i][2]; - } - } - } - /* Initialize the data about options. */ @@ -187,10 +152,7 @@ main (argc, argv) ldsym_init (); ldfile_add_arch (""); - /* Set the default directory for finding script files. - Libraries will be searched for here too, but we want - them to be, anyway. */ - ldfile_add_library_path (SCRIPTDIR); + set_scripts_dir (); config.make_executable = true; force_make_executable = false; @@ -205,11 +167,8 @@ main (argc, argv) config.magic_demand_paged = true; config.text_read_only = true; config.make_executable = true; - if (emulation == (char *) NULL) - { - emulation = DEFAULT_EMULATION; - } + emulation = get_emulation (argc, argv); ldemul_choose_mode (emulation); default_target = ldemul_choose_target (); lang_init (); @@ -301,7 +260,115 @@ main (argc, argv) } exit (0); -} /* main() */ +} + +/* 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). */ + +static char * +get_emulation (argc, argv) + int argc; + char **argv; +{ + char *emulation; + int i; + +#ifdef GNU960 + check_v960 (argc, argv); + emulation = "gld960"; + for (i = 1; i < argc; i++) + { + if (!strcmp (argv[i], "-Fcoff")) + { + emulation = "lnk960"; + output_flavor = BFD_COFF_FORMAT; + break; + } + } +#else + emulation = (char *) getenv (EMULATION_ENVIRON); + if (emulation == NULL) + emulation = DEFAULT_EMULATION; +#endif + + for (i = 1; i < argc; i++) + { + if (!strncmp (argv[i], "-m", 2)) + { + if (argv[i][2] == '\0') + { + /* -m EMUL */ + if (i < argc - 1) + { + emulation = argv[i + 1]; + i++; + } + else + { + einfo("%P%F missing argument to -m\n"); + } + } + else + { + /* -mEMUL */ + emulation = &argv[i][2]; + } + } + } + + return emulation; +} + +/* If directory DIR contains an "ldscripts" subdirectory, + add DIR to the library search path and return true, + else return false. */ + +static boolean +check_for_scripts_dir (dir) + char *dir; +{ + size_t dirlen; + char *buf; + struct stat s; + boolean res; + + dirlen = strlen (dir); + /* sizeof counts the terminating NUL. */ + buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts")); + sprintf (buf, "%s/ldscripts", dir); + + res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); + if (res) + { + buf[dirlen] = '\0'; + ldfile_add_library_path (buf); + } + else + free (buf); + + return res; +} + +/* Set the default directory for finding script files. + Libraries will be searched for here too, but that's ok. */ + +static void +set_scripts_dir () +{ + char *end; + + if (check_for_scripts_dir (SCRIPTDIR)) + return; /* Good--we've been installed. */ + + /* Look for "ldscripts" in the dir where our binary is. */ + end = strrchr (program_name, '/'); + if (!end) + return; /* Hope for the best. */ + + *end = '\0'; + check_for_scripts_dir (program_name); + *end = '/'; +} void Q_read_entry_symbols (desc, entry) -- 2.30.2