--- ldmain.c 2005/05/12 05:49:05 1.1 +++ ldmain.c 2005/05/14 20:33:57 @@ -164,6 +164,10 @@ }; struct bfd_link_info link_info; + +static int modify_env_argc(int argc, char ***); + + static void remove_output (void) @@ -320,12 +324,14 @@ config.magic_demand_paged = TRUE; config.text_read_only = TRUE; + argc = modify_env_args(argc, &argv); emulation = get_emulation (argc, argv); ldemul_choose_mode (emulation); default_target = ldemul_choose_target (argc, argv); lang_init (); ldemul_before_parse (); lang_has_input_file = FALSE; + parse_args (argc, argv); if (config.hash_table_size != 0) @@ -1526,3 +1532,93 @@ return TRUE; } + +static int count_options(const char *env) +{ + int i; + + i = 0; + + while(*env) { + /* skip white */ + while(*env == ' ') + env++; + + if(*env) + i++; + + /* find white */ + while(*env && *env != ' ' ) + env++; + } + return i; +} + +/* malloc space and figure out option, copy it into the space */ +static char *get_env_option(char *env) +{ + char *end; + int size; + char *new; + + for(end = env; *end && *end != ' '; end++) + ; + + size = end - env + 1; + new = malloc(size); + strncpy(new, env, size - 1); + new[size - 1] = 0; +#if 0 + printf("option is %s\n", new); +#endif + return new; +} + +static char *skip_white(char *env) +{ + while(*env && *env == ' ') + env++; + + return env; +} + +/* possibly modify the argc/argv list from an environment + * variable. + * If the list is modified, new arguments are inserts started + * at position argv[1] and everything else is shifted down. + * New space is allocated for the argv array. + */ +static int modify_env_args(int argc, + char ***pass_argv) +{ + char *env; + int count; + int space_needed; + char *cp; + char **new_argv; + char *space; + int i; + char **argv; + + env = getenv("LD_ENV_OPTIONS"); + if(!env) + return argc; + + argv = *pass_argv; + count = count_options(env); + new_argv = malloc((argc + count + 1) * sizeof(char *)); + new_argv[0] = argv[0]; + for(i = 0; i < count; i++) { + env = skip_white(env); + new_argv[i + 1] = get_env_option(env); + env = index(env, ' '); + } + + /* move rest of argv to new_argv */ + memcpy(new_argv + (i + 1), argv + 1, sizeof(char *) * argc); + + *pass_argv = new_argv; + + return argc + count; +} +