1 // parameters.cc -- general parameters for a link using gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
29 #include "target-select.h"
34 // Our local version of the variable, which is not const.
36 static Parameters static_parameters;
38 // The global variable.
40 const Parameters* parameters = &static_parameters;
42 // A helper class to set the target once.
44 class Set_parameters_target_once : public Once
47 Set_parameters_target_once(Parameters* parameters)
48 : parameters_(parameters)
53 do_run_once(void* arg)
54 { this->parameters_->set_target_once(static_cast<Target*>(arg)); }
57 Parameters* parameters_;
60 // We only need one Set_parameters_target_once.
63 Set_parameters_target_once set_parameters_target_once(&static_parameters);
67 Parameters::Parameters()
68 : errors_(NULL), timer_(NULL), options_(NULL), target_(NULL),
69 doing_static_link_valid_(false), doing_static_link_(false),
70 debug_(0), incremental_mode_(General_options::INCREMENTAL_OFF),
71 set_parameters_target_once_(&set_parameters_target_once)
76 Parameters::set_errors(Errors* errors)
78 gold_assert(this->errors_ == NULL);
79 this->errors_ = errors;
83 Parameters::set_timer(Timer* timer)
85 gold_assert(this->timer_ == NULL);
90 Parameters::set_options(const General_options* options)
92 gold_assert(!this->options_valid());
93 this->options_ = options;
94 // For speed, we convert the options() debug var from a string to an
95 // enum (from debug.h).
96 this->debug_ = debug_string_to_enum(this->options().debug());
97 // Set incremental_mode_ based on the value of the --incremental option.
98 // We copy the mode into parameters because it can change based on inputs.
99 this->incremental_mode_ = this->options().incremental_mode();
100 // If --verbose is set, it acts as "--debug=files".
101 if (options->verbose())
102 this->debug_ |= DEBUG_FILES;
103 if (this->target_valid())
104 this->check_target_endianness();
108 Parameters::set_doing_static_link(bool doing_static_link)
110 gold_assert(!this->doing_static_link_valid_);
111 this->doing_static_link_ = doing_static_link;
112 this->doing_static_link_valid_ = true;
116 Parameters::set_target(Target* target)
118 this->set_parameters_target_once_->run_once(static_cast<void*>(target));
119 gold_assert(target == this->target_);
122 // This is called at most once.
125 Parameters::set_target_once(Target* target)
127 gold_assert(this->target_ == NULL);
128 this->target_ = target;
129 if (this->options_valid())
131 this->check_target_endianness();
132 this->check_rodata_segment();
136 // Clear the target, for testing.
139 Parameters::clear_target()
141 this->target_ = NULL;
142 // We need a new Set_parameters_target_once so that we can set the
144 this->set_parameters_target_once_ = new Set_parameters_target_once(this);
147 // Return whether TARGET is compatible with the target we are using.
150 Parameters::is_compatible_target(const Target* target) const
152 if (this->target_ == NULL)
154 return target == this->target_;
157 Parameters::Target_size_endianness
158 Parameters::size_and_endianness() const
160 if (this->target().get_size() == 32)
162 if (!this->target().is_big_endian())
164 #ifdef HAVE_TARGET_32_LITTLE
165 return TARGET_32_LITTLE;
172 #ifdef HAVE_TARGET_32_BIG
173 return TARGET_32_BIG;
179 else if (parameters->target().get_size() == 64)
181 if (!parameters->target().is_big_endian())
183 #ifdef HAVE_TARGET_64_LITTLE
184 return TARGET_64_LITTLE;
191 #ifdef HAVE_TARGET_64_BIG
192 return TARGET_64_BIG;
202 // If output endianness is specified in command line, check that it does
203 // not conflict with the target.
206 Parameters::check_target_endianness()
208 General_options::Endianness endianness = this->options().endianness();
209 if (endianness != General_options::ENDIANNESS_NOT_SET)
212 if (endianness == General_options::ENDIANNESS_BIG)
216 gold_assert(endianness == General_options::ENDIANNESS_LITTLE);
220 if (this->target().is_big_endian() != big_endian)
221 gold_error(_("input file does not match -EB/EL option"));
226 Parameters::check_rodata_segment()
228 if (this->options().user_set_Trodata_segment()
229 && !this->options().rosegment()
230 && !this->target().isolate_execinstr())
231 gold_error(_("-Trodata-segment is meaningless without --rosegment"));
234 // Return the name of the entry symbol.
237 Parameters::entry() const
239 const char* ret = this->options().entry();
241 ret = parameters->target().entry_symbol_name();
245 // Set the incremental linking mode to INCREMENTAL_FULL. Used when
246 // the linker determines that an incremental update is not possible.
247 // Returns false if the incremental mode was INCREMENTAL_UPDATE,
248 // indicating that the linker should exit if an update is not possible.
251 Parameters::set_incremental_full()
253 gold_assert(this->incremental_mode_ != General_options::INCREMENTAL_OFF);
254 if (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE)
256 this->incremental_mode_ = General_options::INCREMENTAL_FULL;
260 // Return true if we need to prepare incremental linking information.
263 Parameters::incremental() const
265 return this->incremental_mode_ != General_options::INCREMENTAL_OFF;
268 // Return true if we are doing a full incremental link.
271 Parameters::incremental_full() const
273 return this->incremental_mode_ == General_options::INCREMENTAL_FULL;
276 // Return true if we are doing an incremental update.
279 Parameters::incremental_update() const
281 return (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE
282 || this->incremental_mode_ == General_options::INCREMENTAL_AUTO);
286 set_parameters_errors(Errors* errors)
287 { static_parameters.set_errors(errors); }
290 set_parameters_timer(Timer* timer)
291 { static_parameters.set_timer(timer); }
294 set_parameters_options(const General_options* options)
295 { static_parameters.set_options(options); }
298 set_parameters_target(Target* target)
300 static_parameters.set_target(target);
301 target->select_as_default_target();
305 set_parameters_doing_static_link(bool doing_static_link)
306 { static_parameters.set_doing_static_link(doing_static_link); }
308 // Set the incremental linking mode to INCREMENTAL_FULL. Used when
309 // the linker determines that an incremental update is not possible.
310 // Returns false if the incremental mode was INCREMENTAL_UPDATE,
311 // indicating that the linker should exit if an update is not possible.
313 set_parameters_incremental_full()
314 { return static_parameters.set_incremental_full(); }
316 // Force the target to be valid by using the default. Use the
317 // --oformat option is set; this supports the x86_64 kernel build,
318 // which converts a binary file to an object file using -r --format
319 // binary --oformat elf32-i386 foo.o. Otherwise use the configured
323 parameters_force_valid_target()
325 if (parameters->target_valid())
328 gold_assert(parameters->options_valid());
329 if (parameters->options().user_set_oformat())
331 const char* bfd_name = parameters->options().oformat();
332 Target* target = select_target_by_bfd_name(bfd_name);
335 set_parameters_target(target);
339 gold_error(_("unrecognized output format %s"), bfd_name);
342 if (parameters->options().user_set_m())
344 const char* emulation = parameters->options().m();
345 Target* target = select_target_by_emulation(emulation);
348 set_parameters_target(target);
352 gold_error(_("unrecognized emulation %s"), emulation);
355 // The GOLD_DEFAULT_xx macros are defined by the configure script.
357 General_options::Endianness endianness = parameters->options().endianness();
358 if (endianness == General_options::ENDIANNESS_BIG)
359 is_big_endian = true;
360 else if (endianness == General_options::ENDIANNESS_LITTLE)
361 is_big_endian = false;
363 is_big_endian = GOLD_DEFAULT_BIG_ENDIAN;
365 Target* target = select_target(NULL, 0,
366 elfcpp::GOLD_DEFAULT_MACHINE,
369 elfcpp::GOLD_DEFAULT_OSABI,
374 gold_assert(is_big_endian != GOLD_DEFAULT_BIG_ENDIAN);
375 gold_fatal(_("no supported target for -EB/-EL option"));
378 set_parameters_target(target);
381 // Clear the current target, for testing.
384 parameters_clear_target()
386 static_parameters.clear_target();
389 } // End namespace gold.