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