Commit | Line | Data |
---|---|---|
7e1edb90 ILT |
1 | // parameters.cc -- general parameters for a link using gold |
2 | ||
2e702c99 RM |
3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
4 | // Free Software Foundation, Inc. | |
6cb15b7f ILT |
5 | // Written by Ian Lance Taylor <iant@google.com>. |
6 | ||
7 | // This file is part of gold. | |
8 | ||
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. | |
13 | ||
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. | |
18 | ||
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. | |
23 | ||
7e1edb90 ILT |
24 | #include "gold.h" |
25 | ||
ee1fe73e | 26 | #include "debug.h" |
7e1edb90 | 27 | #include "options.h" |
96803768 | 28 | #include "target.h" |
8851ecca | 29 | #include "target-select.h" |
7e1edb90 ILT |
30 | |
31 | namespace gold | |
32 | { | |
33 | ||
114dfbe1 ILT |
34 | // Our local version of the variable, which is not const. |
35 | ||
36 | static Parameters static_parameters; | |
37 | ||
38 | // The global variable. | |
39 | ||
40 | const Parameters* parameters = &static_parameters; | |
41 | ||
42 | // A helper class to set the target once. | |
43 | ||
44 | class Set_parameters_target_once : public Once | |
45 | { | |
46 | public: | |
47 | Set_parameters_target_once(Parameters* parameters) | |
48 | : parameters_(parameters) | |
49 | { } | |
50 | ||
51 | protected: | |
52 | void | |
53 | do_run_once(void* arg) | |
54 | { this->parameters_->set_target_once(static_cast<Target*>(arg)); } | |
55 | ||
56 | private: | |
57 | Parameters* parameters_; | |
58 | }; | |
59 | ||
60 | // We only need one Set_parameters_target_once. | |
61 | ||
62 | static | |
63 | Set_parameters_target_once set_parameters_target_once(&static_parameters); | |
64 | ||
65 | // Class Parameters. | |
66 | ||
67 | Parameters::Parameters() | |
b490c0bb | 68 | : errors_(NULL), timer_(NULL), options_(NULL), target_(NULL), |
114dfbe1 | 69 | doing_static_link_valid_(false), doing_static_link_(false), |
8c21d9d3 | 70 | debug_(0), incremental_mode_(General_options::INCREMENTAL_OFF), |
114dfbe1 ILT |
71 | set_parameters_target_once_(&set_parameters_target_once) |
72 | { | |
73 | } | |
74 | ||
8851ecca | 75 | void |
2ea97941 | 76 | Parameters::set_errors(Errors* errors) |
7e1edb90 | 77 | { |
8851ecca | 78 | gold_assert(this->errors_ == NULL); |
2ea97941 | 79 | this->errors_ = errors; |
3c2fafa5 ILT |
80 | } |
81 | ||
b490c0bb CC |
82 | void |
83 | Parameters::set_timer(Timer* timer) | |
84 | { | |
85 | gold_assert(this->timer_ == NULL); | |
86 | this->timer_ = timer; | |
87 | } | |
88 | ||
3c2fafa5 | 89 | void |
2ea97941 | 90 | Parameters::set_options(const General_options* options) |
3c2fafa5 | 91 | { |
8851ecca | 92 | gold_assert(!this->options_valid()); |
2ea97941 | 93 | this->options_ = options; |
ee1fe73e ILT |
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()); | |
8c21d9d3 CC |
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(); | |
2285a610 | 100 | // If --verbose is set, it acts as "--debug=files". |
2ea97941 | 101 | if (options->verbose()) |
2285a610 | 102 | this->debug_ |= DEBUG_FILES; |
7296d933 DK |
103 | if (this->target_valid()) |
104 | this->check_target_endianness(); | |
7e1edb90 ILT |
105 | } |
106 | ||
b3b74ddc | 107 | void |
2ea97941 | 108 | Parameters::set_doing_static_link(bool doing_static_link) |
b3b74ddc | 109 | { |
8851ecca | 110 | gold_assert(!this->doing_static_link_valid_); |
2ea97941 | 111 | this->doing_static_link_ = doing_static_link; |
8851ecca | 112 | this->doing_static_link_valid_ = true; |
b3b74ddc ILT |
113 | } |
114 | ||
9025d29d | 115 | void |
2ea97941 | 116 | Parameters::set_target(Target* target) |
9025d29d | 117 | { |
114dfbe1 ILT |
118 | this->set_parameters_target_once_->run_once(static_cast<void*>(target)); |
119 | gold_assert(target == this->target_); | |
120 | } | |
121 | ||
122 | // This is called at most once. | |
123 | ||
124 | void | |
125 | Parameters::set_target_once(Target* target) | |
126 | { | |
127 | gold_assert(this->target_ == NULL); | |
128 | this->target_ = target; | |
7296d933 DK |
129 | if (this->options_valid()) |
130 | this->check_target_endianness(); | |
114dfbe1 ILT |
131 | } |
132 | ||
133 | // Clear the target, for testing. | |
134 | ||
135 | void | |
136 | Parameters::clear_target() | |
137 | { | |
138 | this->target_ = NULL; | |
139 | // We need a new Set_parameters_target_once so that we can set the | |
140 | // target again. | |
141 | this->set_parameters_target_once_ = new Set_parameters_target_once(this); | |
9025d29d ILT |
142 | } |
143 | ||
15f8229b ILT |
144 | // Return whether TARGET is compatible with the target we are using. |
145 | ||
146 | bool | |
2ea97941 | 147 | Parameters::is_compatible_target(const Target* target) const |
15f8229b ILT |
148 | { |
149 | if (this->target_ == NULL) | |
150 | return true; | |
2ea97941 | 151 | return target == this->target_; |
15f8229b ILT |
152 | } |
153 | ||
8851ecca ILT |
154 | Parameters::Target_size_endianness |
155 | Parameters::size_and_endianness() const | |
3c2fafa5 | 156 | { |
8851ecca ILT |
157 | if (this->target().get_size() == 32) |
158 | { | |
159 | if (!this->target().is_big_endian()) | |
160 | { | |
161 | #ifdef HAVE_TARGET_32_LITTLE | |
162 | return TARGET_32_LITTLE; | |
163 | #else | |
164 | gold_unreachable(); | |
165 | #endif | |
166 | } | |
167 | else | |
168 | { | |
169 | #ifdef HAVE_TARGET_32_BIG | |
170 | return TARGET_32_BIG; | |
171 | #else | |
172 | gold_unreachable(); | |
173 | #endif | |
174 | } | |
175 | } | |
176 | else if (parameters->target().get_size() == 64) | |
177 | { | |
178 | if (!parameters->target().is_big_endian()) | |
179 | { | |
180 | #ifdef HAVE_TARGET_64_LITTLE | |
181 | return TARGET_64_LITTLE; | |
182 | #else | |
183 | gold_unreachable(); | |
184 | #endif | |
185 | } | |
186 | else | |
187 | { | |
188 | #ifdef HAVE_TARGET_64_BIG | |
189 | return TARGET_64_BIG; | |
190 | #else | |
191 | gold_unreachable(); | |
192 | #endif | |
193 | } | |
194 | } | |
195 | else | |
196 | gold_unreachable(); | |
3c2fafa5 ILT |
197 | } |
198 | ||
7296d933 DK |
199 | // If output endianness is specified in command line, check that it does |
200 | // not conflict with the target. | |
201 | ||
202 | void | |
203 | Parameters::check_target_endianness() | |
204 | { | |
205 | General_options::Endianness endianness = this->options().endianness(); | |
206 | if (endianness != General_options::ENDIANNESS_NOT_SET) | |
207 | { | |
208 | bool big_endian; | |
209 | if (endianness == General_options::ENDIANNESS_BIG) | |
210 | big_endian = true; | |
211 | else | |
212 | { | |
213 | gold_assert(endianness == General_options::ENDIANNESS_LITTLE); | |
214 | big_endian = false;; | |
215 | } | |
2e702c99 | 216 | |
7296d933 DK |
217 | if (this->target().is_big_endian() != big_endian) |
218 | gold_error(_("input file does not match -EB/EL option")); | |
219 | } | |
220 | } | |
221 | ||
a10ae760 ILT |
222 | // Return the name of the entry symbol. |
223 | ||
224 | const char* | |
225 | Parameters::entry() const | |
226 | { | |
227 | const char* ret = this->options().entry(); | |
228 | if (ret == NULL) | |
229 | { | |
230 | // FIXME: Need to support target specific entry symbol. | |
231 | ret = "_start"; | |
232 | } | |
233 | return ret; | |
234 | } | |
235 | ||
8c21d9d3 CC |
236 | // Set the incremental linking mode to INCREMENTAL_FULL. Used when |
237 | // the linker determines that an incremental update is not possible. | |
238 | // Returns false if the incremental mode was INCREMENTAL_UPDATE, | |
239 | // indicating that the linker should exit if an update is not possible. | |
240 | ||
241 | bool | |
242 | Parameters::set_incremental_full() | |
243 | { | |
244 | gold_assert(this->incremental_mode_ != General_options::INCREMENTAL_OFF); | |
245 | if (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE) | |
246 | return false; | |
247 | this->incremental_mode_ = General_options::INCREMENTAL_FULL; | |
248 | return true; | |
249 | } | |
250 | ||
251 | // Return true if we need to prepare incremental linking information. | |
252 | ||
253 | bool | |
254 | Parameters::incremental() const | |
255 | { | |
256 | return this->incremental_mode_ != General_options::INCREMENTAL_OFF; | |
257 | } | |
258 | ||
9fbd3822 CC |
259 | // Return true if we are doing a full incremental link. |
260 | ||
261 | bool | |
262 | Parameters::incremental_full() const | |
263 | { | |
264 | return this->incremental_mode_ == General_options::INCREMENTAL_FULL; | |
265 | } | |
266 | ||
8c21d9d3 CC |
267 | // Return true if we are doing an incremental update. |
268 | ||
269 | bool | |
270 | Parameters::incremental_update() const | |
271 | { | |
272 | return (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE | |
273 | || this->incremental_mode_ == General_options::INCREMENTAL_AUTO); | |
274 | } | |
275 | ||
b3b74ddc | 276 | void |
8851ecca ILT |
277 | set_parameters_errors(Errors* errors) |
278 | { static_parameters.set_errors(errors); } | |
b3b74ddc | 279 | |
b490c0bb CC |
280 | void |
281 | set_parameters_timer(Timer* timer) | |
282 | { static_parameters.set_timer(timer); } | |
283 | ||
8851ecca ILT |
284 | void |
285 | set_parameters_options(const General_options* options) | |
286 | { static_parameters.set_options(options); } | |
b3b74ddc | 287 | |
9025d29d | 288 | void |
029ba973 | 289 | set_parameters_target(Target* target) |
0d31c79d DK |
290 | { |
291 | static_parameters.set_target(target); | |
292 | target->select_as_default_target(); | |
293 | } | |
8851ecca ILT |
294 | |
295 | void | |
296 | set_parameters_doing_static_link(bool doing_static_link) | |
297 | { static_parameters.set_doing_static_link(doing_static_link); } | |
7e1edb90 | 298 | |
8c21d9d3 CC |
299 | // Set the incremental linking mode to INCREMENTAL_FULL. Used when |
300 | // the linker determines that an incremental update is not possible. | |
301 | // Returns false if the incremental mode was INCREMENTAL_UPDATE, | |
302 | // indicating that the linker should exit if an update is not possible. | |
303 | bool | |
304 | set_parameters_incremental_full() | |
305 | { return static_parameters.set_incremental_full(); } | |
306 | ||
029ba973 ILT |
307 | // Force the target to be valid by using the default. Use the |
308 | // --oformat option is set; this supports the x86_64 kernel build, | |
309 | // which converts a binary file to an object file using -r --format | |
310 | // binary --oformat elf32-i386 foo.o. Otherwise use the configured | |
311 | // default. | |
312 | ||
313 | void | |
314 | parameters_force_valid_target() | |
315 | { | |
316 | if (parameters->target_valid()) | |
317 | return; | |
318 | ||
319 | gold_assert(parameters->options_valid()); | |
320 | if (parameters->options().user_set_oformat()) | |
321 | { | |
03ef7571 ILT |
322 | const char* bfd_name = parameters->options().oformat(); |
323 | Target* target = select_target_by_bfd_name(bfd_name); | |
029ba973 ILT |
324 | if (target != NULL) |
325 | { | |
326 | set_parameters_target(target); | |
327 | return; | |
328 | } | |
329 | ||
03ef7571 ILT |
330 | gold_error(_("unrecognized output format %s"), bfd_name); |
331 | } | |
332 | ||
333 | if (parameters->options().user_set_m()) | |
334 | { | |
335 | const char* emulation = parameters->options().m(); | |
336 | Target* target = select_target_by_emulation(emulation); | |
337 | if (target != NULL) | |
338 | { | |
339 | set_parameters_target(target); | |
340 | return; | |
341 | } | |
342 | ||
343 | gold_error(_("unrecognized emulation %s"), emulation); | |
029ba973 ILT |
344 | } |
345 | ||
346 | // The GOLD_DEFAULT_xx macros are defined by the configure script. | |
7296d933 DK |
347 | bool is_big_endian; |
348 | General_options::Endianness endianness = parameters->options().endianness(); | |
349 | if (endianness == General_options::ENDIANNESS_BIG) | |
350 | is_big_endian = true; | |
351 | else if (endianness == General_options::ENDIANNESS_LITTLE) | |
352 | is_big_endian = false; | |
353 | else | |
354 | is_big_endian = GOLD_DEFAULT_BIG_ENDIAN; | |
355 | ||
2e702c99 RM |
356 | Target* target = select_target(NULL, 0, |
357 | elfcpp::GOLD_DEFAULT_MACHINE, | |
029ba973 | 358 | GOLD_DEFAULT_SIZE, |
7296d933 | 359 | is_big_endian, |
029ba973 ILT |
360 | elfcpp::GOLD_DEFAULT_OSABI, |
361 | 0); | |
f1ddb600 ILT |
362 | |
363 | if (target == NULL) | |
364 | { | |
365 | gold_assert(is_big_endian != GOLD_DEFAULT_BIG_ENDIAN); | |
366 | gold_fatal(_("no supported target for -EB/-EL option")); | |
367 | } | |
368 | ||
029ba973 ILT |
369 | set_parameters_target(target); |
370 | } | |
371 | ||
372 | // Clear the current target, for testing. | |
373 | ||
374 | void | |
375 | parameters_clear_target() | |
376 | { | |
377 | static_parameters.clear_target(); | |
378 | } | |
379 | ||
7e1edb90 | 380 | } // End namespace gold. |