Commit | Line | Data |
---|---|---|
7e1edb90 ILT |
1 | // parameters.cc -- general parameters for a link using gold |
2 | ||
114dfbe1 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
6cb15b7f ILT |
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 | ||
7e1edb90 ILT |
23 | #include "gold.h" |
24 | ||
ee1fe73e | 25 | #include "debug.h" |
7e1edb90 | 26 | #include "options.h" |
96803768 | 27 | #include "target.h" |
8851ecca | 28 | #include "target-select.h" |
7e1edb90 ILT |
29 | |
30 | namespace gold | |
31 | { | |
32 | ||
114dfbe1 ILT |
33 | // Our local version of the variable, which is not const. |
34 | ||
35 | static Parameters static_parameters; | |
36 | ||
37 | // The global variable. | |
38 | ||
39 | const Parameters* parameters = &static_parameters; | |
40 | ||
41 | // A helper class to set the target once. | |
42 | ||
43 | class Set_parameters_target_once : public Once | |
44 | { | |
45 | public: | |
46 | Set_parameters_target_once(Parameters* parameters) | |
47 | : parameters_(parameters) | |
48 | { } | |
49 | ||
50 | protected: | |
51 | void | |
52 | do_run_once(void* arg) | |
53 | { this->parameters_->set_target_once(static_cast<Target*>(arg)); } | |
54 | ||
55 | private: | |
56 | Parameters* parameters_; | |
57 | }; | |
58 | ||
59 | // We only need one Set_parameters_target_once. | |
60 | ||
61 | static | |
62 | Set_parameters_target_once set_parameters_target_once(&static_parameters); | |
63 | ||
64 | // Class Parameters. | |
65 | ||
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) | |
71 | { | |
72 | } | |
73 | ||
8851ecca | 74 | void |
2ea97941 | 75 | Parameters::set_errors(Errors* errors) |
7e1edb90 | 76 | { |
8851ecca | 77 | gold_assert(this->errors_ == NULL); |
2ea97941 | 78 | this->errors_ = errors; |
3c2fafa5 ILT |
79 | } |
80 | ||
3c2fafa5 | 81 | void |
2ea97941 | 82 | Parameters::set_options(const General_options* options) |
3c2fafa5 | 83 | { |
8851ecca | 84 | gold_assert(!this->options_valid()); |
2ea97941 | 85 | this->options_ = options; |
ee1fe73e ILT |
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()); | |
2285a610 | 89 | // If --verbose is set, it acts as "--debug=files". |
2ea97941 | 90 | if (options->verbose()) |
2285a610 | 91 | this->debug_ |= DEBUG_FILES; |
7296d933 DK |
92 | if (this->target_valid()) |
93 | this->check_target_endianness(); | |
7e1edb90 ILT |
94 | } |
95 | ||
b3b74ddc | 96 | void |
2ea97941 | 97 | Parameters::set_doing_static_link(bool doing_static_link) |
b3b74ddc | 98 | { |
8851ecca | 99 | gold_assert(!this->doing_static_link_valid_); |
2ea97941 | 100 | this->doing_static_link_ = doing_static_link; |
8851ecca | 101 | this->doing_static_link_valid_ = true; |
b3b74ddc ILT |
102 | } |
103 | ||
9025d29d | 104 | void |
2ea97941 | 105 | Parameters::set_target(Target* target) |
9025d29d | 106 | { |
114dfbe1 ILT |
107 | this->set_parameters_target_once_->run_once(static_cast<void*>(target)); |
108 | gold_assert(target == this->target_); | |
109 | } | |
110 | ||
111 | // This is called at most once. | |
112 | ||
113 | void | |
114 | Parameters::set_target_once(Target* target) | |
115 | { | |
116 | gold_assert(this->target_ == NULL); | |
117 | this->target_ = target; | |
7296d933 DK |
118 | if (this->options_valid()) |
119 | this->check_target_endianness(); | |
114dfbe1 ILT |
120 | } |
121 | ||
122 | // Clear the target, for testing. | |
123 | ||
124 | void | |
125 | Parameters::clear_target() | |
126 | { | |
127 | this->target_ = NULL; | |
128 | // We need a new Set_parameters_target_once so that we can set the | |
129 | // target again. | |
130 | this->set_parameters_target_once_ = new Set_parameters_target_once(this); | |
9025d29d ILT |
131 | } |
132 | ||
15f8229b ILT |
133 | // Return whether TARGET is compatible with the target we are using. |
134 | ||
135 | bool | |
2ea97941 | 136 | Parameters::is_compatible_target(const Target* target) const |
15f8229b ILT |
137 | { |
138 | if (this->target_ == NULL) | |
139 | return true; | |
2ea97941 | 140 | return target == this->target_; |
15f8229b ILT |
141 | } |
142 | ||
8851ecca ILT |
143 | Parameters::Target_size_endianness |
144 | Parameters::size_and_endianness() const | |
3c2fafa5 | 145 | { |
8851ecca ILT |
146 | if (this->target().get_size() == 32) |
147 | { | |
148 | if (!this->target().is_big_endian()) | |
149 | { | |
150 | #ifdef HAVE_TARGET_32_LITTLE | |
151 | return TARGET_32_LITTLE; | |
152 | #else | |
153 | gold_unreachable(); | |
154 | #endif | |
155 | } | |
156 | else | |
157 | { | |
158 | #ifdef HAVE_TARGET_32_BIG | |
159 | return TARGET_32_BIG; | |
160 | #else | |
161 | gold_unreachable(); | |
162 | #endif | |
163 | } | |
164 | } | |
165 | else if (parameters->target().get_size() == 64) | |
166 | { | |
167 | if (!parameters->target().is_big_endian()) | |
168 | { | |
169 | #ifdef HAVE_TARGET_64_LITTLE | |
170 | return TARGET_64_LITTLE; | |
171 | #else | |
172 | gold_unreachable(); | |
173 | #endif | |
174 | } | |
175 | else | |
176 | { | |
177 | #ifdef HAVE_TARGET_64_BIG | |
178 | return TARGET_64_BIG; | |
179 | #else | |
180 | gold_unreachable(); | |
181 | #endif | |
182 | } | |
183 | } | |
184 | else | |
185 | gold_unreachable(); | |
3c2fafa5 ILT |
186 | } |
187 | ||
7296d933 DK |
188 | // If output endianness is specified in command line, check that it does |
189 | // not conflict with the target. | |
190 | ||
191 | void | |
192 | Parameters::check_target_endianness() | |
193 | { | |
194 | General_options::Endianness endianness = this->options().endianness(); | |
195 | if (endianness != General_options::ENDIANNESS_NOT_SET) | |
196 | { | |
197 | bool big_endian; | |
198 | if (endianness == General_options::ENDIANNESS_BIG) | |
199 | big_endian = true; | |
200 | else | |
201 | { | |
202 | gold_assert(endianness == General_options::ENDIANNESS_LITTLE); | |
203 | big_endian = false;; | |
204 | } | |
205 | ||
206 | if (this->target().is_big_endian() != big_endian) | |
207 | gold_error(_("input file does not match -EB/EL option")); | |
208 | } | |
209 | } | |
210 | ||
b3b74ddc | 211 | void |
8851ecca ILT |
212 | set_parameters_errors(Errors* errors) |
213 | { static_parameters.set_errors(errors); } | |
b3b74ddc | 214 | |
8851ecca ILT |
215 | void |
216 | set_parameters_options(const General_options* options) | |
217 | { static_parameters.set_options(options); } | |
b3b74ddc | 218 | |
9025d29d | 219 | void |
029ba973 | 220 | set_parameters_target(Target* target) |
0d31c79d DK |
221 | { |
222 | static_parameters.set_target(target); | |
223 | target->select_as_default_target(); | |
224 | } | |
8851ecca ILT |
225 | |
226 | void | |
227 | set_parameters_doing_static_link(bool doing_static_link) | |
228 | { static_parameters.set_doing_static_link(doing_static_link); } | |
7e1edb90 | 229 | |
029ba973 ILT |
230 | // Force the target to be valid by using the default. Use the |
231 | // --oformat option is set; this supports the x86_64 kernel build, | |
232 | // which converts a binary file to an object file using -r --format | |
233 | // binary --oformat elf32-i386 foo.o. Otherwise use the configured | |
234 | // default. | |
235 | ||
236 | void | |
237 | parameters_force_valid_target() | |
238 | { | |
239 | if (parameters->target_valid()) | |
240 | return; | |
241 | ||
242 | gold_assert(parameters->options_valid()); | |
243 | if (parameters->options().user_set_oformat()) | |
244 | { | |
245 | Target* target = select_target_by_name(parameters->options().oformat()); | |
246 | if (target != NULL) | |
247 | { | |
248 | set_parameters_target(target); | |
249 | return; | |
250 | } | |
251 | ||
252 | gold_error(_("unrecognized output format %s"), | |
253 | parameters->options().oformat()); | |
254 | } | |
255 | ||
256 | // The GOLD_DEFAULT_xx macros are defined by the configure script. | |
7296d933 DK |
257 | bool is_big_endian; |
258 | General_options::Endianness endianness = parameters->options().endianness(); | |
259 | if (endianness == General_options::ENDIANNESS_BIG) | |
260 | is_big_endian = true; | |
261 | else if (endianness == General_options::ENDIANNESS_LITTLE) | |
262 | is_big_endian = false; | |
263 | else | |
264 | is_big_endian = GOLD_DEFAULT_BIG_ENDIAN; | |
265 | ||
029ba973 ILT |
266 | Target* target = select_target(elfcpp::GOLD_DEFAULT_MACHINE, |
267 | GOLD_DEFAULT_SIZE, | |
7296d933 | 268 | is_big_endian, |
029ba973 ILT |
269 | elfcpp::GOLD_DEFAULT_OSABI, |
270 | 0); | |
271 | gold_assert(target != NULL); | |
272 | set_parameters_target(target); | |
273 | } | |
274 | ||
275 | // Clear the current target, for testing. | |
276 | ||
277 | void | |
278 | parameters_clear_target() | |
279 | { | |
280 | static_parameters.clear_target(); | |
281 | } | |
282 | ||
7e1edb90 | 283 | } // End namespace gold. |