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