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