#include "gold.h"
+#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
#include "filenames.h"
#include "libiberty.h"
+#include "debug.h"
#include "options.h"
namespace gold
// be 0 if this function changes *argv. ARG points to the location
// in *ARGV where the option starts, which may be helpful for a
// short option.
- int (*special)(int argc, char** argv, char *arg, Command_line*);
+ int (*special)(int argc, char** argv, char *arg, bool long_option,
+ Command_line*);
// If this is a position independent option which does not take an
// argument, this is the member function to call to record it.
{ return this->general_arg != NULL || this->dependent_arg != NULL; }
};
+// We have a separate table for -z options.
+
+struct options::One_z_option
+{
+ // The name of the option.
+ const char* name;
+
+ // The member function in General_options called to record it.
+ void (General_options::*set)();
+};
+
+// We have a separate table for --debug options.
+
+struct options::One_debug_option
+{
+ // The name of the option.
+ const char* name;
+
+ // The flags to turn on.
+ unsigned int debug_flags;
+};
+
class options::Command_line_options
{
public:
static const One_option options[];
static const int options_size;
+ static const One_z_option z_options[];
+ static const int z_options_size;
+ static const One_debug_option debug_options[];
+ static const int debug_options_size;
};
} // End namespace gold.
// Handle the special -l option, which adds an input file.
int
-library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
+library(int argc, char** argv, char* arg, bool long_option,
+ gold::Command_line* cmdline)
+{
+ return cmdline->process_l_option(argc, argv, arg, long_option);
+}
+
+// Handle the special -T/--script option, which reads a linker script.
+
+int
+invoke_script(int argc, char** argv, char* arg, bool long_option,
+ gold::Command_line* cmdline)
{
- return cmdline->process_l_option(argc, argv, arg);
+ int ret;
+ const char* script_name = cmdline->get_special_argument("script", argc, argv,
+ arg, long_option,
+ &ret);
+ if (!read_commandline_script(script_name, cmdline))
+ gold::gold_error(_("%s: unable to parse script file %s\n"),
+ gold::program_name, arg);
+ return ret;
}
// Handle the special --start-group option.
int
-start_group(int, char**, char* arg, gold::Command_line* cmdline)
+start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
{
cmdline->start_group(arg);
return 1;
// Handle the special --end-group option.
int
-end_group(int, char**, char* arg, gold::Command_line* cmdline)
+end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
{
cmdline->end_group(arg);
return 1;
// Report usage information for ld --help, and exit.
int
-help(int, char**, char*, gold::Command_line*)
+help(int, char**, char*, bool, gold::Command_line*)
{
printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
std::puts(options[i].doc);
}
- ::exit(0);
+ ::exit(EXIT_SUCCESS);
return 0;
}
// Report version information.
int
-version(int, char**, char* opt, gold::Command_line*)
+version(int, char**, char* opt, bool, gold::Command_line*)
{
gold::print_version(opt[0] == 'v' && opt[1] == '\0');
- ::exit(0);
+ ::exit(EXIT_SUCCESS);
return 0;
}
const options::One_option
options::Command_line_options::options[] =
{
+ GENERAL_NOARG('\0', "allow-shlib-undefined",
+ N_("Allow unresolved references in shared libraries"),
+ NULL, TWO_DASHES,
+ &General_options::set_allow_shlib_undefined),
+ GENERAL_NOARG('\0', "no-allow-shlib-undefined",
+ N_("Do not allow unresolved references in shared libraries"),
+ NULL, TWO_DASHES,
+ &General_options::set_no_allow_shlib_undefined),
POSDEP_NOARG('\0', "as-needed",
N_("Only set DT_NEEDED for dynamic libs if used"),
NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
&Position_dependent_options::set_static_search),
GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
NULL, ONE_DASH, &General_options::set_symbolic),
+#ifdef HAVE_ZLIB_H
+# define ZLIB_STR ",zlib"
+#else
+# define ZLIB_STR ""
+#endif
+ GENERAL_ARG('\0', "compress-debug-sections",
+ N_("Compress .debug_* sections in the output file "
+ "(default is none)"),
+ N_("--compress-debug-sections=[none" ZLIB_STR "]"),
+ TWO_DASHES,
+ &General_options::set_compress_debug_sections),
+ GENERAL_NOARG('\0', "demangle", N_("Demangle C++ symbols in log messages"),
+ NULL, TWO_DASHES, &General_options::set_demangle),
+ GENERAL_NOARG('\0', "no-demangle",
+ N_("Do not demangle C++ symbols in log messages"),
+ NULL, TWO_DASHES, &General_options::clear_demangle),
+ GENERAL_NOARG('\0', "detect-odr-violations",
+ N_("Try to detect violations of the One Definition Rule"),
+ NULL, TWO_DASHES, &General_options::set_detect_odr_violations),
GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
NULL, TWO_DASHES, &General_options::set_export_dynamic),
GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
&General_options::add_to_rpath_link),
GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
TWO_DASHES, &General_options::set_strip_all),
+ GENERAL_NOARG('\0', "strip-debug-gdb",
+ N_("Strip debug symbols that are unused by gdb "
+ "(at least versions <= 6.7)"),
+ NULL, TWO_DASHES, &General_options::set_strip_debug_gdb),
+ // This must come after -Sdebug since it's a prefix of it.
GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
TWO_DASHES, &General_options::set_strip_debug),
GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
N_("-Ttext ADDRESS"), ONE_DASH,
&General_options::set_text_segment_address),
+ // This must come after -Ttext since it's a prefix of it.
+ SPECIAL('T', "script", N_("Read linker script"),
+ N_("-T FILE, --script FILE"), TWO_DASHES,
+ &invoke_script),
GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
NULL, TWO_DASHES, &General_options::set_threads),
GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
N_("Include only needed archive contents"),
NULL, TWO_DASHES,
&Position_dependent_options::clear_whole_archive),
+
+ GENERAL_ARG('z', NULL,
+ N_("Subcommands as follows:\n\
+ -z execstack Mark output as requiring executable stack\n\
+ -z noexecstack Mark output as not requiring executable stack"),
+ N_("-z SUBCOMMAND"), ONE_DASH,
+ &General_options::handle_z_option),
+
SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
TWO_DASHES, &start_group),
SPECIAL(')', "end-group", N_("End a library search group"), NULL,
SPECIAL('\0', "help", N_("Report usage information"), NULL,
TWO_DASHES, &help),
SPECIAL('v', "version", N_("Report version information"), NULL,
- TWO_DASHES, &version)
+ TWO_DASHES, &version),
+ GENERAL_ARG('\0', "debug", N_("Turn on debugging (all,task)"),
+ N_("--debug=TYPE"), TWO_DASHES,
+ &General_options::handle_debug_option)
};
const int options::Command_line_options::options_size =
sizeof (options) / sizeof (options[0]);
+// The -z options.
+
+const options::One_z_option
+options::Command_line_options::z_options[] =
+{
+ { "execstack", &General_options::set_execstack },
+ { "noexecstack", &General_options::set_noexecstack },
+};
+
+const int options::Command_line_options::z_options_size =
+ sizeof(z_options) / sizeof(z_options[0]);
+
+// The --debug options.
+
+const options::One_debug_option
+options::Command_line_options::debug_options[] =
+{
+ { "all", DEBUG_ALL },
+ { "task", DEBUG_TASK },
+};
+
+const int options::Command_line_options::debug_options_size =
+ sizeof(debug_options) / sizeof(debug_options[0]);
+
// The default values for the general options.
General_options::General_options()
output_file_name_("a.out"),
is_relocatable_(false),
strip_(STRIP_NONE),
+ allow_shlib_undefined_(false),
symbolic_(false),
+ compress_debug_sections_(NO_COMPRESSION),
+ detect_odr_violations_(false),
create_eh_frame_hdr_(false),
rpath_(),
rpath_link_(),
threads_(false),
thread_count_initial_(0),
thread_count_middle_(0),
- thread_count_final_(0)
+ thread_count_final_(0),
+ execstack_(EXECSTACK_FROM_INPUT),
+ debug_(0)
{
+ // We initialize demangle_ based on the environment variable
+ // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
+ // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
+ // environment. Acting the same way here lets us provide the same
+ // interface by default.
+ this->demangle_ = getenv("COLLECT_NO_DEMANGLE") == NULL;
}
// The default values for the position dependent options.
{
}
+// Handle the -z option.
+
+void
+General_options::handle_z_option(const char* arg)
+{
+ const int z_options_size = options::Command_line_options::z_options_size;
+ const gold::options::One_z_option* z_options =
+ gold::options::Command_line_options::z_options;
+ for (int i = 0; i < z_options_size; ++i)
+ {
+ if (strcmp(arg, z_options[i].name) == 0)
+ {
+ (this->*(z_options[i].set))();
+ return;
+ }
+ }
+
+ fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
+ program_name, arg);
+ ::exit(EXIT_FAILURE);
+}
+
+// Handle the --debug option.
+
+void
+General_options::handle_debug_option(const char* arg)
+{
+ const int debug_options_size =
+ options::Command_line_options::debug_options_size;
+ const gold::options::One_debug_option* debug_options =
+ options::Command_line_options::debug_options;
+ for (int i = 0; i < debug_options_size; ++i)
+ {
+ if (strcmp(arg, debug_options[i].name) == 0)
+ {
+ this->set_debug(debug_options[i].debug_flags);
+ return;
+ }
+ }
+
+ fprintf(stderr, _("%s: unrecognized --debug subcommand: %s\n"),
+ program_name, arg);
+ ::exit(EXIT_FAILURE);
+}
+
// Add the sysroot, if any, to the search paths.
void
{
}
-// Process the command line options.
+// Process the command line options. For process_one_option,
+// i is the index of argv to process next, and the return value
+// is the index of the next option to process (i+1 or i+2, or argc
+// to indicate processing is done). no_more_options is set to true
+// if (and when) "--" is seen as an option.
-void
-Command_line::process(int argc, char** argv)
+int
+Command_line::process_one_option(int argc, char** argv, int i,
+ bool* no_more_options)
{
const int options_size = options::Command_line_options::options_size;
- const options::One_option* options =
- options::Command_line_options::options;
- bool no_more_options = false;
- int i = 0;
- while (i < argc)
+ const options::One_option* options = options::Command_line_options::options;
+ gold_assert(i < argc);
+
+ if (argv[i][0] != '-' || *no_more_options)
{
- if (argv[i][0] != '-' || no_more_options)
- {
- this->add_file(argv[i], false);
- ++i;
- continue;
- }
+ this->add_file(argv[i], false);
+ return i + 1;
+ }
- // Option starting with '-'.
- int dashes = 1;
- if (argv[i][1] == '-')
- {
- dashes = 2;
- if (argv[i][2] == '\0')
- {
- no_more_options = true;
- continue;
- }
- }
+ // Option starting with '-'.
+ int dashes = 1;
+ if (argv[i][1] == '-')
+ {
+ dashes = 2;
+ if (argv[i][2] == '\0')
+ {
+ *no_more_options = true;
+ return i + 1;
+ }
+ }
- // Look for a long option match.
- char* opt = argv[i] + dashes;
- char first = opt[0];
- int skiparg = 0;
- char* arg = strchr(opt, '=');
- bool argument_with_equals = arg != NULL;
- if (arg != NULL)
- {
- *arg = '\0';
- ++arg;
- }
- else if (i + 1 < argc)
- {
- arg = argv[i + 1];
- skiparg = 1;
- }
+ // Look for a long option match.
+ char* opt = argv[i] + dashes;
+ char first = opt[0];
+ int skiparg = 0;
+ char* arg = strchr(opt, '=');
+ bool argument_with_equals = arg != NULL;
+ if (arg != NULL)
+ {
+ *arg = '\0';
+ ++arg;
+ }
+ else if (i + 1 < argc)
+ {
+ arg = argv[i + 1];
+ skiparg = 1;
+ }
+ int j;
+ for (j = 0; j < options_size; ++j)
+ {
+ if (options[j].long_option != NULL
+ && (dashes == 2
+ || (options[j].dash
+ != options::One_option::EXACTLY_TWO_DASHES))
+ && first == options[j].long_option[0]
+ && strcmp(opt, options[j].long_option) == 0)
+ {
+ if (options[j].special)
+ {
+ // Restore the '=' we clobbered above.
+ if (arg != NULL && skiparg == 0)
+ arg[-1] = '=';
+ i += options[j].special(argc - i, argv + i, opt, true, this);
+ }
+ else
+ {
+ if (!options[j].takes_argument())
+ {
+ if (argument_with_equals)
+ this->usage(_("unexpected argument"), argv[i]);
+ arg = NULL;
+ skiparg = 0;
+ }
+ else
+ {
+ if (arg == NULL)
+ this->usage(_("missing argument"), argv[i]);
+ }
+ this->apply_option(options[j], arg);
+ i += skiparg + 1;
+ }
+ break;
+ }
+ }
+ if (j < options_size)
+ return i;
+
+ // If we saw two dashes, we needed to have seen a long option.
+ if (dashes == 2)
+ this->usage(_("unknown option"), argv[i]);
+
+ // Look for a short option match. There may be more than one
+ // short option in a given argument.
+ bool done = false;
+ char* s = argv[i] + 1;
+ ++i;
+ while (*s != '\0' && !done)
+ {
+ char opt = *s;
int j;
for (j = 0; j < options_size; ++j)
- {
- if (options[j].long_option != NULL
- && (dashes == 2
- || (options[j].dash
- != options::One_option::EXACTLY_TWO_DASHES))
- && first == options[j].long_option[0]
- && strcmp(opt, options[j].long_option) == 0)
- {
- if (options[j].special)
- i += options[j].special(argc - 1, argv + i, opt, this);
- else
- {
- if (!options[j].takes_argument())
- {
- if (argument_with_equals)
- this->usage(_("unexpected argument"), argv[i]);
- arg = NULL;
- skiparg = 0;
- }
- else
- {
- if (arg == NULL)
- this->usage(_("missing argument"), argv[i]);
- }
- this->apply_option(options[j], arg);
- i += skiparg + 1;
- }
- break;
- }
- }
- if (j < options_size)
- continue;
-
- // If we saw two dashes, we need to see a long option.
- if (dashes == 2)
- this->usage(_("unknown option"), argv[i]);
-
- // Look for a short option match. There may be more than one
- // short option in a given argument.
- bool done = false;
- char* s = argv[i] + 1;
- ++i;
- while (*s != '\0' && !done)
- {
- char opt = *s;
- int j;
- for (j = 0; j < options_size; ++j)
- {
- if (options[j].short_option == opt)
- {
- if (options[j].special)
- {
- // Undo the argument skip done above.
- --i;
- i += options[j].special(argc - i, argv + i, s, this);
- done = true;
- }
- else
- {
- arg = NULL;
- if (options[j].takes_argument())
- {
- if (s[1] != '\0')
- {
- arg = s + 1;
- done = true;
- }
- else if (i < argc)
- {
- arg = argv[i];
- ++i;
- }
- else
- this->usage(_("missing argument"), opt);
- }
- this->apply_option(options[j], arg);
- }
- break;
- }
- }
+ {
+ if (options[j].short_option == opt)
+ {
+ if (options[j].special)
+ {
+ // Undo the argument skip done above.
+ --i;
+ i += options[j].special(argc - i, argv + i, s, false,
+ this);
+ done = true;
+ }
+ else
+ {
+ arg = NULL;
+ if (options[j].takes_argument())
+ {
+ if (s[1] != '\0')
+ {
+ arg = s + 1;
+ done = true;
+ }
+ else if (i < argc)
+ {
+ arg = argv[i];
+ ++i;
+ }
+ else
+ this->usage(_("missing argument"), opt);
+ }
+ this->apply_option(options[j], arg);
+ }
+ break;
+ }
+ }
+
+ if (j >= options_size)
+ this->usage(_("unknown option"), *s);
+
+ ++s;
+ }
+ return i;
+}
- if (j >= options_size)
- this->usage(_("unknown option"), *s);
- ++s;
- }
- }
+void
+Command_line::process(int argc, char** argv)
+{
+ bool no_more_options = false;
+ int i = 0;
+ while (i < argc)
+ i = process_one_option(argc, argv, i, &no_more_options);
if (this->inputs_.in_group())
{
- fprintf(stderr, _("%s: missing group end"), program_name);
+ fprintf(stderr, _("%s: missing group end\n"), program_name);
this->usage();
}
this->normalize_options();
}
+// Extract an option argument for a special option. LONGNAME is the
+// long name of the option. This sets *PRET to the return value for
+// the special function handler to skip to the next option.
+
+const char*
+Command_line::get_special_argument(const char* longname, int argc, char** argv,
+ const char* arg, bool long_option,
+ int *pret)
+{
+ if (long_option)
+ {
+ size_t longlen = strlen(longname);
+ gold_assert(strncmp(arg, longname, longlen) == 0);
+ arg += longlen;
+ if (*arg == '=')
+ {
+ *pret = 1;
+ return arg + 1;
+ }
+ else if (argc > 1)
+ {
+ gold_assert(*arg == '\0');
+ *pret = 2;
+ return argv[1];
+ }
+ }
+ else
+ {
+ if (arg[1] != '\0')
+ {
+ *pret = 1;
+ return arg + 1;
+ }
+ else if (argc > 1)
+ {
+ *pret = 2;
+ return argv[1];
+ }
+ }
+
+ this->usage(_("missing argument"), arg);
+}
+
// Ensure options don't contradict each other and are otherwise kosher.
void
// Handle the -l option, which requires special treatment.
int
-Command_line::process_l_option(int argc, char** argv, char* arg)
+Command_line::process_l_option(int argc, char** argv, char* arg,
+ bool long_option)
{
int ret;
- const char* libname;
- if (arg[1] != '\0')
- {
- ret = 1;
- libname = arg + 1;
- }
- else if (argc > 1)
- {
- ret = 2;
- libname = argv[argc + 1];
- }
- else
- this->usage(_("missing argument"), arg);
-
+ const char* libname = this->get_special_argument("library", argc, argv, arg,
+ long_option, &ret);
this->add_file(libname, true);
-
return ret;
}
fprintf(stderr,
_("%s: use the --help option for usage information\n"),
program_name);
- ::exit(1);
+ ::exit(EXIT_FAILURE);
}
void