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