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