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