From Craig Silverstein: Have Parameters point to General_options.
[deliverable/binutils-gdb.git] / gold / options.cc
1 // options.c -- handle command line options for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <iostream>
27 #include <sys/stat.h>
28 #include "filenames.h"
29 #include "libiberty.h"
30
31 #include "debug.h"
32 #include "script.h"
33 #include "options.h"
34
35 namespace gold
36 {
37
38 // The information we keep for a single command line option.
39
40 struct options::One_option
41 {
42 // The single character option name, or '\0' if this is only a long
43 // option.
44 char short_option;
45
46 // The long option name, or NULL if this is only a short option.
47 const char* long_option;
48
49 // Description of the option for --help output, or NULL if there is none.
50 const char* doc;
51
52 // How to print the option name in --help output, or NULL to use the
53 // default.
54 const char* help_output;
55
56 // Long option dash control. This is ignored if long_option is
57 // NULL.
58 enum
59 {
60 // Long option normally takes one dash; two dashes are also
61 // accepted.
62 ONE_DASH,
63 // Long option normally takes two dashes; one dash is also
64 // accepted.
65 TWO_DASHES,
66 // Long option always takes two dashes.
67 EXACTLY_TWO_DASHES
68 } dash;
69
70 // Function for special handling, or NULL. Returns the number of
71 // arguments to skip. This will normally be at least 1, but it may
72 // be 0 if this function changes *argv. ARG points to the location
73 // in *ARGV where the option starts, which may be helpful for a
74 // short option.
75 int (*special)(int argc, char** argv, char *arg, bool long_option,
76 Command_line*);
77
78 // If this is a position independent option which does not take an
79 // argument, this is the member function to call to record it. (In
80 // this file, the bool will always be 'true' to indicate the option
81 // is set.)
82 void (General_options::*general_noarg)(bool);
83
84 // If this is a position independent function which takes an
85 // argument, this is the member function to call to record it.
86 void (General_options::*general_arg)(const char*);
87
88 // If this is a position dependent option which does not take an
89 // argument, this is the member function to call to record it. (In
90 // this file, the bool will always be 'true' to indicate the option
91 // is set.)
92 void (Position_dependent_options::*dependent_noarg)(bool);
93
94 // If this is a position dependent option which takes an argument,
95 // this is the member function to record it.
96 void (Position_dependent_options::*dependent_arg)(const char*);
97
98 // Return whether this option takes an argument.
99 bool
100 takes_argument() const
101 { return this->general_arg != NULL || this->dependent_arg != NULL; }
102 };
103
104 // We have a separate table for -z options.
105
106 struct options::One_z_option
107 {
108 // The name of the option.
109 const char* name;
110
111 // The member function in General_options called to record an option
112 // which does not take an argument.
113 void (General_options::*set_noarg)(bool);
114
115 // The member function in General_options called to record an option
116 // which does take an argument.
117 void (General_options::*set_arg)(const char*);
118 };
119
120 // We have a separate table for --debug options.
121
122 struct options::One_debug_option
123 {
124 // The name of the option.
125 const char* name;
126
127 // The flags to turn on.
128 unsigned int debug_flags;
129 };
130
131 class options::Command_line_options
132 {
133 public:
134 static const One_option options[];
135 static const int options_size;
136 static const One_z_option z_options[];
137 static const int z_options_size;
138 static const One_debug_option debug_options[];
139 static const int debug_options_size;
140 };
141
142 } // End namespace gold.
143
144 namespace
145 {
146
147 // Recognize input and output target names. The GNU linker accepts
148 // these with --format and --oformat. This code is intended to be
149 // minimally compatible. In practice for an ELF target this would be
150 // the same target as the input files; that name always start with
151 // "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex",
152 // "binary", "ihex". See also
153 // General_options::default_target_settings.
154
155 gold::General_options::Object_format
156 string_to_object_format(const char* arg)
157 {
158 if (strncmp(arg, "elf", 3) == 0)
159 return gold::General_options::OBJECT_FORMAT_ELF;
160 else if (strcmp(arg, "binary") == 0)
161 return gold::General_options::OBJECT_FORMAT_BINARY;
162 else
163 {
164 gold::gold_error(_("format '%s' not supported "
165 "(supported formats: elf, binary)"),
166 arg);
167 return gold::General_options::OBJECT_FORMAT_ELF;
168 }
169 }
170
171 // Handle the special -defsym option, which defines a symbol.
172
173 int
174 add_to_defsym(int argc, char** argv, char* arg, bool long_option,
175 gold::Command_line* cmdline)
176 {
177 int ret;
178 const char* val = cmdline->get_special_argument("defsym", argc, argv, arg,
179 long_option, &ret);
180 cmdline->script_options().define_symbol(val);
181 return ret;
182 }
183
184 // Handle the special -l option, which adds an input file.
185
186 int
187 library(int argc, char** argv, char* arg, bool long_option,
188 gold::Command_line* cmdline)
189 {
190 return cmdline->process_l_option(argc, argv, arg, long_option);
191 }
192
193 // Handle the -R option. Historically the GNU linker made -R a
194 // synonym for --just-symbols. ELF linkers have traditionally made -R
195 // a synonym for -rpath. When ELF support was added to the GNU
196 // linker, -R was changed to switch based on the argument: if the
197 // argument is an ordinary file, we treat it as --just-symbols,
198 // otherwise we treat it as -rpath. We need to be compatible with
199 // this, because existing build scripts rely on it.
200
201 int
202 handle_r_option(int argc, char** argv, char* arg, bool long_option,
203 gold::Command_line* cmdline)
204 {
205 int ret;
206 const char* val = cmdline->get_special_argument("R", argc, argv, arg,
207 long_option, &ret);
208 struct stat s;
209 if (::stat(val, &s) != 0 || S_ISDIR(s.st_mode))
210 cmdline->add_to_rpath(val);
211 else
212 cmdline->add_just_symbols_file(val);
213 return ret;
214 }
215
216 // Handle the --just-symbols option.
217
218 int
219 handle_just_symbols_option(int argc, char** argv, char* arg,
220 bool long_option, gold::Command_line* cmdline)
221 {
222 int ret;
223 const char* val = cmdline->get_special_argument("just-symbols", argc, argv,
224 arg, long_option, &ret);
225 cmdline->add_just_symbols_file(val);
226 return ret;
227 }
228
229 // Handle the special -T/--script option, which reads a linker script.
230
231 int
232 invoke_script(int argc, char** argv, char* arg, bool long_option,
233 gold::Command_line* cmdline)
234 {
235 int ret;
236 const char* script_name = cmdline->get_special_argument("script", argc, argv,
237 arg, long_option,
238 &ret);
239 if (!read_commandline_script(script_name, cmdline))
240 gold::gold_fatal(_("unable to parse script file %s"), script_name);
241 return ret;
242 }
243
244 // Handle the special --version-script option, which reads a version script.
245
246 int
247 invoke_version_script(int argc, char** argv, char* arg, bool long_option,
248 gold::Command_line* cmdline)
249 {
250 int ret;
251 const char* script_name = cmdline->get_special_argument("version-script",
252 argc, argv,
253 arg, long_option,
254 &ret);
255 if (!read_version_script(script_name, cmdline))
256 gold::gold_fatal(_("unable to parse version script file %s"), script_name);
257 return ret;
258 }
259
260 // Handle the special --start-group option.
261
262 int
263 start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
264 {
265 cmdline->start_group(arg);
266 return 1;
267 }
268
269 // Handle the special --end-group option.
270
271 int
272 end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
273 {
274 cmdline->end_group(arg);
275 return 1;
276 }
277
278 // Report usage information for ld --help, and exit.
279
280 int
281 help(int, char**, char*, bool, gold::Command_line*)
282 {
283 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
284
285 const int options_size = gold::options::Command_line_options::options_size;
286 const gold::options::One_option* options =
287 gold::options::Command_line_options::options;
288 for (int i = 0; i < options_size; ++i)
289 {
290 if (options[i].doc == NULL)
291 continue;
292
293 printf(" ");
294 int len = 2;
295 bool comma = false;
296
297 int j = i;
298 do
299 {
300 if (options[j].help_output != NULL)
301 {
302 if (comma)
303 {
304 printf(", ");
305 len += 2;
306 }
307 printf(options[j].help_output);
308 len += std::strlen(options[j].help_output);
309 comma = true;
310 }
311 else
312 {
313 if (options[j].short_option != '\0')
314 {
315 if (comma)
316 {
317 printf(", ");
318 len += 2;
319 }
320 printf("-%c", options[j].short_option);
321 len += 2;
322 comma = true;
323 }
324
325 if (options[j].long_option != NULL)
326 {
327 if (comma)
328 {
329 printf(", ");
330 len += 2;
331 }
332 if (options[j].dash == gold::options::One_option::ONE_DASH)
333 {
334 printf("-");
335 ++len;
336 }
337 else
338 {
339 printf("--");
340 len += 2;
341 }
342 printf("%s", options[j].long_option);
343 len += std::strlen(options[j].long_option);
344 comma = true;
345 }
346 }
347 ++j;
348 }
349 while (j < options_size && options[j].doc == NULL);
350
351 if (len >= 30)
352 {
353 printf("\n");
354 len = 0;
355 }
356 for (; len < 30; ++len)
357 std::putchar(' ');
358
359 std::puts(options[i].doc);
360 }
361
362 ::exit(EXIT_SUCCESS);
363
364 return 0;
365 }
366
367 // Report version information.
368
369 int
370 version(int, char**, char* opt, bool, gold::Command_line*)
371 {
372 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
373 ::exit(EXIT_SUCCESS);
374 return 0;
375 }
376
377 // If the default sysroot is relocatable, try relocating it based on
378 // the prefix FROM.
379
380 char*
381 get_relative_sysroot(const char* from)
382 {
383 char* path = make_relative_prefix(gold::program_name, from,
384 TARGET_SYSTEM_ROOT);
385 if (path != NULL)
386 {
387 struct stat s;
388 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
389 return path;
390 free(path);
391 }
392
393 return NULL;
394 }
395
396 // Return the default sysroot. This is set by the --with-sysroot
397 // option to configure.
398
399 std::string
400 get_default_sysroot()
401 {
402 const char* sysroot = TARGET_SYSTEM_ROOT;
403 if (*sysroot == '\0')
404 return "";
405
406 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
407 {
408 char* path = get_relative_sysroot (BINDIR);
409 if (path == NULL)
410 path = get_relative_sysroot (TOOLBINDIR);
411 if (path != NULL)
412 {
413 std::string ret = path;
414 free(path);
415 return ret;
416 }
417 }
418
419 return sysroot;
420 }
421
422 } // End anonymous namespace.
423
424 namespace gold
425 {
426
427 // Helper macros used to specify the options. We could also do this
428 // using constructors, but then g++ would generate code to initialize
429 // the array. We want the array to be initialized statically so that
430 // we get better startup time.
431
432 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
433 { short_option, long_option, doc, help, options::One_option::dash, \
434 NULL, func, NULL, NULL, NULL }
435 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
436 { short_option, long_option, doc, help, options::One_option::dash, \
437 NULL, NULL, func, NULL, NULL }
438 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
439 { short_option, long_option, doc, help, options::One_option::dash, \
440 NULL, NULL, NULL, func, NULL }
441 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
442 { short_option, long_option, doc, help, options::One_option::dash, \
443 NULL, NULL, NULL, NULL, func }
444 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
445 { short_option, long_option, doc, help, options::One_option::dash, \
446 func, NULL, NULL, NULL, NULL }
447
448 // Here is the actual list of options which we accept.
449
450 const options::One_option
451 options::Command_line_options::options[] =
452 {
453 GENERAL_NOARG('\0', "allow-shlib-undefined",
454 N_("Allow unresolved references in shared libraries"),
455 NULL, TWO_DASHES,
456 &General_options::set_allow_shlib_undefined),
457 GENERAL_NOARG('\0', "no-allow-shlib-undefined",
458 N_("Do not allow unresolved references in shared libraries"),
459 NULL, TWO_DASHES,
460 &General_options::set_no_allow_shlib_undefined),
461 POSDEP_NOARG('\0', "as-needed",
462 N_("Only set DT_NEEDED for dynamic libs if used"),
463 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
464 POSDEP_NOARG('\0', "no-as-needed",
465 N_("Always DT_NEEDED for dynamic libs (default)"),
466 NULL, TWO_DASHES, &Position_dependent_options::set_no_as_needed),
467 POSDEP_NOARG('\0', "Bdynamic",
468 N_("-l searches for shared libraries"),
469 NULL, ONE_DASH,
470 &Position_dependent_options::set_Bdynamic),
471 POSDEP_NOARG('\0', "Bstatic",
472 N_("-l does not search for shared libraries"),
473 NULL, ONE_DASH,
474 &Position_dependent_options::set_Bstatic),
475 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
476 NULL, ONE_DASH, &General_options::set_Bsymbolic),
477 POSDEP_ARG('b', "format", N_("Set input format (elf, binary)"),
478 N_("-b FORMAT, --format FORMAT"), TWO_DASHES,
479 &Position_dependent_options::set_format),
480 #ifdef HAVE_ZLIB_H
481 # define ZLIB_STR ",zlib"
482 #else
483 # define ZLIB_STR ""
484 #endif
485 GENERAL_ARG('\0', "compress-debug-sections",
486 N_("Compress .debug_* sections in the output file "
487 "(default is none)"),
488 N_("--compress-debug-sections=[none" ZLIB_STR "]"),
489 TWO_DASHES,
490 &General_options::set_compress_debug_sections),
491 SPECIAL('\0', "defsym", N_("Define a symbol"),
492 N_("--defsym SYMBOL=EXPRESSION"), TWO_DASHES,
493 &add_to_defsym),
494 GENERAL_NOARG('\0', "demangle", N_("Demangle C++ symbols in log messages"),
495 NULL, TWO_DASHES, &General_options::set_demangle),
496 GENERAL_NOARG('\0', "no-demangle",
497 N_("Do not demangle C++ symbols in log messages"),
498 NULL, TWO_DASHES, &General_options::set_no_demangle),
499 GENERAL_NOARG('\0', "detect-odr-violations",
500 N_("Try to detect violations of the One Definition Rule"),
501 NULL, TWO_DASHES, &General_options::set_detect_odr_violations),
502 GENERAL_ARG('e', "entry", N_("Set program start address"),
503 N_("-e ADDRESS, --entry ADDRESS"), TWO_DASHES,
504 &General_options::set_entry),
505 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
506 NULL, TWO_DASHES, &General_options::set_export_dynamic),
507 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
508 NULL, TWO_DASHES, &General_options::set_eh_frame_hdr),
509 GENERAL_ARG('h', "soname", N_("Set shared library name"),
510 N_("-h FILENAME, -soname FILENAME"), ONE_DASH,
511 &General_options::set_soname),
512 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
513 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
514 &General_options::set_dynamic_linker),
515 SPECIAL('l', "library", N_("Search for library LIBNAME"),
516 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
517 &library),
518 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
519 N_("-L DIR, --library-path DIR"), TWO_DASHES,
520 &General_options::add_to_search_path),
521 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
522 &General_options::ignore),
523 GENERAL_ARG('o', "output", N_("Set output file name"),
524 N_("-o FILE, --output FILE"), TWO_DASHES,
525 &General_options::set_output),
526 GENERAL_ARG('O', "optimize", N_("Optimize output file size"),
527 N_("-O level"), ONE_DASH,
528 &General_options::set_optimize),
529 GENERAL_ARG('\0', "oformat", N_("Set output format (only binary supported)"),
530 N_("--oformat FORMAT"), EXACTLY_TWO_DASHES,
531 &General_options::set_oformat),
532 GENERAL_NOARG('q', "emit-relocs", N_("Generate relocations in output"),
533 NULL, TWO_DASHES, &General_options::set_emit_relocs),
534 GENERAL_NOARG('r', "relocatable", N_("Generate relocatable output"), NULL,
535 TWO_DASHES, &General_options::set_relocatable),
536 // -R really means -rpath, but can mean --just-symbols for
537 // compatibility with GNU ld. -rpath is always -rpath, so we list
538 // it separately.
539 SPECIAL('R', NULL, N_("Add DIR to runtime search path"),
540 N_("-R DIR"), ONE_DASH, &handle_r_option),
541 GENERAL_ARG('\0', "rpath", NULL, N_("-rpath DIR"), ONE_DASH,
542 &General_options::add_to_rpath),
543 SPECIAL('\0', "just-symbols", N_("Read only symbol values from file"),
544 N_("-R FILE, --just-symbols FILE"), TWO_DASHES,
545 &handle_just_symbols_option),
546 GENERAL_ARG('\0', "rpath-link",
547 N_("Add DIR to link time shared library search path"),
548 N_("--rpath-link DIR"), TWO_DASHES,
549 &General_options::add_to_rpath_link),
550 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
551 TWO_DASHES, &General_options::set_strip_all),
552 GENERAL_NOARG('\0', "strip-debug-gdb",
553 N_("Strip debug symbols that are unused by gdb "
554 "(at least versions <= 6.7)"),
555 NULL, TWO_DASHES, &General_options::set_strip_debug_gdb),
556 // This must come after -Sdebug since it's a prefix of it.
557 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
558 TWO_DASHES, &General_options::set_strip_debug),
559 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
560 NULL, ONE_DASH, &General_options::set_shared),
561 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
562 NULL, ONE_DASH, &General_options::set_static),
563 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
564 NULL, TWO_DASHES, &General_options::set_stats),
565 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
566 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
567 GENERAL_ARG('\0', "Tbss", N_("Set the address of the bss segment"),
568 N_("-Tbss ADDRESS"), ONE_DASH,
569 &General_options::set_Tbss),
570 GENERAL_ARG('\0', "Tdata", N_("Set the address of the data segment"),
571 N_("-Tdata ADDRESS"), ONE_DASH,
572 &General_options::set_Tdata),
573 GENERAL_ARG('\0', "Ttext", N_("Set the address of the text segment"),
574 N_("-Ttext ADDRESS"), ONE_DASH,
575 &General_options::set_Ttext),
576 // This must come after -Ttext and friends since it's a prefix of
577 // them.
578 SPECIAL('T', "script", N_("Read linker script"),
579 N_("-T FILE, --script FILE"), TWO_DASHES,
580 &invoke_script),
581 SPECIAL('\0', "version-script", N_("Read version script"),
582 N_("--version-script FILE"), TWO_DASHES,
583 &invoke_version_script),
584 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
585 NULL, TWO_DASHES, &General_options::set_threads),
586 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
587 NULL, TWO_DASHES, &General_options::set_no_threads),
588 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
589 N_("--thread-count COUNT"), TWO_DASHES,
590 &General_options::set_thread_count),
591 GENERAL_ARG('\0', "thread-count-initial",
592 N_("Number of threads to use in initial pass"),
593 N_("--thread-count-initial COUNT"), TWO_DASHES,
594 &General_options::set_thread_count_initial),
595 GENERAL_ARG('\0', "thread-count-middle",
596 N_("Number of threads to use in middle pass"),
597 N_("--thread-count-middle COUNT"), TWO_DASHES,
598 &General_options::set_thread_count_middle),
599 GENERAL_ARG('\0', "thread-count-final",
600 N_("Number of threads to use in final pass"),
601 N_("--thread-count-final COUNT"), TWO_DASHES,
602 &General_options::set_thread_count_final),
603 POSDEP_NOARG('\0', "whole-archive",
604 N_("Include all archive contents"),
605 NULL, TWO_DASHES,
606 &Position_dependent_options::set_whole_archive),
607 POSDEP_NOARG('\0', "no-whole-archive",
608 N_("Include only needed archive contents"),
609 NULL, TWO_DASHES,
610 &Position_dependent_options::set_no_whole_archive),
611
612 GENERAL_ARG('z', NULL,
613 N_("Subcommands as follows:\n\
614 -z execstack Mark output as requiring executable stack\n\
615 -z noexecstack Mark output as not requiring executable stack\n\
616 -z max-page-size=SIZE Set maximum page size to SIZE\n\
617 -z common-page-size=SIZE Set common page size to SIZE"),
618 N_("-z SUBCOMMAND"), ONE_DASH,
619 &General_options::handle_z_option),
620
621 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
622 TWO_DASHES, &start_group),
623 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
624 TWO_DASHES, &end_group),
625 SPECIAL('\0', "help", N_("Report usage information"), NULL,
626 TWO_DASHES, &help),
627 SPECIAL('v', "version", N_("Report version information"), NULL,
628 TWO_DASHES, &version),
629 GENERAL_ARG('\0', "debug", N_("Turn on debugging (all,task,script)"),
630 N_("--debug=TYPE"), TWO_DASHES,
631 &General_options::handle_debug_option)
632 };
633
634 const int options::Command_line_options::options_size =
635 sizeof (options) / sizeof (options[0]);
636
637 // The -z options.
638
639 const options::One_z_option
640 options::Command_line_options::z_options[] =
641 {
642 { "execstack", &General_options::set_execstack, NULL },
643 { "noexecstack", &General_options::set_noexecstack, NULL },
644 { "max-page-size", NULL, &General_options::set_max_page_size },
645 { "common-page-size", NULL, &General_options::set_common_page_size }
646 };
647
648 const int options::Command_line_options::z_options_size =
649 sizeof(z_options) / sizeof(z_options[0]);
650
651 // The --debug options.
652
653 const options::One_debug_option
654 options::Command_line_options::debug_options[] =
655 {
656 { "all", DEBUG_ALL },
657 { "task", DEBUG_TASK },
658 { "script", DEBUG_SCRIPT }
659 };
660
661 const int options::Command_line_options::debug_options_size =
662 sizeof(debug_options) / sizeof(debug_options[0]);
663
664 // The default values for the general options.
665
666 General_options::General_options()
667 : entry_(NULL),
668 export_dynamic_(false),
669 soname_(NULL),
670 dynamic_linker_(NULL),
671 search_path_(),
672 optimization_level_(0),
673 output_file_name_("a.out"),
674 oformat_(OBJECT_FORMAT_ELF),
675 oformat_string_(NULL),
676 emit_relocs_(false),
677 is_relocatable_(false),
678 strip_(STRIP_NONE),
679 allow_shlib_undefined_(false),
680 symbolic_(false),
681 compress_debug_sections_(NO_COMPRESSION),
682 detect_odr_violations_(false),
683 create_eh_frame_hdr_(false),
684 rpath_(),
685 rpath_link_(),
686 is_shared_(false),
687 is_static_(false),
688 print_stats_(false),
689 sysroot_(),
690 bss_segment_address_(-1U), // -1 indicates value not set by user
691 data_segment_address_(-1U),
692 text_segment_address_(-1U),
693 threads_(false),
694 thread_count_initial_(0),
695 thread_count_middle_(0),
696 thread_count_final_(0),
697 execstack_(EXECSTACK_FROM_INPUT),
698 max_page_size_(0),
699 common_page_size_(0),
700 debug_(0)
701 {
702 // We initialize demangle_ based on the environment variable
703 // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
704 // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
705 // environment. Acting the same way here lets us provide the same
706 // interface by default.
707 this->demangle_ = getenv("COLLECT_NO_DEMANGLE") == NULL;
708 }
709
710 // Handle the --oformat option.
711
712 void
713 General_options::set_oformat(const char* arg)
714 {
715 this->oformat_string_ = arg;
716 this->oformat_ = string_to_object_format(arg);
717 }
718
719 // Handle the -z option.
720
721 void
722 General_options::handle_z_option(const char* arg)
723 {
724 // ARG may be a word, like "noexec", or it may be an option in its
725 // own right, like "max-page-size=SIZE".
726 const char* argarg = strchr(arg, '='); // the argument to the -z argument
727 int arglen;
728 if (argarg)
729 {
730 arglen = argarg - arg;
731 argarg++;
732 }
733 else
734 arglen = strlen(arg);
735
736 const int z_options_size = options::Command_line_options::z_options_size;
737 const gold::options::One_z_option* z_options =
738 gold::options::Command_line_options::z_options;
739 for (int i = 0; i < z_options_size; ++i)
740 {
741 if (memcmp(arg, z_options[i].name, arglen) == 0
742 && z_options[i].name[arglen] == '\0')
743 {
744 if (z_options[i].set_noarg && argarg)
745 gold::gold_fatal(_("-z subcommand does not take an argument: %s\n"),
746 z_options[i].name);
747 else if (z_options[i].set_arg && !argarg)
748 gold::gold_fatal(_("-z subcommand requires an argument: %s\n"),
749 z_options[i].name);
750 else if (z_options[i].set_arg)
751 (this->*(z_options[i].set_arg))(argarg);
752 else
753 (this->*(z_options[i].set_noarg))(true);
754 return;
755 }
756 }
757
758 gold::gold_fatal(_("%s: unrecognized -z subcommand: %s\n"),
759 program_name, arg);
760 }
761
762 // Handle the --debug option.
763
764 void
765 General_options::handle_debug_option(const char* arg)
766 {
767 const int debug_options_size =
768 options::Command_line_options::debug_options_size;
769 const gold::options::One_debug_option* debug_options =
770 options::Command_line_options::debug_options;
771 for (int i = 0; i < debug_options_size; ++i)
772 {
773 if (strcmp(arg, debug_options[i].name) == 0)
774 {
775 this->set_debug(debug_options[i].debug_flags);
776 return;
777 }
778 }
779
780 fprintf(stderr, _("%s: unrecognized --debug subcommand: %s\n"),
781 program_name, arg);
782 ::exit(EXIT_FAILURE);
783 }
784
785 // Add the sysroot, if any, to the search paths.
786
787 void
788 General_options::add_sysroot()
789 {
790 if (this->sysroot_.empty())
791 {
792 this->sysroot_ = get_default_sysroot();
793 if (this->sysroot_.empty())
794 return;
795 }
796
797 const char* sysroot = this->sysroot_.c_str();
798 char* canonical_sysroot = lrealpath(sysroot);
799
800 for (Dir_list::iterator p = this->search_path_.begin();
801 p != this->search_path_.end();
802 ++p)
803 p->add_sysroot(sysroot, canonical_sysroot);
804
805 free(canonical_sysroot);
806 }
807
808 // The default values for the position dependent options.
809
810 Position_dependent_options::Position_dependent_options()
811 : do_static_search_(false),
812 as_needed_(false),
813 include_whole_archive_(false),
814 input_format_(General_options::OBJECT_FORMAT_ELF)
815 {
816 }
817
818 // Set the input format.
819
820 void
821 Position_dependent_options::set_format(const char* arg)
822 {
823 this->input_format_ = string_to_object_format(arg);
824 }
825
826 // Search_directory methods.
827
828 // This is called if we have a sysroot. Apply the sysroot if
829 // appropriate. Record whether the directory is in the sysroot.
830
831 void
832 Search_directory::add_sysroot(const char* sysroot,
833 const char* canonical_sysroot)
834 {
835 gold_assert(*sysroot != '\0');
836 if (this->put_in_sysroot_)
837 {
838 if (!IS_DIR_SEPARATOR(this->name_[0])
839 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
840 this->name_ = '/' + this->name_;
841 this->name_ = sysroot + this->name_;
842 this->is_in_sysroot_ = true;
843 }
844 else
845 {
846 // Check whether this entry is in the sysroot. To do this
847 // correctly, we need to use canonical names. Otherwise we will
848 // get confused by the ../../.. paths that gcc tends to use.
849 char* canonical_name = lrealpath(this->name_.c_str());
850 int canonical_name_len = strlen(canonical_name);
851 int canonical_sysroot_len = strlen(canonical_sysroot);
852 if (canonical_name_len > canonical_sysroot_len
853 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
854 {
855 canonical_name[canonical_sysroot_len] = '\0';
856 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
857 this->is_in_sysroot_ = true;
858 }
859 free(canonical_name);
860 }
861 }
862
863 // Input_arguments methods.
864
865 // Add a file to the list.
866
867 void
868 Input_arguments::add_file(const Input_file_argument& file)
869 {
870 if (!this->in_group_)
871 this->input_argument_list_.push_back(Input_argument(file));
872 else
873 {
874 gold_assert(!this->input_argument_list_.empty());
875 gold_assert(this->input_argument_list_.back().is_group());
876 this->input_argument_list_.back().group()->add_file(file);
877 }
878 }
879
880 // Start a group.
881
882 void
883 Input_arguments::start_group()
884 {
885 gold_assert(!this->in_group_);
886 Input_file_group* group = new Input_file_group();
887 this->input_argument_list_.push_back(Input_argument(group));
888 this->in_group_ = true;
889 }
890
891 // End a group.
892
893 void
894 Input_arguments::end_group()
895 {
896 gold_assert(this->in_group_);
897 this->in_group_ = false;
898 }
899
900 // Command_line options.
901
902 Command_line::Command_line()
903 : options_(), position_options_(), script_options_(), inputs_()
904 {
905 }
906
907 // Process the command line options. For process_one_option,
908 // i is the index of argv to process next, and the return value
909 // is the index of the next option to process (i+1 or i+2, or argc
910 // to indicate processing is done). no_more_options is set to true
911 // if (and when) "--" is seen as an option.
912
913 int
914 Command_line::process_one_option(int argc, char** argv, int i,
915 bool* no_more_options)
916 {
917 const int options_size = options::Command_line_options::options_size;
918 const options::One_option* options = options::Command_line_options::options;
919 gold_assert(i < argc);
920
921 if (argv[i][0] != '-' || *no_more_options)
922 {
923 this->add_file(argv[i], false);
924 return i + 1;
925 }
926
927 // Option starting with '-'.
928 int dashes = 1;
929 if (argv[i][1] == '-')
930 {
931 dashes = 2;
932 if (argv[i][2] == '\0')
933 {
934 *no_more_options = true;
935 return i + 1;
936 }
937 }
938
939 // Look for a long option match.
940 char* opt = argv[i] + dashes;
941 char first = opt[0];
942 int skiparg = 0;
943 char* arg = strchr(opt, '=');
944 bool argument_with_equals = arg != NULL;
945 if (arg != NULL)
946 {
947 *arg = '\0';
948 ++arg;
949 }
950 else if (i + 1 < argc)
951 {
952 arg = argv[i + 1];
953 skiparg = 1;
954 }
955
956 int j;
957 for (j = 0; j < options_size; ++j)
958 {
959 if (options[j].long_option != NULL
960 && (dashes == 2
961 || (options[j].dash
962 != options::One_option::EXACTLY_TWO_DASHES))
963 && first == options[j].long_option[0]
964 && strcmp(opt, options[j].long_option) == 0)
965 {
966 if (options[j].special)
967 {
968 // Restore the '=' we clobbered above.
969 if (arg != NULL && skiparg == 0)
970 arg[-1] = '=';
971 i += options[j].special(argc - i, argv + i, opt, true, this);
972 }
973 else
974 {
975 if (!options[j].takes_argument())
976 {
977 if (argument_with_equals)
978 this->usage(_("unexpected argument"), argv[i]);
979 arg = NULL;
980 skiparg = 0;
981 }
982 else
983 {
984 if (arg == NULL)
985 this->usage(_("missing argument"), argv[i]);
986 }
987 this->apply_option(options[j], arg);
988 i += skiparg + 1;
989 }
990 break;
991 }
992 }
993 if (j < options_size)
994 return i;
995
996 // If we saw two dashes, we needed to have seen a long option.
997 if (dashes == 2)
998 this->usage(_("unknown option"), argv[i]);
999
1000 // Look for a short option match. There may be more than one
1001 // short option in a given argument.
1002 bool done = false;
1003 char* s = argv[i] + 1;
1004 ++i;
1005 while (*s != '\0' && !done)
1006 {
1007 char opt = *s;
1008 int j;
1009 for (j = 0; j < options_size; ++j)
1010 {
1011 if (options[j].short_option == opt)
1012 {
1013 if (options[j].special)
1014 {
1015 // Undo the argument skip done above.
1016 --i;
1017 i += options[j].special(argc - i, argv + i, s, false,
1018 this);
1019 done = true;
1020 }
1021 else
1022 {
1023 arg = NULL;
1024 if (options[j].takes_argument())
1025 {
1026 if (s[1] != '\0')
1027 {
1028 arg = s + 1;
1029 done = true;
1030 }
1031 else if (i < argc)
1032 {
1033 arg = argv[i];
1034 ++i;
1035 }
1036 else
1037 this->usage(_("missing argument"), opt);
1038 }
1039 this->apply_option(options[j], arg);
1040 }
1041 break;
1042 }
1043 }
1044
1045 if (j >= options_size)
1046 this->usage(_("unknown option"), *s);
1047
1048 ++s;
1049 }
1050 return i;
1051 }
1052
1053
1054 void
1055 Command_line::process(int argc, char** argv)
1056 {
1057 bool no_more_options = false;
1058 int i = 0;
1059 while (i < argc)
1060 i = process_one_option(argc, argv, i, &no_more_options);
1061
1062 if (this->inputs_.in_group())
1063 {
1064 fprintf(stderr, _("%s: missing group end\n"), program_name);
1065 this->usage();
1066 }
1067
1068 // FIXME: We should only do this when configured in native mode.
1069 this->options_.add_to_search_path_with_sysroot("/lib");
1070 this->options_.add_to_search_path_with_sysroot("/usr/lib");
1071
1072 this->options_.add_sysroot();
1073
1074 // Ensure options don't contradict each other and are otherwise kosher.
1075 this->normalize_options();
1076 }
1077
1078 // Extract an option argument for a special option. LONGNAME is the
1079 // long name of the option. This sets *PRET to the return value for
1080 // the special function handler to skip to the next option.
1081
1082 const char*
1083 Command_line::get_special_argument(const char* longname, int argc, char** argv,
1084 const char* arg, bool long_option,
1085 int *pret)
1086 {
1087 if (long_option)
1088 {
1089 size_t longlen = strlen(longname);
1090 gold_assert(strncmp(arg, longname, longlen) == 0);
1091 arg += longlen;
1092 if (*arg == '=')
1093 {
1094 *pret = 1;
1095 return arg + 1;
1096 }
1097 else if (argc > 1)
1098 {
1099 gold_assert(*arg == '\0');
1100 *pret = 2;
1101 return argv[1];
1102 }
1103 }
1104 else
1105 {
1106 if (arg[1] != '\0')
1107 {
1108 *pret = 1;
1109 return arg + 1;
1110 }
1111 else if (argc > 1)
1112 {
1113 *pret = 2;
1114 return argv[1];
1115 }
1116 }
1117
1118 this->usage(_("missing argument"), arg);
1119 }
1120
1121 // Ensure options don't contradict each other and are otherwise kosher.
1122
1123 void
1124 Command_line::normalize_options()
1125 {
1126 if (this->options_.shared() && this->options_.relocatable())
1127 gold_fatal(_("-shared and -r are incompatible"));
1128
1129 if (this->options_.oformat() != General_options::OBJECT_FORMAT_ELF
1130 && (this->options_.shared() || this->options_.relocatable()))
1131 gold_fatal(_("binary output format not compatible with -shared or -r"));
1132
1133 // If the user specifies both -s and -r, convert the -s as -S.
1134 // -r requires us to keep externally visible symbols!
1135 if (this->options_.strip_all() && this->options_.relocatable())
1136 {
1137 // Clears the strip_all() status, replacing it with strip_debug().
1138 this->options_.set_strip_debug(true);
1139 }
1140
1141 // FIXME: we can/should be doing a lot more sanity checking here.
1142 }
1143
1144
1145 // Apply a command line option.
1146
1147 void
1148 Command_line::apply_option(const options::One_option& opt,
1149 const char* arg)
1150 {
1151 if (arg == NULL)
1152 {
1153 if (opt.general_noarg)
1154 (this->options_.*(opt.general_noarg))(true);
1155 else if (opt.dependent_noarg)
1156 (this->position_options_.*(opt.dependent_noarg))(true);
1157 else
1158 gold_unreachable();
1159 }
1160 else
1161 {
1162 if (opt.general_arg)
1163 (this->options_.*(opt.general_arg))(arg);
1164 else if (opt.dependent_arg)
1165 (this->position_options_.*(opt.dependent_arg))(arg);
1166 else
1167 gold_unreachable();
1168 }
1169 }
1170
1171 // Add an input file or library.
1172
1173 void
1174 Command_line::add_file(const char* name, bool is_lib)
1175 {
1176 Input_file_argument file(name, is_lib, "", false, this->position_options_);
1177 this->inputs_.add_file(file);
1178 }
1179
1180 // Handle the -l option, which requires special treatment.
1181
1182 int
1183 Command_line::process_l_option(int argc, char** argv, char* arg,
1184 bool long_option)
1185 {
1186 int ret;
1187 const char* libname = this->get_special_argument("library", argc, argv, arg,
1188 long_option, &ret);
1189 this->add_file(libname, true);
1190 return ret;
1191 }
1192
1193 // Handle the --start-group option.
1194
1195 void
1196 Command_line::start_group(const char* arg)
1197 {
1198 if (this->inputs_.in_group())
1199 this->usage(_("may not nest groups"), arg);
1200 this->inputs_.start_group();
1201 }
1202
1203 // Handle the --end-group option.
1204
1205 void
1206 Command_line::end_group(const char* arg)
1207 {
1208 if (!this->inputs_.in_group())
1209 this->usage(_("group end without group start"), arg);
1210 this->inputs_.end_group();
1211 }
1212
1213 // Report a usage error. */
1214
1215 void
1216 Command_line::usage()
1217 {
1218 fprintf(stderr,
1219 _("%s: use the --help option for usage information\n"),
1220 program_name);
1221 ::exit(EXIT_FAILURE);
1222 }
1223
1224 void
1225 Command_line::usage(const char* msg, const char *opt)
1226 {
1227 fprintf(stderr,
1228 _("%s: %s: %s\n"),
1229 program_name, opt, msg);
1230 this->usage();
1231 }
1232
1233 void
1234 Command_line::usage(const char* msg, char opt)
1235 {
1236 fprintf(stderr,
1237 _("%s: -%c: %s\n"),
1238 program_name, opt, msg);
1239 this->usage();
1240 }
1241
1242 } // End namespace gold.
This page took 0.059688 seconds and 5 git commands to generate.