Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.h -- handle command line options for gold -*- C++ -*- |
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 | ||
bae7f79e ILT |
23 | // Command_line |
24 | // Holds everything we get from the command line. | |
25 | // General_options (from Command_line::options()) | |
26 | // Options which are not position dependent. | |
27 | // Input_argument (from Command_line::inputs()) | |
28 | // The list of input files, including -l options. | |
29 | // Position_dependent_options (from Input_argument::options()) | |
30 | // Position dependent options which apply to this argument. | |
31 | ||
32 | #ifndef GOLD_OPTIONS_H | |
33 | #define GOLD_OPTIONS_H | |
34 | ||
ca3a67a5 | 35 | #include <cstdlib> |
bae7f79e | 36 | #include <list> |
61ba1cf9 | 37 | #include <string> |
92e059d8 | 38 | #include <vector> |
bae7f79e | 39 | |
3c2fafa5 ILT |
40 | #include "script.h" |
41 | ||
bae7f79e ILT |
42 | namespace gold |
43 | { | |
44 | ||
45 | class Command_line; | |
ead1e424 | 46 | class Input_file_group; |
3c2fafa5 | 47 | class Position_dependent_options; |
bae7f79e | 48 | |
c7912668 ILT |
49 | namespace options |
50 | { | |
bae7f79e ILT |
51 | |
52 | class Command_line_options; | |
53 | struct One_option; | |
35cdfc9a | 54 | struct One_z_option; |
c7912668 | 55 | struct One_debug_option; |
bae7f79e ILT |
56 | |
57 | } // End namespace gold::options. | |
58 | ||
ad2d6943 ILT |
59 | // A directory to search. For each directory we record whether it is |
60 | // in the sysroot. We need to know this so that, if a linker script | |
61 | // is found within the sysroot, we will apply the sysroot to any files | |
62 | // named by that script. | |
63 | ||
64 | class Search_directory | |
65 | { | |
66 | public: | |
67 | // We need a default constructor because we put this in a | |
68 | // std::vector. | |
69 | Search_directory() | |
70 | : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false) | |
71 | { } | |
72 | ||
73 | // This is the usual constructor. | |
74 | Search_directory(const char* name, bool put_in_sysroot) | |
75 | : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false) | |
76 | { gold_assert(!this->name_.empty()); } | |
77 | ||
78 | // This is called if we have a sysroot. The sysroot is prefixed to | |
79 | // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is | |
80 | // set to true for any enries which are in the sysroot (this will | |
81 | // naturally include any entries for which put_in_sysroot_ is true). | |
82 | // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of | |
83 | // passing SYSROOT to lrealpath. | |
84 | void | |
85 | add_sysroot(const char* sysroot, const char* canonical_sysroot); | |
86 | ||
87 | // Get the directory name. | |
88 | const std::string& | |
89 | name() const | |
90 | { return this->name_; } | |
91 | ||
92 | // Return whether this directory is in the sysroot. | |
93 | bool | |
94 | is_in_sysroot() const | |
95 | { return this->is_in_sysroot_; } | |
96 | ||
97 | private: | |
98 | std::string name_; | |
99 | bool put_in_sysroot_; | |
100 | bool is_in_sysroot_; | |
101 | }; | |
102 | ||
bae7f79e ILT |
103 | // The position independent options which apply to the whole link. |
104 | // There are a lot of them. | |
105 | ||
106 | class General_options | |
107 | { | |
108 | public: | |
109 | General_options(); | |
110 | ||
a6badf5a ILT |
111 | // -E: export dynamic symbols. |
112 | bool | |
113 | export_dynamic() const | |
114 | { return this->export_dynamic_; } | |
115 | ||
fced7afd ILT |
116 | // -h: shared library name. |
117 | const char* | |
118 | soname() const | |
119 | { return this->soname_; } | |
120 | ||
dbe717ef ILT |
121 | // -I: dynamic linker name. |
122 | const char* | |
123 | dynamic_linker() const | |
124 | { return this->dynamic_linker_; } | |
125 | ||
bae7f79e | 126 | // -L: Library search path. |
ad2d6943 | 127 | typedef std::vector<Search_directory> Dir_list; |
bae7f79e ILT |
128 | |
129 | const Dir_list& | |
130 | search_path() const | |
131 | { return this->search_path_; } | |
132 | ||
ca3a67a5 ILT |
133 | // -O: optimization level (0: don't try to optimize output size). |
134 | int | |
135 | optimization_level() const | |
136 | { return this->optimization_level_; } | |
137 | ||
61ba1cf9 ILT |
138 | // -o: Output file name. |
139 | const char* | |
140 | output_file_name() const | |
141 | { return this->output_file_name_; } | |
142 | ||
bae7f79e ILT |
143 | // -r: Whether we are doing a relocatable link. |
144 | bool | |
145 | is_relocatable() const | |
146 | { return this->is_relocatable_; } | |
147 | ||
9e2dcb77 ILT |
148 | // -s: Strip all symbols. |
149 | bool | |
150 | strip_all() const | |
151 | { return this->strip_ == STRIP_ALL; } | |
152 | ||
153 | // -S: Strip debugging information. | |
154 | bool | |
155 | strip_debug() const | |
156 | { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; } | |
157 | ||
9a0910c3 ILT |
158 | // --strip-debug-gdb: strip only debugging information that's not |
159 | // used by gdb (at least, for gdb versions <= 6.7). | |
02d2ba74 ILT |
160 | bool |
161 | strip_debug_gdb() const | |
162 | { return this->strip_debug() || this->strip_ == STRIP_DEBUG_UNUSED_BY_GDB; } | |
163 | ||
e2827e5f ILT |
164 | // --allow-shlib-undefined: do not warn about unresolved symbols in |
165 | // --shared libraries. | |
166 | bool | |
167 | allow_shlib_undefined() const | |
168 | { return this->allow_shlib_undefined_; } | |
169 | ||
51b08ebe ILT |
170 | // -Bsymbolic: bind defined symbols locally. |
171 | bool | |
172 | symbolic() const | |
173 | { return this->symbolic_; } | |
174 | ||
9a0910c3 ILT |
175 | // --compress-debug-sections: compress .debug_* sections in the |
176 | // output file using the given compression method. This is useful | |
177 | // when the tools (such as gdb) support compressed sections. | |
178 | bool | |
179 | compress_debug_sections() const | |
180 | { return this->compress_debug_sections_ != NO_COMPRESSION; } | |
181 | ||
182 | bool | |
183 | zlib_compress_debug_sections() const | |
184 | { return this->compress_debug_sections_ == ZLIB_COMPRESSION; } | |
185 | ||
a2b1aa12 ILT |
186 | // --demangle: demangle C++ symbols in our log messages. |
187 | bool | |
188 | demangle() const | |
189 | { return this->demangle_; } | |
190 | ||
a55ce7fe ILT |
191 | // --detect-odr-violations: Whether to search for One Defn Rule violations. |
192 | bool | |
193 | detect_odr_violations() const | |
194 | { return this->detect_odr_violations_; } | |
195 | ||
7da52175 ILT |
196 | // --eh-frame-hdr: Whether to generate an exception frame header. |
197 | bool | |
198 | create_eh_frame_hdr() const | |
199 | { return this->create_eh_frame_hdr_; } | |
200 | ||
41f542e7 ILT |
201 | // --rpath: The runtime search path. |
202 | const Dir_list& | |
203 | rpath() const | |
204 | { return this->rpath_; } | |
205 | ||
15b3cfae ILT |
206 | // --rpath-link: The link time search patch for shared libraries. |
207 | const Dir_list& | |
208 | rpath_link() const | |
209 | { return this->rpath_link_; } | |
210 | ||
92e059d8 ILT |
211 | // --shared: Whether generating a shared object. |
212 | bool | |
213 | is_shared() const | |
214 | { return this->is_shared_; } | |
215 | ||
bae7f79e ILT |
216 | // --static: Whether doing a static link. |
217 | bool | |
218 | is_static() const | |
219 | { return this->is_static_; } | |
220 | ||
0c5e9c22 | 221 | // --stats: Print resource usage statistics. |
e44fcf3b ILT |
222 | bool |
223 | print_stats() const | |
224 | { return this->print_stats_; } | |
225 | ||
ad2d6943 ILT |
226 | // --sysroot: The system root of a cross-linker. |
227 | const std::string& | |
228 | sysroot() const | |
229 | { return this->sysroot_; } | |
230 | ||
0c5e9c22 ILT |
231 | // -Ttext: The address of the .text section |
232 | uint64_t | |
233 | text_segment_address() const | |
234 | { return this->text_segment_address_; } | |
235 | ||
236 | // Whether -Ttext was used. | |
237 | bool | |
238 | user_set_text_segment_address() const | |
239 | { return this->text_segment_address_ != -1U; } | |
240 | ||
fe9a4c12 ILT |
241 | // --threads: Whether to use threads. |
242 | bool | |
243 | threads() const | |
244 | { return this->threads_; } | |
245 | ||
246 | // --thread-count-initial: Threads to use in initial pass. | |
247 | int | |
248 | thread_count_initial() const | |
249 | { return this->thread_count_initial_; } | |
250 | ||
251 | // --thread-count-middle: Threads to use in middle pass. | |
252 | int | |
253 | thread_count_middle() const | |
254 | { return this->thread_count_middle_; } | |
255 | ||
256 | // --thread-count-final: Threads to use in final pass. | |
257 | int | |
258 | thread_count_final() const | |
259 | { return this->thread_count_final_; } | |
260 | ||
35cdfc9a ILT |
261 | // -z execstack, -z noexecstack |
262 | bool | |
263 | is_execstack_set() const | |
264 | { return this->execstack_ != EXECSTACK_FROM_INPUT; } | |
265 | ||
266 | bool | |
267 | is_stack_executable() const | |
268 | { return this->execstack_ == EXECSTACK_YES; } | |
269 | ||
c7912668 ILT |
270 | // --debug |
271 | unsigned int | |
272 | debug() const | |
273 | { return this->debug_; } | |
274 | ||
bae7f79e | 275 | private: |
dbe717ef ILT |
276 | // Don't copy this structure. |
277 | General_options(const General_options&); | |
278 | General_options& operator=(const General_options&); | |
279 | ||
bae7f79e ILT |
280 | friend class Command_line; |
281 | friend class options::Command_line_options; | |
282 | ||
9e2dcb77 ILT |
283 | // Which symbols to strip. |
284 | enum Strip | |
285 | { | |
286 | // Don't strip any symbols. | |
287 | STRIP_NONE, | |
288 | // Strip all symbols. | |
289 | STRIP_ALL, | |
290 | // Strip debugging information. | |
02d2ba74 ILT |
291 | STRIP_DEBUG, |
292 | // Strip debugging information that's not used by gdb (at least <= 6.7) | |
293 | STRIP_DEBUG_UNUSED_BY_GDB | |
9e2dcb77 ILT |
294 | }; |
295 | ||
35cdfc9a ILT |
296 | // Whether to mark the stack as executable. |
297 | enum Execstack | |
298 | { | |
299 | // Not set on command line. | |
300 | EXECSTACK_FROM_INPUT, | |
301 | // Mark the stack as executable. | |
302 | EXECSTACK_YES, | |
303 | // Mark the stack as not executable. | |
304 | EXECSTACK_NO | |
305 | }; | |
306 | ||
9a0910c3 ILT |
307 | // What compression method to use |
308 | enum CompressionMethod | |
309 | { | |
310 | NO_COMPRESSION, | |
311 | ZLIB_COMPRESSION, | |
312 | }; | |
313 | ||
a6badf5a ILT |
314 | void |
315 | set_export_dynamic() | |
316 | { this->export_dynamic_ = true; } | |
317 | ||
fced7afd ILT |
318 | void |
319 | set_soname(const char* arg) | |
320 | { this->soname_ = arg; } | |
321 | ||
dbe717ef ILT |
322 | void |
323 | set_dynamic_linker(const char* arg) | |
324 | { this->dynamic_linker_ = arg; } | |
325 | ||
bae7f79e ILT |
326 | void |
327 | add_to_search_path(const char* arg) | |
ad2d6943 ILT |
328 | { this->search_path_.push_back(Search_directory(arg, false)); } |
329 | ||
330 | void | |
331 | add_to_search_path_with_sysroot(const char* arg) | |
332 | { this->search_path_.push_back(Search_directory(arg, true)); } | |
bae7f79e | 333 | |
ca3a67a5 ILT |
334 | void |
335 | set_optimization_level(const char* arg) | |
3ae7da37 ILT |
336 | { |
337 | char* endptr; | |
338 | this->optimization_level_ = strtol(arg, &endptr, 0); | |
339 | if (*endptr != '\0' || this->optimization_level_ < 0) | |
340 | gold_fatal(_("invalid optimization level: %s"), arg); | |
341 | } | |
ca3a67a5 | 342 | |
61ba1cf9 ILT |
343 | void |
344 | set_output_file_name(const char* arg) | |
345 | { this->output_file_name_ = arg; } | |
346 | ||
bae7f79e ILT |
347 | void |
348 | set_relocatable() | |
349 | { this->is_relocatable_ = true; } | |
350 | ||
9e2dcb77 ILT |
351 | void |
352 | set_strip_all() | |
353 | { this->strip_ = STRIP_ALL; } | |
354 | ||
46738c9a ILT |
355 | // Note: normalize_options() depends on the fact that this turns off |
356 | // STRIP_ALL if it were already set. | |
9e2dcb77 ILT |
357 | void |
358 | set_strip_debug() | |
359 | { this->strip_ = STRIP_DEBUG; } | |
360 | ||
02d2ba74 ILT |
361 | void |
362 | set_strip_debug_gdb() | |
363 | { this->strip_ = STRIP_DEBUG_UNUSED_BY_GDB; } | |
364 | ||
e2827e5f ILT |
365 | void |
366 | set_allow_shlib_undefined() | |
367 | { this->allow_shlib_undefined_ = true; } | |
368 | ||
369 | void | |
370 | set_no_allow_shlib_undefined() | |
371 | { this->allow_shlib_undefined_ = false; } | |
372 | ||
51b08ebe ILT |
373 | void |
374 | set_symbolic() | |
375 | { this->symbolic_ = true; } | |
376 | ||
bc2c67ff | 377 | void set_compress_debug_sections(const char* arg) |
9a0910c3 ILT |
378 | { |
379 | if (strcmp(arg, "none") == 0) | |
380 | this->compress_debug_sections_ = NO_COMPRESSION; | |
381 | #ifdef HAVE_ZLIB_H | |
382 | else if (strcmp(arg, "zlib") == 0) | |
383 | this->compress_debug_sections_ = ZLIB_COMPRESSION; | |
384 | #endif | |
385 | else | |
bc2c67ff | 386 | gold_fatal(_("unsupported argument to --compress-debug-sections: %s"), |
9a0910c3 ILT |
387 | arg); |
388 | } | |
389 | ||
a2b1aa12 ILT |
390 | void |
391 | set_demangle() | |
392 | { this->demangle_ = true; } | |
393 | ||
394 | void | |
395 | clear_demangle() | |
396 | { this->demangle_ = false; } | |
397 | ||
a55ce7fe ILT |
398 | void |
399 | set_detect_odr_violations() | |
400 | { this->detect_odr_violations_ = true; } | |
401 | ||
7da52175 | 402 | void |
192f9b85 | 403 | set_create_eh_frame_hdr() |
7da52175 ILT |
404 | { this->create_eh_frame_hdr_ = true; } |
405 | ||
41f542e7 ILT |
406 | void |
407 | add_to_rpath(const char* arg) | |
ad2d6943 | 408 | { this->rpath_.push_back(Search_directory(arg, false)); } |
41f542e7 | 409 | |
15b3cfae ILT |
410 | void |
411 | add_to_rpath_link(const char* arg) | |
ad2d6943 | 412 | { this->rpath_link_.push_back(Search_directory(arg, false)); } |
15b3cfae | 413 | |
92e059d8 ILT |
414 | void |
415 | set_shared() | |
416 | { this->is_shared_ = true; } | |
417 | ||
bae7f79e ILT |
418 | void |
419 | set_static() | |
420 | { this->is_static_ = true; } | |
421 | ||
e44fcf3b ILT |
422 | void |
423 | set_stats() | |
424 | { this->print_stats_ = true; } | |
425 | ||
ad2d6943 ILT |
426 | void |
427 | set_sysroot(const char* arg) | |
428 | { this->sysroot_ = arg; } | |
429 | ||
0c5e9c22 ILT |
430 | void |
431 | set_text_segment_address(const char* arg) | |
432 | { | |
433 | char* endptr; | |
434 | this->text_segment_address_ = strtoull(arg, &endptr, 0); | |
435 | if (*endptr != '\0' | |
436 | || this->text_segment_address_ == -1U) | |
3ae7da37 | 437 | gold_fatal(_("invalid argument to -Ttext: %s"), arg); |
0c5e9c22 ILT |
438 | } |
439 | ||
fe9a4c12 ILT |
440 | int |
441 | parse_thread_count(const char* arg) | |
442 | { | |
443 | char* endptr; | |
3ae7da37 | 444 | const int count = strtol(arg, &endptr, 0); |
fe9a4c12 | 445 | if (*endptr != '\0' || count < 0) |
3ae7da37 | 446 | gold_fatal(_("invalid thread count: %s"), arg); |
fe9a4c12 ILT |
447 | return count; |
448 | } | |
449 | ||
450 | void | |
451 | set_threads() | |
3ae7da37 ILT |
452 | { |
453 | #ifndef ENABLE_THREADS | |
454 | gold_fatal(_("--threads not supported")); | |
455 | #endif | |
456 | this->threads_ = true; | |
457 | } | |
fe9a4c12 ILT |
458 | |
459 | void | |
460 | clear_threads() | |
461 | { this->threads_ = false; } | |
462 | ||
463 | void | |
464 | set_thread_count(const char* arg) | |
465 | { | |
466 | int count = this->parse_thread_count(arg); | |
467 | this->thread_count_initial_ = count; | |
468 | this->thread_count_middle_ = count; | |
469 | this->thread_count_final_ = count; | |
470 | } | |
471 | ||
472 | void | |
473 | set_thread_count_initial(const char* arg) | |
474 | { this->thread_count_initial_ = this->parse_thread_count(arg); } | |
475 | ||
476 | void | |
477 | set_thread_count_middle(const char* arg) | |
460c00b5 | 478 | { this->thread_count_middle_ = this->parse_thread_count(arg); } |
fe9a4c12 ILT |
479 | |
480 | void | |
481 | set_thread_count_final(const char* arg) | |
460c00b5 | 482 | { this->thread_count_final_ = this->parse_thread_count(arg); } |
fe9a4c12 | 483 | |
652ec9bd ILT |
484 | void |
485 | ignore(const char*) | |
486 | { } | |
487 | ||
35cdfc9a ILT |
488 | void |
489 | set_execstack() | |
490 | { this->execstack_ = EXECSTACK_YES; } | |
491 | ||
492 | void | |
493 | set_noexecstack() | |
494 | { this->execstack_ = EXECSTACK_NO; } | |
495 | ||
c7912668 ILT |
496 | void |
497 | set_debug(unsigned int flags) | |
498 | { this->debug_ = flags; } | |
499 | ||
35cdfc9a ILT |
500 | // Handle the -z option. |
501 | void | |
502 | handle_z_option(const char*); | |
503 | ||
c7912668 ILT |
504 | // Handle the --debug option. |
505 | void | |
506 | handle_debug_option(const char*); | |
507 | ||
ad2d6943 ILT |
508 | // Apply any sysroot to the directory lists. |
509 | void | |
510 | add_sysroot(); | |
511 | ||
a6badf5a | 512 | bool export_dynamic_; |
fced7afd | 513 | const char* soname_; |
dbe717ef | 514 | const char* dynamic_linker_; |
bae7f79e | 515 | Dir_list search_path_; |
ca3a67a5 | 516 | int optimization_level_; |
61ba1cf9 | 517 | const char* output_file_name_; |
bae7f79e | 518 | bool is_relocatable_; |
9e2dcb77 | 519 | Strip strip_; |
e2827e5f | 520 | bool allow_shlib_undefined_; |
51b08ebe | 521 | bool symbolic_; |
9a0910c3 | 522 | CompressionMethod compress_debug_sections_; |
a2b1aa12 | 523 | bool demangle_; |
a55ce7fe | 524 | bool detect_odr_violations_; |
7da52175 | 525 | bool create_eh_frame_hdr_; |
41f542e7 | 526 | Dir_list rpath_; |
15b3cfae | 527 | Dir_list rpath_link_; |
92e059d8 | 528 | bool is_shared_; |
bae7f79e | 529 | bool is_static_; |
e44fcf3b | 530 | bool print_stats_; |
ad2d6943 | 531 | std::string sysroot_; |
0c5e9c22 | 532 | uint64_t text_segment_address_; |
fe9a4c12 ILT |
533 | bool threads_; |
534 | int thread_count_initial_; | |
535 | int thread_count_middle_; | |
536 | int thread_count_final_; | |
35cdfc9a | 537 | Execstack execstack_; |
c7912668 | 538 | unsigned int debug_; |
bae7f79e ILT |
539 | }; |
540 | ||
541 | // The current state of the position dependent options. | |
542 | ||
543 | class Position_dependent_options | |
544 | { | |
545 | public: | |
546 | Position_dependent_options(); | |
547 | ||
61611222 ILT |
548 | // -Bdynamic/-Bstatic: Whether we are searching for a static archive |
549 | // -rather than a shared object. | |
bae7f79e | 550 | bool |
dbe717ef | 551 | do_static_search() const |
bae7f79e ILT |
552 | { return this->do_static_search_; } |
553 | ||
dbe717ef ILT |
554 | // --as-needed: Whether to add a DT_NEEDED argument only if the |
555 | // dynamic object is used. | |
556 | bool | |
557 | as_needed() const | |
558 | { return this->as_needed_; } | |
bae7f79e | 559 | |
4973341a ILT |
560 | // --whole-archive: Whether to include the entire contents of an |
561 | // --archive. | |
562 | bool | |
563 | include_whole_archive() const | |
564 | { return this->include_whole_archive_; } | |
565 | ||
bae7f79e ILT |
566 | void |
567 | set_static_search() | |
568 | { this->do_static_search_ = true; } | |
569 | ||
570 | void | |
571 | set_dynamic_search() | |
572 | { this->do_static_search_ = false; } | |
573 | ||
dbe717ef ILT |
574 | void |
575 | set_as_needed() | |
576 | { this->as_needed_ = true; } | |
577 | ||
578 | void | |
579 | clear_as_needed() | |
580 | { this->as_needed_ = false; } | |
581 | ||
4973341a ILT |
582 | void |
583 | set_whole_archive() | |
584 | { this->include_whole_archive_ = true; } | |
585 | ||
586 | void | |
587 | clear_whole_archive() | |
588 | { this->include_whole_archive_ = false; } | |
589 | ||
dbe717ef | 590 | private: |
bae7f79e | 591 | bool do_static_search_; |
dbe717ef | 592 | bool as_needed_; |
4973341a | 593 | bool include_whole_archive_; |
bae7f79e ILT |
594 | }; |
595 | ||
596 | // A single file or library argument from the command line. | |
597 | ||
ead1e424 | 598 | class Input_file_argument |
bae7f79e ILT |
599 | { |
600 | public: | |
51dee2fe ILT |
601 | // name: file name or library name |
602 | // is_lib: true if name is a library name: that is, emits the leading | |
603 | // "lib" and trailing ".so"/".a" from the name | |
604 | // extra_search_path: an extra directory to look for the file, prior | |
605 | // to checking the normal library search path. If this is "", | |
606 | // then no extra directory is added. | |
607 | // options: The position dependent options at this point in the | |
ad2d6943 | 608 | // command line, such as --whole-archive. |
ead1e424 | 609 | Input_file_argument() |
51dee2fe | 610 | : name_(), is_lib_(false), extra_search_path_(""), options_() |
ead1e424 ILT |
611 | { } |
612 | ||
613 | Input_file_argument(const char* name, bool is_lib, | |
51dee2fe | 614 | const char* extra_search_path, |
ead1e424 | 615 | const Position_dependent_options& options) |
51dee2fe ILT |
616 | : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path), |
617 | options_(options) | |
bae7f79e ILT |
618 | { } |
619 | ||
620 | const char* | |
621 | name() const | |
dbe717ef | 622 | { return this->name_.c_str(); } |
bae7f79e ILT |
623 | |
624 | const Position_dependent_options& | |
625 | options() const | |
626 | { return this->options_; } | |
627 | ||
628 | bool | |
629 | is_lib() const | |
61ba1cf9 | 630 | { return this->is_lib_; } |
bae7f79e | 631 | |
51dee2fe ILT |
632 | const char* |
633 | extra_search_path() const | |
634 | { | |
635 | return (this->extra_search_path_.empty() | |
636 | ? NULL | |
637 | : this->extra_search_path_.c_str()); | |
638 | } | |
639 | ||
640 | // Return whether this file may require a search using the -L | |
641 | // options. | |
642 | bool | |
643 | may_need_search() const | |
644 | { return this->is_lib_ || !this->extra_search_path_.empty(); } | |
645 | ||
bae7f79e | 646 | private: |
dbe717ef ILT |
647 | // We use std::string, not const char*, here for convenience when |
648 | // using script files, so that we do not have to preserve the string | |
649 | // in that case. | |
650 | std::string name_; | |
61ba1cf9 | 651 | bool is_lib_; |
51dee2fe | 652 | std::string extra_search_path_; |
bae7f79e ILT |
653 | Position_dependent_options options_; |
654 | }; | |
655 | ||
ead1e424 ILT |
656 | // A file or library, or a group, from the command line. |
657 | ||
658 | class Input_argument | |
659 | { | |
660 | public: | |
661 | // Create a file or library argument. | |
662 | explicit Input_argument(Input_file_argument file) | |
663 | : is_file_(true), file_(file), group_(NULL) | |
664 | { } | |
665 | ||
666 | // Create a group argument. | |
667 | explicit Input_argument(Input_file_group* group) | |
668 | : is_file_(false), group_(group) | |
669 | { } | |
670 | ||
671 | // Return whether this is a file. | |
672 | bool | |
673 | is_file() const | |
674 | { return this->is_file_; } | |
675 | ||
676 | // Return whether this is a group. | |
677 | bool | |
678 | is_group() const | |
679 | { return !this->is_file_; } | |
680 | ||
681 | // Return the information about the file. | |
682 | const Input_file_argument& | |
683 | file() const | |
684 | { | |
a3ad94ed | 685 | gold_assert(this->is_file_); |
ead1e424 ILT |
686 | return this->file_; |
687 | } | |
688 | ||
689 | // Return the information about the group. | |
690 | const Input_file_group* | |
691 | group() const | |
692 | { | |
a3ad94ed | 693 | gold_assert(!this->is_file_); |
ead1e424 ILT |
694 | return this->group_; |
695 | } | |
696 | ||
697 | Input_file_group* | |
698 | group() | |
699 | { | |
a3ad94ed | 700 | gold_assert(!this->is_file_); |
ead1e424 ILT |
701 | return this->group_; |
702 | } | |
703 | ||
704 | private: | |
705 | bool is_file_; | |
706 | Input_file_argument file_; | |
707 | Input_file_group* group_; | |
708 | }; | |
709 | ||
710 | // A group from the command line. This is a set of arguments within | |
711 | // --start-group ... --end-group. | |
712 | ||
713 | class Input_file_group | |
92e059d8 | 714 | { |
ead1e424 ILT |
715 | public: |
716 | typedef std::vector<Input_argument> Files; | |
717 | typedef Files::const_iterator const_iterator; | |
718 | ||
719 | Input_file_group() | |
720 | : files_() | |
721 | { } | |
722 | ||
723 | // Add a file to the end of the group. | |
724 | void | |
725 | add_file(const Input_file_argument& arg) | |
726 | { this->files_.push_back(Input_argument(arg)); } | |
727 | ||
728 | // Iterators to iterate over the group contents. | |
729 | ||
730 | const_iterator | |
731 | begin() const | |
732 | { return this->files_.begin(); } | |
733 | ||
734 | const_iterator | |
735 | end() const | |
736 | { return this->files_.end(); } | |
737 | ||
738 | private: | |
739 | Files files_; | |
92e059d8 ILT |
740 | }; |
741 | ||
dbe717ef ILT |
742 | // A list of files from the command line or a script. |
743 | ||
744 | class Input_arguments | |
745 | { | |
746 | public: | |
747 | typedef std::vector<Input_argument> Input_argument_list; | |
748 | typedef Input_argument_list::const_iterator const_iterator; | |
749 | ||
750 | Input_arguments() | |
751 | : input_argument_list_(), in_group_(false) | |
752 | { } | |
753 | ||
754 | // Add a file. | |
755 | void | |
756 | add_file(const Input_file_argument& arg); | |
757 | ||
758 | // Start a group (the --start-group option). | |
759 | void | |
760 | start_group(); | |
761 | ||
762 | // End a group (the --end-group option). | |
763 | void | |
764 | end_group(); | |
765 | ||
766 | // Return whether we are currently in a group. | |
767 | bool | |
768 | in_group() const | |
769 | { return this->in_group_; } | |
770 | ||
fe9a4c12 ILT |
771 | // The number of entries in the list. |
772 | int | |
773 | size() const | |
774 | { return this->input_argument_list_.size(); } | |
775 | ||
dbe717ef ILT |
776 | // Iterators to iterate over the list of input files. |
777 | ||
778 | const_iterator | |
779 | begin() const | |
780 | { return this->input_argument_list_.begin(); } | |
781 | ||
782 | const_iterator | |
783 | end() const | |
784 | { return this->input_argument_list_.end(); } | |
785 | ||
786 | // Return whether the list is empty. | |
787 | bool | |
788 | empty() const | |
789 | { return this->input_argument_list_.empty(); } | |
790 | ||
791 | private: | |
792 | Input_argument_list input_argument_list_; | |
793 | bool in_group_; | |
794 | }; | |
795 | ||
bae7f79e ILT |
796 | // All the information read from the command line. |
797 | ||
798 | class Command_line | |
799 | { | |
800 | public: | |
ead1e424 ILT |
801 | typedef Input_arguments::const_iterator const_iterator; |
802 | ||
bae7f79e ILT |
803 | Command_line(); |
804 | ||
805 | // Process the command line options. This will exit with an | |
806 | // appropriate error message if an unrecognized option is seen. | |
807 | void | |
808 | process(int argc, char** argv); | |
809 | ||
a0451b38 ILT |
810 | // Process one command-line option. This takes the index of argv to |
811 | // process, and returns the index for the next option. | |
812 | int | |
813 | process_one_option(int argc, char** argv, int i, bool* no_more_options); | |
814 | ||
61ba1cf9 ILT |
815 | // Handle a -l option. |
816 | int | |
3c2fafa5 | 817 | process_l_option(int, char**, char*, bool); |
61ba1cf9 | 818 | |
ead1e424 ILT |
819 | // Handle a --start-group option. |
820 | void | |
821 | start_group(const char* arg); | |
822 | ||
823 | // Handle a --end-group option. | |
824 | void | |
825 | end_group(const char* arg); | |
826 | ||
3c2fafa5 ILT |
827 | // Get an option argument--a helper function for special processing. |
828 | const char* | |
829 | get_special_argument(const char* longname, int argc, char** argv, | |
830 | const char* arg, bool long_option, | |
831 | int *pret); | |
832 | ||
61ba1cf9 | 833 | // Get the general options. |
bae7f79e ILT |
834 | const General_options& |
835 | options() const | |
836 | { return this->options_; } | |
837 | ||
3c2fafa5 ILT |
838 | // Get the position dependent options. |
839 | const Position_dependent_options& | |
840 | position_dependent_options() const | |
841 | { return this->position_options_; } | |
842 | ||
fe9a4c12 ILT |
843 | // The number of input files. |
844 | int | |
845 | number_of_input_files() const | |
846 | { return this->inputs_.size(); } | |
847 | ||
ead1e424 ILT |
848 | // Iterators to iterate over the list of input files. |
849 | ||
850 | const_iterator | |
851 | begin() const | |
852 | { return this->inputs_.begin(); } | |
853 | ||
854 | const_iterator | |
855 | end() const | |
856 | { return this->inputs_.end(); } | |
bae7f79e ILT |
857 | |
858 | private: | |
ead1e424 ILT |
859 | Command_line(const Command_line&); |
860 | Command_line& operator=(const Command_line&); | |
861 | ||
862 | // Report usage error. | |
863 | void | |
864 | usage() ATTRIBUTE_NORETURN; | |
865 | void | |
866 | usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN; | |
867 | void | |
868 | usage(const char* msg, char opt) ATTRIBUTE_NORETURN; | |
869 | ||
870 | // Apply a command line option. | |
871 | void | |
872 | apply_option(const gold::options::One_option&, const char*); | |
873 | ||
874 | // Add a file. | |
875 | void | |
876 | add_file(const char* name, bool is_lib); | |
bae7f79e | 877 | |
46738c9a ILT |
878 | // Examine the result of processing the command-line, and verify |
879 | // the flags do not contradict each other or are otherwise illegal. | |
880 | void | |
881 | normalize_options(); | |
882 | ||
bae7f79e ILT |
883 | General_options options_; |
884 | Position_dependent_options position_options_; | |
ead1e424 | 885 | Input_arguments inputs_; |
bae7f79e ILT |
886 | }; |
887 | ||
888 | } // End namespace gold. | |
889 | ||
890 | #endif // !defined(GOLD_OPTIONS_H) |