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