Implement -Bstatic/-Bdynamic.
[binutils-gdb.git] / gold / options.h
1 // options.h -- handle command line options for gold -*- C++ -*-
2
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
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
35 #include <cstdlib>
36 #include <list>
37 #include <string>
38 #include <vector>
39
40 namespace gold
41 {
42
43 class Command_line;
44 class Input_file_group;
45
46 namespace options {
47
48 class Command_line_options;
49 struct One_option;
50
51 } // End namespace gold::options.
52
53 // A directory to search. For each directory we record whether it is
54 // in the sysroot. We need to know this so that, if a linker script
55 // is found within the sysroot, we will apply the sysroot to any files
56 // named by that script.
57
58 class Search_directory
59 {
60 public:
61 // We need a default constructor because we put this in a
62 // std::vector.
63 Search_directory()
64 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
65 { }
66
67 // This is the usual constructor.
68 Search_directory(const char* name, bool put_in_sysroot)
69 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
70 { gold_assert(!this->name_.empty()); }
71
72 // This is called if we have a sysroot. The sysroot is prefixed to
73 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
74 // set to true for any enries which are in the sysroot (this will
75 // naturally include any entries for which put_in_sysroot_ is true).
76 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
77 // passing SYSROOT to lrealpath.
78 void
79 add_sysroot(const char* sysroot, const char* canonical_sysroot);
80
81 // Get the directory name.
82 const std::string&
83 name() const
84 { return this->name_; }
85
86 // Return whether this directory is in the sysroot.
87 bool
88 is_in_sysroot() const
89 { return this->is_in_sysroot_; }
90
91 private:
92 std::string name_;
93 bool put_in_sysroot_;
94 bool is_in_sysroot_;
95 };
96
97 // The position independent options which apply to the whole link.
98 // There are a lot of them.
99
100 class General_options
101 {
102 public:
103 General_options();
104
105 // -E: export dynamic symbols.
106 bool
107 export_dynamic() const
108 { return this->export_dynamic_; }
109
110 // -I: dynamic linker name.
111 const char*
112 dynamic_linker() const
113 { return this->dynamic_linker_; }
114
115 // -L: Library search path.
116 typedef std::vector<Search_directory> Dir_list;
117
118 const Dir_list&
119 search_path() const
120 { return this->search_path_; }
121
122 // -O: optimization level (0: don't try to optimize output size).
123 int
124 optimization_level() const
125 { return this->optimization_level_; }
126
127 // -o: Output file name.
128 const char*
129 output_file_name() const
130 { return this->output_file_name_; }
131
132 // -r: Whether we are doing a relocatable link.
133 bool
134 is_relocatable() const
135 { return this->is_relocatable_; }
136
137 // -s: Strip all symbols.
138 bool
139 strip_all() const
140 { return this->strip_ == STRIP_ALL; }
141
142 // -S: Strip debugging information.
143 bool
144 strip_debug() const
145 { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
146
147 // -Bsymbolic: bind defined symbols locally.
148 bool
149 symbolic() const
150 { return this->symbolic_; }
151
152 // --eh-frame-hdr: Whether to generate an exception frame header.
153 bool
154 create_eh_frame_hdr() const
155 { return this->create_eh_frame_hdr_; }
156
157 // --rpath: The runtime search path.
158 const Dir_list&
159 rpath() const
160 { return this->rpath_; }
161
162 // --rpath-link: The link time search patch for shared libraries.
163 const Dir_list&
164 rpath_link() const
165 { return this->rpath_link_; }
166
167 // --shared: Whether generating a shared object.
168 bool
169 is_shared() const
170 { return this->is_shared_; }
171
172 // --static: Whether doing a static link.
173 bool
174 is_static() const
175 { return this->is_static_; }
176
177 // --stats: Print resource usage statistics.
178 bool
179 print_stats() const
180 { return this->print_stats_; }
181
182 // --sysroot: The system root of a cross-linker.
183 const std::string&
184 sysroot() const
185 { return this->sysroot_; }
186
187 // -Ttext: The address of the .text section
188 uint64_t
189 text_segment_address() const
190 { return this->text_segment_address_; }
191
192 // Whether -Ttext was used.
193 bool
194 user_set_text_segment_address() const
195 { return this->text_segment_address_ != -1U; }
196
197 // --threads: Whether to use threads.
198 bool
199 threads() const
200 { return this->threads_; }
201
202 // --thread-count-initial: Threads to use in initial pass.
203 int
204 thread_count_initial() const
205 { return this->thread_count_initial_; }
206
207 // --thread-count-middle: Threads to use in middle pass.
208 int
209 thread_count_middle() const
210 { return this->thread_count_middle_; }
211
212 // --thread-count-final: Threads to use in final pass.
213 int
214 thread_count_final() const
215 { return this->thread_count_final_; }
216
217 private:
218 // Don't copy this structure.
219 General_options(const General_options&);
220 General_options& operator=(const General_options&);
221
222 friend class Command_line;
223 friend class options::Command_line_options;
224
225 // Which symbols to strip.
226 enum Strip
227 {
228 // Don't strip any symbols.
229 STRIP_NONE,
230 // Strip all symbols.
231 STRIP_ALL,
232 // Strip debugging information.
233 STRIP_DEBUG
234 };
235
236 void
237 set_export_dynamic()
238 { this->export_dynamic_ = true; }
239
240 void
241 set_dynamic_linker(const char* arg)
242 { this->dynamic_linker_ = arg; }
243
244 void
245 add_to_search_path(const char* arg)
246 { this->search_path_.push_back(Search_directory(arg, false)); }
247
248 void
249 add_to_search_path_with_sysroot(const char* arg)
250 { this->search_path_.push_back(Search_directory(arg, true)); }
251
252 void
253 set_optimization_level(const char* arg)
254 { this->optimization_level_ = atoi(arg); }
255
256 void
257 set_output_file_name(const char* arg)
258 { this->output_file_name_ = arg; }
259
260 void
261 set_relocatable()
262 { this->is_relocatable_ = true; }
263
264 void
265 set_strip_all()
266 { this->strip_ = STRIP_ALL; }
267
268 // Note: normalize_options() depends on the fact that this turns off
269 // STRIP_ALL if it were already set.
270 void
271 set_strip_debug()
272 { this->strip_ = STRIP_DEBUG; }
273
274 void
275 set_symbolic()
276 { this->symbolic_ = true; }
277
278 void
279 set_create_eh_frame_hdr()
280 { this->create_eh_frame_hdr_ = true; }
281
282 void
283 add_to_rpath(const char* arg)
284 { this->rpath_.push_back(Search_directory(arg, false)); }
285
286 void
287 add_to_rpath_link(const char* arg)
288 { this->rpath_link_.push_back(Search_directory(arg, false)); }
289
290 void
291 set_shared()
292 { this->is_shared_ = true; }
293
294 void
295 set_static()
296 { this->is_static_ = true; }
297
298 void
299 set_stats()
300 { this->print_stats_ = true; }
301
302 void
303 set_sysroot(const char* arg)
304 { this->sysroot_ = arg; }
305
306 void
307 set_text_segment_address(const char* arg)
308 {
309 char* endptr;
310 this->text_segment_address_ = strtoull(arg, &endptr, 0);
311 if (*endptr != '\0'
312 || this->text_segment_address_ == -1U)
313 {
314 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
315 program_name, arg);
316 ::exit(1);
317 }
318 }
319
320 int
321 parse_thread_count(const char* arg)
322 {
323 char* endptr;
324 int count = strtol(arg, &endptr, 0);
325 if (*endptr != '\0' || count < 0)
326 {
327 fprintf(stderr, _("%s: invalid thread count: %s\n"),
328 program_name, arg);
329 ::exit(1);
330 }
331 return count;
332 }
333
334 void
335 set_threads()
336 { this->threads_ = true; }
337
338 void
339 clear_threads()
340 { this->threads_ = false; }
341
342 void
343 set_thread_count(const char* arg)
344 {
345 int count = this->parse_thread_count(arg);
346 this->thread_count_initial_ = count;
347 this->thread_count_middle_ = count;
348 this->thread_count_final_ = count;
349 }
350
351 void
352 set_thread_count_initial(const char* arg)
353 { this->thread_count_initial_ = this->parse_thread_count(arg); }
354
355 void
356 set_thread_count_middle(const char* arg)
357 { this->thread_count_initial_ = this->parse_thread_count(arg); }
358
359 void
360 set_thread_count_final(const char* arg)
361 { this->thread_count_initial_ = this->parse_thread_count(arg); }
362
363 void
364 ignore(const char*)
365 { }
366
367 // Apply any sysroot to the directory lists.
368 void
369 add_sysroot();
370
371 bool export_dynamic_;
372 const char* dynamic_linker_;
373 Dir_list search_path_;
374 int optimization_level_;
375 const char* output_file_name_;
376 bool is_relocatable_;
377 Strip strip_;
378 bool symbolic_;
379 bool create_eh_frame_hdr_;
380 Dir_list rpath_;
381 Dir_list rpath_link_;
382 bool is_shared_;
383 bool is_static_;
384 bool print_stats_;
385 std::string sysroot_;
386 uint64_t text_segment_address_;
387 bool threads_;
388 int thread_count_initial_;
389 int thread_count_middle_;
390 int thread_count_final_;
391 };
392
393 // The current state of the position dependent options.
394
395 class Position_dependent_options
396 {
397 public:
398 Position_dependent_options();
399
400 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
401 // -rather than a shared object.
402 bool
403 do_static_search() const
404 { return this->do_static_search_; }
405
406 // --as-needed: Whether to add a DT_NEEDED argument only if the
407 // dynamic object is used.
408 bool
409 as_needed() const
410 { return this->as_needed_; }
411
412 // --whole-archive: Whether to include the entire contents of an
413 // --archive.
414 bool
415 include_whole_archive() const
416 { return this->include_whole_archive_; }
417
418 void
419 set_static_search()
420 { this->do_static_search_ = true; }
421
422 void
423 set_dynamic_search()
424 { this->do_static_search_ = false; }
425
426 void
427 set_as_needed()
428 { this->as_needed_ = true; }
429
430 void
431 clear_as_needed()
432 { this->as_needed_ = false; }
433
434 void
435 set_whole_archive()
436 { this->include_whole_archive_ = true; }
437
438 void
439 clear_whole_archive()
440 { this->include_whole_archive_ = false; }
441
442 private:
443 bool do_static_search_;
444 bool as_needed_;
445 bool include_whole_archive_;
446 };
447
448 // A single file or library argument from the command line.
449
450 class Input_file_argument
451 {
452 public:
453 // name: file name or library name
454 // is_lib: true if name is a library name: that is, emits the leading
455 // "lib" and trailing ".so"/".a" from the name
456 // extra_search_path: an extra directory to look for the file, prior
457 // to checking the normal library search path. If this is "",
458 // then no extra directory is added.
459 // options: The position dependent options at this point in the
460 // command line, such as --whole-archive.
461 Input_file_argument()
462 : name_(), is_lib_(false), extra_search_path_(""), options_()
463 { }
464
465 Input_file_argument(const char* name, bool is_lib,
466 const char* extra_search_path,
467 const Position_dependent_options& options)
468 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
469 options_(options)
470 { }
471
472 const char*
473 name() const
474 { return this->name_.c_str(); }
475
476 const Position_dependent_options&
477 options() const
478 { return this->options_; }
479
480 bool
481 is_lib() const
482 { return this->is_lib_; }
483
484 const char*
485 extra_search_path() const
486 {
487 return (this->extra_search_path_.empty()
488 ? NULL
489 : this->extra_search_path_.c_str());
490 }
491
492 // Return whether this file may require a search using the -L
493 // options.
494 bool
495 may_need_search() const
496 { return this->is_lib_ || !this->extra_search_path_.empty(); }
497
498 private:
499 // We use std::string, not const char*, here for convenience when
500 // using script files, so that we do not have to preserve the string
501 // in that case.
502 std::string name_;
503 bool is_lib_;
504 std::string extra_search_path_;
505 Position_dependent_options options_;
506 };
507
508 // A file or library, or a group, from the command line.
509
510 class Input_argument
511 {
512 public:
513 // Create a file or library argument.
514 explicit Input_argument(Input_file_argument file)
515 : is_file_(true), file_(file), group_(NULL)
516 { }
517
518 // Create a group argument.
519 explicit Input_argument(Input_file_group* group)
520 : is_file_(false), group_(group)
521 { }
522
523 // Return whether this is a file.
524 bool
525 is_file() const
526 { return this->is_file_; }
527
528 // Return whether this is a group.
529 bool
530 is_group() const
531 { return !this->is_file_; }
532
533 // Return the information about the file.
534 const Input_file_argument&
535 file() const
536 {
537 gold_assert(this->is_file_);
538 return this->file_;
539 }
540
541 // Return the information about the group.
542 const Input_file_group*
543 group() const
544 {
545 gold_assert(!this->is_file_);
546 return this->group_;
547 }
548
549 Input_file_group*
550 group()
551 {
552 gold_assert(!this->is_file_);
553 return this->group_;
554 }
555
556 private:
557 bool is_file_;
558 Input_file_argument file_;
559 Input_file_group* group_;
560 };
561
562 // A group from the command line. This is a set of arguments within
563 // --start-group ... --end-group.
564
565 class Input_file_group
566 {
567 public:
568 typedef std::vector<Input_argument> Files;
569 typedef Files::const_iterator const_iterator;
570
571 Input_file_group()
572 : files_()
573 { }
574
575 // Add a file to the end of the group.
576 void
577 add_file(const Input_file_argument& arg)
578 { this->files_.push_back(Input_argument(arg)); }
579
580 // Iterators to iterate over the group contents.
581
582 const_iterator
583 begin() const
584 { return this->files_.begin(); }
585
586 const_iterator
587 end() const
588 { return this->files_.end(); }
589
590 private:
591 Files files_;
592 };
593
594 // A list of files from the command line or a script.
595
596 class Input_arguments
597 {
598 public:
599 typedef std::vector<Input_argument> Input_argument_list;
600 typedef Input_argument_list::const_iterator const_iterator;
601
602 Input_arguments()
603 : input_argument_list_(), in_group_(false)
604 { }
605
606 // Add a file.
607 void
608 add_file(const Input_file_argument& arg);
609
610 // Start a group (the --start-group option).
611 void
612 start_group();
613
614 // End a group (the --end-group option).
615 void
616 end_group();
617
618 // Return whether we are currently in a group.
619 bool
620 in_group() const
621 { return this->in_group_; }
622
623 // The number of entries in the list.
624 int
625 size() const
626 { return this->input_argument_list_.size(); }
627
628 // Iterators to iterate over the list of input files.
629
630 const_iterator
631 begin() const
632 { return this->input_argument_list_.begin(); }
633
634 const_iterator
635 end() const
636 { return this->input_argument_list_.end(); }
637
638 // Return whether the list is empty.
639 bool
640 empty() const
641 { return this->input_argument_list_.empty(); }
642
643 private:
644 Input_argument_list input_argument_list_;
645 bool in_group_;
646 };
647
648 // All the information read from the command line.
649
650 class Command_line
651 {
652 public:
653 typedef Input_arguments::const_iterator const_iterator;
654
655 Command_line();
656
657 // Process the command line options. This will exit with an
658 // appropriate error message if an unrecognized option is seen.
659 void
660 process(int argc, char** argv);
661
662 // Handle a -l option.
663 int
664 process_l_option(int, char**, char*);
665
666 // Handle a --start-group option.
667 void
668 start_group(const char* arg);
669
670 // Handle a --end-group option.
671 void
672 end_group(const char* arg);
673
674 // Get the general options.
675 const General_options&
676 options() const
677 { return this->options_; }
678
679 // The number of input files.
680 int
681 number_of_input_files() const
682 { return this->inputs_.size(); }
683
684 // Iterators to iterate over the list of input files.
685
686 const_iterator
687 begin() const
688 { return this->inputs_.begin(); }
689
690 const_iterator
691 end() const
692 { return this->inputs_.end(); }
693
694 private:
695 Command_line(const Command_line&);
696 Command_line& operator=(const Command_line&);
697
698 // Report usage error.
699 void
700 usage() ATTRIBUTE_NORETURN;
701 void
702 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
703 void
704 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
705
706 // Apply a command line option.
707 void
708 apply_option(const gold::options::One_option&, const char*);
709
710 // Add a file.
711 void
712 add_file(const char* name, bool is_lib);
713
714 // Examine the result of processing the command-line, and verify
715 // the flags do not contradict each other or are otherwise illegal.
716 void
717 normalize_options();
718
719 General_options options_;
720 Position_dependent_options position_options_;
721 Input_arguments inputs_;
722 };
723
724 } // End namespace gold.
725
726 #endif // !defined(GOLD_OPTIONS_H)