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