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