* options.h (class General_options): Add --print-output-format.
[deliverable/binutils-gdb.git] / gold / target-select.cc
1 // target-select.cc -- select a target for an object file
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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
23 #include "gold.h"
24
25 #include <cstdio>
26 #include <cstring>
27
28 #include "elfcpp.h"
29 #include "options.h"
30 #include "parameters.h"
31 #include "target-select.h"
32
33 namespace
34 {
35
36 // The start of the list of target selectors.
37
38 gold::Target_selector* target_selectors;
39
40 } // End anonymous namespace.
41
42 namespace gold
43 {
44
45 // Class Set_target_once.
46
47 void
48 Set_target_once::do_run_once(void*)
49 {
50 this->target_selector_->set_target();
51 }
52
53 // Construct a Target_selector, which means adding it to the linked
54 // list. This runs at global constructor time, so we want it to be
55 // fast.
56
57 Target_selector::Target_selector(int machine, int size, bool is_big_endian,
58 const char* bfd_name, const char* emulation)
59 : machine_(machine), size_(size), is_big_endian_(is_big_endian),
60 bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
61 set_target_once_(this)
62 {
63 this->next_ = target_selectors;
64 target_selectors = this;
65 }
66
67 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
68 // avoid instantiating two instances of the same target.
69
70 Target*
71 Target_selector::instantiate_target()
72 {
73 this->set_target_once_.run_once(NULL);
74 return this->instantiated_target_;
75 }
76
77 // Instantiate the target. This is called at most once.
78
79 void
80 Target_selector::set_target()
81 {
82 gold_assert(this->instantiated_target_ == NULL);
83 this->instantiated_target_ = this->do_instantiate_target();
84 }
85
86 // If we instantiated TARGET, return the corresponding BFD name.
87
88 const char*
89 Target_selector::do_target_bfd_name(const Target* target)
90 {
91 if (!this->is_our_target(target))
92 return NULL;
93 const char* my_bfd_name = this->bfd_name();
94 gold_assert(my_bfd_name != NULL);
95 return my_bfd_name;
96 }
97
98 // Find the target for an ELF file.
99
100 Target*
101 select_target(int machine, int size, bool is_big_endian, int osabi,
102 int abiversion)
103 {
104 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
105 {
106 int pmach = p->machine();
107 if ((pmach == machine || pmach == elfcpp::EM_NONE)
108 && p->get_size() == size
109 && (p->is_big_endian() ? is_big_endian : !is_big_endian))
110 {
111 Target* ret = p->recognize(machine, osabi, abiversion);
112 if (ret != NULL)
113 return ret;
114 }
115 }
116 return NULL;
117 }
118
119 // Find a target using a BFD name. This is used to support the
120 // --oformat option.
121
122 Target*
123 select_target_by_bfd_name(const char* name)
124 {
125 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
126 {
127 const char* pname = p->bfd_name();
128 if (pname == NULL || strcmp(pname, name) == 0)
129 {
130 Target* ret = p->recognize_by_bfd_name(name);
131 if (ret != NULL)
132 return ret;
133 }
134 }
135 return NULL;
136 }
137
138 // Find a target using a GNU linker emulation. This is used to
139 // support the -m option.
140
141 Target*
142 select_target_by_emulation(const char* name)
143 {
144 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
145 {
146 const char* pname = p->emulation();
147 if (pname == NULL || strcmp(pname, name) == 0)
148 {
149 Target* ret = p->recognize_by_emulation(name);
150 if (ret != NULL)
151 return ret;
152 }
153 }
154 return NULL;
155 }
156
157 // Push all the supported BFD names onto a vector.
158
159 void
160 supported_target_names(std::vector<const char*>* names)
161 {
162 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
163 p->supported_bfd_names(names);
164 }
165
166 // Push all the supported emulations onto a vector.
167
168 void
169 supported_emulation_names(std::vector<const char*>* names)
170 {
171 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
172 p->supported_emulations(names);
173 }
174
175 // Implement the --print-output-format option.
176
177 void
178 print_output_format()
179 {
180 if (!parameters->target_valid())
181 {
182 // This case arises when --print-output-format is used with no
183 // input files. We need to come up with the right string to
184 // print based on the other options. If the user specified the
185 // format using a --oformat option, use that. That saves each
186 // target from having to remember the name that was used to
187 // select it. In other cases, we will just have to ask the
188 // target.
189 if (parameters->options().user_set_oformat())
190 {
191 const char* bfd_name = parameters->options().oformat();
192 Target* target = select_target_by_bfd_name(bfd_name);
193 if (target != NULL)
194 printf("%s\n", bfd_name);
195 else
196 gold_error(_("unrecognized output format %s"), bfd_name);
197 return;
198 }
199
200 parameters_force_valid_target();
201 }
202
203 const Target* target = &parameters->target();
204 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
205 {
206 const char* bfd_name = p->target_bfd_name(target);
207 if (bfd_name != NULL)
208 {
209 printf("%s\n", bfd_name);
210 return;
211 }
212 }
213
214 gold_unreachable();
215 }
216
217 } // End namespace gold.
This page took 0.039144 seconds and 5 git commands to generate.