2010-04-05 Kai Tietz <kai.tietz@onevision.com>
[binutils.git] / gold / parameters.cc
blob1d0f082e197d1ef3644ece068c50c1a1cc414edf
1 // parameters.cc -- general parameters for a link using gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include "debug.h"
26 #include "options.h"
27 #include "target.h"
28 #include "target-select.h"
30 namespace gold
33 // Our local version of the variable, which is not const.
35 static Parameters static_parameters;
37 // The global variable.
39 const Parameters* parameters = &static_parameters;
41 // A helper class to set the target once.
43 class Set_parameters_target_once : public Once
45 public:
46 Set_parameters_target_once(Parameters* parameters)
47 : parameters_(parameters)
48 { }
50 protected:
51 void
52 do_run_once(void* arg)
53 { this->parameters_->set_target_once(static_cast<Target*>(arg)); }
55 private:
56 Parameters* parameters_;
59 // We only need one Set_parameters_target_once.
61 static
62 Set_parameters_target_once set_parameters_target_once(&static_parameters);
64 // Class Parameters.
66 Parameters::Parameters()
67 : errors_(NULL), options_(NULL), target_(NULL),
68 doing_static_link_valid_(false), doing_static_link_(false),
69 debug_(0),
70 set_parameters_target_once_(&set_parameters_target_once)
74 void
75 Parameters::set_errors(Errors* errors)
77 gold_assert(this->errors_ == NULL);
78 this->errors_ = errors;
81 void
82 Parameters::set_options(const General_options* options)
84 gold_assert(!this->options_valid());
85 this->options_ = options;
86 // For speed, we convert the options() debug var from a string to an
87 // enum (from debug.h).
88 this->debug_ = debug_string_to_enum(this->options().debug());
89 // If --verbose is set, it acts as "--debug=files".
90 if (options->verbose())
91 this->debug_ |= DEBUG_FILES;
94 void
95 Parameters::set_doing_static_link(bool doing_static_link)
97 gold_assert(!this->doing_static_link_valid_);
98 this->doing_static_link_ = doing_static_link;
99 this->doing_static_link_valid_ = true;
102 void
103 Parameters::set_target(Target* target)
105 this->set_parameters_target_once_->run_once(static_cast<void*>(target));
106 gold_assert(target == this->target_);
109 // This is called at most once.
111 void
112 Parameters::set_target_once(Target* target)
114 gold_assert(this->target_ == NULL);
115 this->target_ = target;
118 // Clear the target, for testing.
120 void
121 Parameters::clear_target()
123 this->target_ = NULL;
124 // We need a new Set_parameters_target_once so that we can set the
125 // target again.
126 this->set_parameters_target_once_ = new Set_parameters_target_once(this);
129 // Return whether TARGET is compatible with the target we are using.
131 bool
132 Parameters::is_compatible_target(const Target* target) const
134 if (this->target_ == NULL)
135 return true;
136 return target == this->target_;
139 Parameters::Target_size_endianness
140 Parameters::size_and_endianness() const
142 if (this->target().get_size() == 32)
144 if (!this->target().is_big_endian())
146 #ifdef HAVE_TARGET_32_LITTLE
147 return TARGET_32_LITTLE;
148 #else
149 gold_unreachable();
150 #endif
152 else
154 #ifdef HAVE_TARGET_32_BIG
155 return TARGET_32_BIG;
156 #else
157 gold_unreachable();
158 #endif
161 else if (parameters->target().get_size() == 64)
163 if (!parameters->target().is_big_endian())
165 #ifdef HAVE_TARGET_64_LITTLE
166 return TARGET_64_LITTLE;
167 #else
168 gold_unreachable();
169 #endif
171 else
173 #ifdef HAVE_TARGET_64_BIG
174 return TARGET_64_BIG;
175 #else
176 gold_unreachable();
177 #endif
180 else
181 gold_unreachable();
184 void
185 set_parameters_errors(Errors* errors)
186 { static_parameters.set_errors(errors); }
188 void
189 set_parameters_options(const General_options* options)
190 { static_parameters.set_options(options); }
192 void
193 set_parameters_target(Target* target)
195 static_parameters.set_target(target);
196 target->select_as_default_target();
199 void
200 set_parameters_doing_static_link(bool doing_static_link)
201 { static_parameters.set_doing_static_link(doing_static_link); }
203 // Force the target to be valid by using the default. Use the
204 // --oformat option is set; this supports the x86_64 kernel build,
205 // which converts a binary file to an object file using -r --format
206 // binary --oformat elf32-i386 foo.o. Otherwise use the configured
207 // default.
209 void
210 parameters_force_valid_target()
212 if (parameters->target_valid())
213 return;
215 gold_assert(parameters->options_valid());
216 if (parameters->options().user_set_oformat())
218 Target* target = select_target_by_name(parameters->options().oformat());
219 if (target != NULL)
221 set_parameters_target(target);
222 return;
225 gold_error(_("unrecognized output format %s"),
226 parameters->options().oformat());
229 // The GOLD_DEFAULT_xx macros are defined by the configure script.
230 Target* target = select_target(elfcpp::GOLD_DEFAULT_MACHINE,
231 GOLD_DEFAULT_SIZE,
232 GOLD_DEFAULT_BIG_ENDIAN,
233 elfcpp::GOLD_DEFAULT_OSABI,
235 gold_assert(target != NULL);
236 set_parameters_target(target);
239 // Clear the current target, for testing.
241 void
242 parameters_clear_target()
244 static_parameters.clear_target();
247 } // End namespace gold.