*** empty log message ***
[deliverable/binutils-gdb.git] / gold / options.cc
CommitLineData
bae7f79e
ILT
1// options.c -- handle command line options for gold
2
e5756efb 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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
ad2d6943
ILT
23#include "gold.h"
24
a2b1aa12 25#include <cstdlib>
bae7f79e 26#include <iostream>
ad2d6943
ILT
27#include <sys/stat.h>
28#include "filenames.h"
29#include "libiberty.h"
bae7f79e 30
c7912668 31#include "debug.h"
e5756efb 32#include "script.h"
bae7f79e
ILT
33#include "options.h"
34
61ba1cf9
ILT
35namespace gold
36{
37
bae7f79e
ILT
38// The information we keep for a single command line option.
39
61ba1cf9 40struct options::One_option
bae7f79e
ILT
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.
3c2fafa5
ILT
75 int (*special)(int argc, char** argv, char *arg, bool long_option,
76 Command_line*);
bae7f79e
ILT
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.
61ba1cf9 80 void (General_options::*general_noarg)();
bae7f79e
ILT
81
82 // If this is a position independent function which takes an
83 // argument, this is the member function to call to record it.
61ba1cf9 84 void (General_options::*general_arg)(const char*);
bae7f79e
ILT
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.
61ba1cf9 88 void (Position_dependent_options::*dependent_noarg)();
bae7f79e
ILT
89
90 // If this is a position dependent option which takes an argument,
91 // this is the member function to record it.
61ba1cf9 92 void (Position_dependent_options::*dependent_arg)(const char*);
bae7f79e
ILT
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
35cdfc9a
ILT
100// We have a separate table for -z options.
101
102struct 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
c7912668
ILT
111// We have a separate table for --debug options.
112
113struct 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
61ba1cf9 122class options::Command_line_options
bae7f79e
ILT
123{
124 public:
125 static const One_option options[];
126 static const int options_size;
35cdfc9a
ILT
127 static const One_z_option z_options[];
128 static const int z_options_size;
c7912668
ILT
129 static const One_debug_option debug_options[];
130 static const int debug_options_size;
bae7f79e
ILT
131};
132
61ba1cf9
ILT
133} // End namespace gold.
134
bae7f79e
ILT
135namespace
136{
137
61ba1cf9
ILT
138// Handle the special -l option, which adds an input file.
139
140int
3c2fafa5
ILT
141library(int argc, char** argv, char* arg, bool long_option,
142 gold::Command_line* cmdline)
61ba1cf9 143{
3c2fafa5
ILT
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
149int
150invoke_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))
09124467
ILT
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
164int
165invoke_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);
3c2fafa5 175 return ret;
61ba1cf9
ILT
176}
177
ead1e424
ILT
178// Handle the special --start-group option.
179
180int
3c2fafa5 181start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
ead1e424
ILT
182{
183 cmdline->start_group(arg);
184 return 1;
185}
186
187// Handle the special --end-group option.
188
189int
3c2fafa5 190end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
ead1e424
ILT
191{
192 cmdline->end_group(arg);
193 return 1;
194}
195
bae7f79e
ILT
196// Report usage information for ld --help, and exit.
197
198int
3c2fafa5 199help(int, char**, char*, bool, gold::Command_line*)
bae7f79e
ILT
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);
a6badf5a 227 comma = true;
bae7f79e
ILT
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;
a6badf5a 240 comma = true;
bae7f79e
ILT
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);
a6badf5a 262 comma = true;
bae7f79e
ILT
263 }
264 }
265 ++j;
266 }
267 while (j < options_size && options[j].doc == NULL);
268
a6badf5a 269 if (len >= 30)
bae7f79e
ILT
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
c7912668 280 ::exit(EXIT_SUCCESS);
bae7f79e
ILT
281
282 return 0;
283}
284
8486ee48
ILT
285// Report version information.
286
287int
3c2fafa5 288version(int, char**, char* opt, bool, gold::Command_line*)
8486ee48
ILT
289{
290 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
c7912668 291 ::exit(EXIT_SUCCESS);
8486ee48
ILT
292 return 0;
293}
294
ad2d6943
ILT
295// If the default sysroot is relocatable, try relocating it based on
296// the prefix FROM.
297
298char*
299get_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
317std::string
318get_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
61ba1cf9
ILT
340} // End anonymous namespace.
341
342namespace gold
343{
bae7f79e
ILT
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) \
61ba1cf9 351 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
352 NULL, func, NULL, NULL, NULL }
353#define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
61ba1cf9 354 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
355 NULL, NULL, func, NULL, NULL }
356#define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
61ba1cf9 357 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
358 NULL, NULL, NULL, func, NULL }
359#define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
61ba1cf9 360 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
361 NULL, NULL, NULL, NULL, func }
362#define SPECIAL(short_option, long_option, doc, help, dash, func) \
61ba1cf9 363 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
364 func, NULL, NULL, NULL, NULL }
365
366// Here is the actual list of options which we accept.
367
61ba1cf9
ILT
368const options::One_option
369options::Command_line_options::options[] =
bae7f79e 370{
e2827e5f
ILT
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),
0c5e9c22
ILT
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),
61611222
ILT
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),
51b08ebe
ILT
393 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
394 NULL, ONE_DASH, &General_options::set_symbolic),
9a0910c3
ILT
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,
bc2c67ff 405 &General_options::set_compress_debug_sections),
e5756efb
ILT
406 GENERAL_ARG('\0', "defsym", N_("Define a symbol"),
407 N_("--defsym SYMBOL=EXPRESSION"), TWO_DASHES,
408 &General_options::define_symbol),
a2b1aa12
ILT
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),
a55ce7fe
ILT
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),
d391083d
ILT
417 GENERAL_ARG('e', "entry", N_("Set program start address"),
418 N_("-e ADDRESS, --entry ADDRESS"), TWO_DASHES,
419 &General_options::set_entry),
a6badf5a
ILT
420 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
421 NULL, TWO_DASHES, &General_options::set_export_dynamic),
0c5e9c22
ILT
422 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
423 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
fced7afd 424 GENERAL_ARG('h', "soname", N_("Set shared library name"),
d391083d 425 N_("-h FILENAME, -soname FILENAME"), ONE_DASH,
fced7afd 426 &General_options::set_soname),
dbe717ef
ILT
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),
0c5e9c22
ILT
430 SPECIAL('l', "library", N_("Search for library LIBNAME"),
431 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
432 &library),
bae7f79e
ILT
433 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
434 N_("-L DIR, --library-path DIR"), TWO_DASHES,
61ba1cf9 435 &General_options::add_to_search_path),
652ec9bd
ILT
436 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
437 &General_options::ignore),
61ba1cf9
ILT
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),
0c5e9c22
ILT
441 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
442 N_("-O level"), ONE_DASH,
443 &General_options::set_optimization_level),
bae7f79e 444 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
61ba1cf9 445 ONE_DASH, &General_options::set_relocatable),
15b3cfae 446 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
41f542e7
ILT
447 N_("-R DIR, -rpath DIR"), ONE_DASH,
448 &General_options::add_to_rpath),
15b3cfae
ILT
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),
0c5e9c22
ILT
453 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
454 TWO_DASHES, &General_options::set_strip_all),
02d2ba74
ILT
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.
0c5e9c22
ILT
460 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
461 TWO_DASHES, &General_options::set_strip_debug),
92e059d8
ILT
462 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
463 NULL, ONE_DASH, &General_options::set_shared),
bae7f79e 464 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
61ba1cf9 465 NULL, ONE_DASH, &General_options::set_static),
e44fcf3b
ILT
466 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
467 NULL, TWO_DASHES, &General_options::set_stats),
ad2d6943
ILT
468 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
469 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
0c5e9c22
ILT
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),
a0451b38
ILT
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),
09124467
ILT
477 SPECIAL('\0', "version-script", N_("Read version script"),
478 N_("--version-script FILE"), TWO_DASHES,
479 &invoke_version_script),
fe9a4c12
ILT
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),
4973341a
ILT
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),
35cdfc9a
ILT
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
0c5e9c22
ILT
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),
bae7f79e 519 SPECIAL('\0', "help", N_("Report usage information"), NULL,
8486ee48
ILT
520 TWO_DASHES, &help),
521 SPECIAL('v', "version", N_("Report version information"), NULL,
c7912668
ILT
522 TWO_DASHES, &version),
523 GENERAL_ARG('\0', "debug", N_("Turn on debugging (all,task)"),
524 N_("--debug=TYPE"), TWO_DASHES,
525 &General_options::handle_debug_option)
bae7f79e
ILT
526};
527
61ba1cf9 528const int options::Command_line_options::options_size =
bae7f79e
ILT
529 sizeof (options) / sizeof (options[0]);
530
35cdfc9a
ILT
531// The -z options.
532
533const options::One_z_option
534options::Command_line_options::z_options[] =
535{
536 { "execstack", &General_options::set_execstack },
537 { "noexecstack", &General_options::set_noexecstack },
538};
539
540const int options::Command_line_options::z_options_size =
541 sizeof(z_options) / sizeof(z_options[0]);
542
c7912668
ILT
543// The --debug options.
544
545const options::One_debug_option
546options::Command_line_options::debug_options[] =
547{
548 { "all", DEBUG_ALL },
549 { "task", DEBUG_TASK },
550};
551
552const int options::Command_line_options::debug_options_size =
553 sizeof(debug_options) / sizeof(debug_options[0]);
554
bae7f79e
ILT
555// The default values for the general options.
556
e5756efb
ILT
557General_options::General_options(Script_options* script_options)
558 : export_dynamic_(false),
fced7afd 559 soname_(NULL),
a6badf5a 560 dynamic_linker_(NULL),
dbe717ef 561 search_path_(),
ca3a67a5 562 optimization_level_(0),
61ba1cf9
ILT
563 output_file_name_("a.out"),
564 is_relocatable_(false),
9e2dcb77 565 strip_(STRIP_NONE),
9a2d6984 566 allow_shlib_undefined_(false),
51b08ebe 567 symbolic_(false),
9a0910c3 568 compress_debug_sections_(NO_COMPRESSION),
a55ce7fe 569 detect_odr_violations_(false),
7da52175 570 create_eh_frame_hdr_(false),
4973341a 571 rpath_(),
15b3cfae 572 rpath_link_(),
92e059d8 573 is_shared_(false),
ad2d6943 574 is_static_(false),
e44fcf3b 575 print_stats_(false),
0c5e9c22 576 sysroot_(),
fe9a4c12
ILT
577 text_segment_address_(-1U), // -1 indicates value not set by user
578 threads_(false),
579 thread_count_initial_(0),
580 thread_count_middle_(0),
35cdfc9a 581 thread_count_final_(0),
c7912668 582 execstack_(EXECSTACK_FROM_INPUT),
e5756efb
ILT
583 debug_(0),
584 script_options_(script_options)
bae7f79e 585{
a2b1aa12
ILT
586 // We initialize demangle_ based on the environment variable
587 // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
588 // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
589 // environment. Acting the same way here lets us provide the same
590 // interface by default.
591 this->demangle_ = getenv("COLLECT_NO_DEMANGLE") == NULL;
bae7f79e
ILT
592}
593
e5756efb 594// Handle the --defsym option.
bae7f79e 595
e5756efb
ILT
596void
597General_options::define_symbol(const char* arg)
bae7f79e 598{
e5756efb 599 this->script_options_->define_symbol(arg);
bae7f79e
ILT
600}
601
35cdfc9a
ILT
602// Handle the -z option.
603
604void
605General_options::handle_z_option(const char* arg)
606{
607 const int z_options_size = options::Command_line_options::z_options_size;
608 const gold::options::One_z_option* z_options =
609 gold::options::Command_line_options::z_options;
610 for (int i = 0; i < z_options_size; ++i)
611 {
612 if (strcmp(arg, z_options[i].name) == 0)
613 {
614 (this->*(z_options[i].set))();
615 return;
616 }
617 }
618
619 fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
620 program_name, arg);
c7912668
ILT
621 ::exit(EXIT_FAILURE);
622}
623
624// Handle the --debug option.
625
626void
627General_options::handle_debug_option(const char* arg)
628{
629 const int debug_options_size =
630 options::Command_line_options::debug_options_size;
631 const gold::options::One_debug_option* debug_options =
632 options::Command_line_options::debug_options;
633 for (int i = 0; i < debug_options_size; ++i)
634 {
635 if (strcmp(arg, debug_options[i].name) == 0)
636 {
637 this->set_debug(debug_options[i].debug_flags);
638 return;
639 }
640 }
641
642 fprintf(stderr, _("%s: unrecognized --debug subcommand: %s\n"),
643 program_name, arg);
644 ::exit(EXIT_FAILURE);
35cdfc9a
ILT
645}
646
ad2d6943
ILT
647// Add the sysroot, if any, to the search paths.
648
649void
650General_options::add_sysroot()
651{
652 if (this->sysroot_.empty())
653 {
654 this->sysroot_ = get_default_sysroot();
655 if (this->sysroot_.empty())
656 return;
657 }
658
659 const char* sysroot = this->sysroot_.c_str();
660 char* canonical_sysroot = lrealpath(sysroot);
661
662 for (Dir_list::iterator p = this->search_path_.begin();
663 p != this->search_path_.end();
664 ++p)
665 p->add_sysroot(sysroot, canonical_sysroot);
666
667 free(canonical_sysroot);
668}
669
e5756efb
ILT
670// The default values for the position dependent options.
671
672Position_dependent_options::Position_dependent_options()
673 : do_static_search_(false),
674 as_needed_(false),
675 include_whole_archive_(false)
676{
677}
678
ad2d6943
ILT
679// Search_directory methods.
680
681// This is called if we have a sysroot. Apply the sysroot if
682// appropriate. Record whether the directory is in the sysroot.
683
684void
685Search_directory::add_sysroot(const char* sysroot,
686 const char* canonical_sysroot)
687{
688 gold_assert(*sysroot != '\0');
689 if (this->put_in_sysroot_)
690 {
691 if (!IS_DIR_SEPARATOR(this->name_[0])
692 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
693 this->name_ = '/' + this->name_;
694 this->name_ = sysroot + this->name_;
695 this->is_in_sysroot_ = true;
696 }
697 else
698 {
699 // Check whether this entry is in the sysroot. To do this
700 // correctly, we need to use canonical names. Otherwise we will
701 // get confused by the ../../.. paths that gcc tends to use.
702 char* canonical_name = lrealpath(this->name_.c_str());
703 int canonical_name_len = strlen(canonical_name);
704 int canonical_sysroot_len = strlen(canonical_sysroot);
705 if (canonical_name_len > canonical_sysroot_len
706 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
707 {
708 canonical_name[canonical_sysroot_len] = '\0';
709 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
710 this->is_in_sysroot_ = true;
711 }
712 free(canonical_name);
713 }
714}
715
dbe717ef
ILT
716// Input_arguments methods.
717
718// Add a file to the list.
719
720void
721Input_arguments::add_file(const Input_file_argument& file)
722{
723 if (!this->in_group_)
724 this->input_argument_list_.push_back(Input_argument(file));
725 else
726 {
a3ad94ed
ILT
727 gold_assert(!this->input_argument_list_.empty());
728 gold_assert(this->input_argument_list_.back().is_group());
dbe717ef
ILT
729 this->input_argument_list_.back().group()->add_file(file);
730 }
731}
732
733// Start a group.
734
735void
736Input_arguments::start_group()
737{
a3ad94ed 738 gold_assert(!this->in_group_);
dbe717ef
ILT
739 Input_file_group* group = new Input_file_group();
740 this->input_argument_list_.push_back(Input_argument(group));
741 this->in_group_ = true;
742}
743
744// End a group.
745
746void
747Input_arguments::end_group()
748{
a3ad94ed 749 gold_assert(this->in_group_);
dbe717ef
ILT
750 this->in_group_ = false;
751}
752
ead1e424 753// Command_line options.
bae7f79e 754
e5756efb
ILT
755Command_line::Command_line(Script_options* script_options)
756 : options_(script_options), position_options_(), inputs_()
bae7f79e
ILT
757{
758}
759
a0451b38
ILT
760// Process the command line options. For process_one_option,
761// i is the index of argv to process next, and the return value
762// is the index of the next option to process (i+1 or i+2, or argc
763// to indicate processing is done). no_more_options is set to true
764// if (and when) "--" is seen as an option.
bae7f79e 765
a0451b38
ILT
766int
767Command_line::process_one_option(int argc, char** argv, int i,
768 bool* no_more_options)
bae7f79e 769{
61ba1cf9 770 const int options_size = options::Command_line_options::options_size;
a0451b38
ILT
771 const options::One_option* options = options::Command_line_options::options;
772 gold_assert(i < argc);
773
774 if (argv[i][0] != '-' || *no_more_options)
bae7f79e 775 {
a0451b38
ILT
776 this->add_file(argv[i], false);
777 return i + 1;
778 }
bae7f79e 779
a0451b38
ILT
780 // Option starting with '-'.
781 int dashes = 1;
782 if (argv[i][1] == '-')
783 {
784 dashes = 2;
785 if (argv[i][2] == '\0')
786 {
787 *no_more_options = true;
788 return i + 1;
789 }
790 }
bae7f79e 791
a0451b38
ILT
792 // Look for a long option match.
793 char* opt = argv[i] + dashes;
794 char first = opt[0];
795 int skiparg = 0;
796 char* arg = strchr(opt, '=');
797 bool argument_with_equals = arg != NULL;
798 if (arg != NULL)
799 {
800 *arg = '\0';
801 ++arg;
802 }
803 else if (i + 1 < argc)
804 {
805 arg = argv[i + 1];
806 skiparg = 1;
807 }
bae7f79e 808
a0451b38
ILT
809 int j;
810 for (j = 0; j < options_size; ++j)
811 {
812 if (options[j].long_option != NULL
813 && (dashes == 2
814 || (options[j].dash
815 != options::One_option::EXACTLY_TWO_DASHES))
816 && first == options[j].long_option[0]
817 && strcmp(opt, options[j].long_option) == 0)
818 {
819 if (options[j].special)
820 {
821 // Restore the '=' we clobbered above.
822 if (arg != NULL && skiparg == 0)
823 arg[-1] = '=';
824 i += options[j].special(argc - i, argv + i, opt, true, this);
825 }
826 else
827 {
828 if (!options[j].takes_argument())
829 {
830 if (argument_with_equals)
831 this->usage(_("unexpected argument"), argv[i]);
832 arg = NULL;
833 skiparg = 0;
834 }
835 else
836 {
837 if (arg == NULL)
838 this->usage(_("missing argument"), argv[i]);
839 }
840 this->apply_option(options[j], arg);
841 i += skiparg + 1;
842 }
843 break;
844 }
845 }
846 if (j < options_size)
847 return i;
848
849 // If we saw two dashes, we needed to have seen a long option.
850 if (dashes == 2)
851 this->usage(_("unknown option"), argv[i]);
852
853 // Look for a short option match. There may be more than one
854 // short option in a given argument.
855 bool done = false;
856 char* s = argv[i] + 1;
857 ++i;
858 while (*s != '\0' && !done)
859 {
860 char opt = *s;
bae7f79e
ILT
861 int j;
862 for (j = 0; j < options_size; ++j)
a0451b38
ILT
863 {
864 if (options[j].short_option == opt)
865 {
866 if (options[j].special)
867 {
868 // Undo the argument skip done above.
869 --i;
870 i += options[j].special(argc - i, argv + i, s, false,
871 this);
872 done = true;
873 }
874 else
875 {
876 arg = NULL;
877 if (options[j].takes_argument())
878 {
879 if (s[1] != '\0')
880 {
881 arg = s + 1;
882 done = true;
883 }
884 else if (i < argc)
885 {
886 arg = argv[i];
887 ++i;
888 }
889 else
890 this->usage(_("missing argument"), opt);
891 }
892 this->apply_option(options[j], arg);
893 }
894 break;
895 }
896 }
897
898 if (j >= options_size)
899 this->usage(_("unknown option"), *s);
900
901 ++s;
902 }
903 return i;
904}
bae7f79e 905
bae7f79e 906
a0451b38
ILT
907void
908Command_line::process(int argc, char** argv)
909{
910 bool no_more_options = false;
911 int i = 0;
912 while (i < argc)
913 i = process_one_option(argc, argv, i, &no_more_options);
61ba1cf9 914
dbe717ef 915 if (this->inputs_.in_group())
ead1e424 916 {
35cdfc9a 917 fprintf(stderr, _("%s: missing group end\n"), program_name);
ead1e424
ILT
918 this->usage();
919 }
920
61ba1cf9 921 // FIXME: We should only do this when configured in native mode.
ad2d6943
ILT
922 this->options_.add_to_search_path_with_sysroot("/lib");
923 this->options_.add_to_search_path_with_sysroot("/usr/lib");
924
925 this->options_.add_sysroot();
46738c9a
ILT
926
927 // Ensure options don't contradict each other and are otherwise kosher.
928 this->normalize_options();
929}
930
3c2fafa5
ILT
931// Extract an option argument for a special option. LONGNAME is the
932// long name of the option. This sets *PRET to the return value for
933// the special function handler to skip to the next option.
934
935const char*
936Command_line::get_special_argument(const char* longname, int argc, char** argv,
937 const char* arg, bool long_option,
938 int *pret)
939{
940 if (long_option)
941 {
942 size_t longlen = strlen(longname);
943 gold_assert(strncmp(arg, longname, longlen) == 0);
944 arg += longlen;
945 if (*arg == '=')
946 {
947 *pret = 1;
948 return arg + 1;
949 }
950 else if (argc > 1)
951 {
952 gold_assert(*arg == '\0');
953 *pret = 2;
954 return argv[1];
955 }
956 }
957 else
958 {
959 if (arg[1] != '\0')
960 {
961 *pret = 1;
962 return arg + 1;
963 }
964 else if (argc > 1)
965 {
966 *pret = 2;
967 return argv[1];
968 }
969 }
970
971 this->usage(_("missing argument"), arg);
972}
973
46738c9a
ILT
974// Ensure options don't contradict each other and are otherwise kosher.
975
976void
977Command_line::normalize_options()
978{
979 // If the user specifies both -s and -r, convert the -s as -S.
980 // -r requires us to keep externally visible symbols!
981 if (this->options_.strip_all() && this->options_.is_relocatable())
982 {
983 // Clears the strip_all() status, replacing it with strip_debug().
984 this->options_.set_strip_debug();
985 }
986
987 // FIXME: we can/should be doing a lot more sanity checking here.
bae7f79e
ILT
988}
989
46738c9a 990
bae7f79e
ILT
991// Apply a command line option.
992
993void
61ba1cf9
ILT
994Command_line::apply_option(const options::One_option& opt,
995 const char* arg)
bae7f79e
ILT
996{
997 if (arg == NULL)
998 {
999 if (opt.general_noarg)
1000 (this->options_.*(opt.general_noarg))();
1001 else if (opt.dependent_noarg)
1002 (this->position_options_.*(opt.dependent_noarg))();
1003 else
61ba1cf9 1004 gold_unreachable();
bae7f79e
ILT
1005 }
1006 else
1007 {
1008 if (opt.general_arg)
1009 (this->options_.*(opt.general_arg))(arg);
1010 else if (opt.dependent_arg)
1011 (this->position_options_.*(opt.dependent_arg))(arg);
1012 else
61ba1cf9 1013 gold_unreachable();
bae7f79e
ILT
1014 }
1015}
1016
ead1e424
ILT
1017// Add an input file or library.
1018
1019void
1020Command_line::add_file(const char* name, bool is_lib)
1021{
51dee2fe 1022 Input_file_argument file(name, is_lib, "", this->position_options_);
dbe717ef 1023 this->inputs_.add_file(file);
ead1e424
ILT
1024}
1025
61ba1cf9
ILT
1026// Handle the -l option, which requires special treatment.
1027
1028int
3c2fafa5
ILT
1029Command_line::process_l_option(int argc, char** argv, char* arg,
1030 bool long_option)
61ba1cf9
ILT
1031{
1032 int ret;
3c2fafa5
ILT
1033 const char* libname = this->get_special_argument("library", argc, argv, arg,
1034 long_option, &ret);
ead1e424 1035 this->add_file(libname, true);
61ba1cf9
ILT
1036 return ret;
1037}
1038
ead1e424
ILT
1039// Handle the --start-group option.
1040
1041void
1042Command_line::start_group(const char* arg)
1043{
dbe717ef 1044 if (this->inputs_.in_group())
ead1e424 1045 this->usage(_("may not nest groups"), arg);
dbe717ef 1046 this->inputs_.start_group();
ead1e424
ILT
1047}
1048
1049// Handle the --end-group option.
1050
1051void
1052Command_line::end_group(const char* arg)
1053{
dbe717ef 1054 if (!this->inputs_.in_group())
ead1e424 1055 this->usage(_("group end without group start"), arg);
dbe717ef 1056 this->inputs_.end_group();
ead1e424
ILT
1057}
1058
bae7f79e
ILT
1059// Report a usage error. */
1060
1061void
61ba1cf9 1062Command_line::usage()
bae7f79e
ILT
1063{
1064 fprintf(stderr,
1065 _("%s: use the --help option for usage information\n"),
61ba1cf9 1066 program_name);
c7912668 1067 ::exit(EXIT_FAILURE);
bae7f79e
ILT
1068}
1069
1070void
61ba1cf9 1071Command_line::usage(const char* msg, const char *opt)
bae7f79e
ILT
1072{
1073 fprintf(stderr,
1074 _("%s: %s: %s\n"),
61ba1cf9 1075 program_name, opt, msg);
bae7f79e
ILT
1076 this->usage();
1077}
1078
1079void
61ba1cf9 1080Command_line::usage(const char* msg, char opt)
bae7f79e
ILT
1081{
1082 fprintf(stderr,
1083 _("%s: -%c: %s\n"),
61ba1cf9 1084 program_name, opt, msg);
bae7f79e
ILT
1085 this->usage();
1086}
61ba1cf9
ILT
1087
1088} // End namespace gold.
This page took 0.152715 seconds and 4 git commands to generate.