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