* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / gold / target-select.cc
blob9370a8714ab6a027160d343de24aecef8598cd82
1 // target-select.cc -- select a target for an object file
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 <cstdio>
26 #include <cstring>
28 #include "elfcpp.h"
29 #include "options.h"
30 #include "parameters.h"
31 #include "target-select.h"
33 namespace
36 // The start of the list of target selectors.
38 gold::Target_selector* target_selectors;
40 } // End anonymous namespace.
42 namespace gold
45 // Class Set_target_once.
47 void
48 Set_target_once::do_run_once(void*)
50 this->target_selector_->set_target();
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.
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)
63 this->next_ = target_selectors;
64 target_selectors = this;
67 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
68 // avoid instantiating two instances of the same target.
70 Target*
71 Target_selector::instantiate_target()
73 this->set_target_once_.run_once(NULL);
74 return this->instantiated_target_;
77 // Instantiate the target. This is called at most once.
79 void
80 Target_selector::set_target()
82 gold_assert(this->instantiated_target_ == NULL);
83 this->instantiated_target_ = this->do_instantiate_target();
86 // If we instantiated TARGET, return the corresponding BFD name.
88 const char*
89 Target_selector::do_target_bfd_name(const Target* target)
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;
98 // Find the target for an ELF file.
100 Target*
101 select_target(int machine, int size, bool is_big_endian, int osabi,
102 int abiversion)
104 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
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))
111 Target* ret = p->recognize(machine, osabi, abiversion);
112 if (ret != NULL)
113 return ret;
116 return NULL;
119 // Find a target using a BFD name. This is used to support the
120 // --oformat option.
122 Target*
123 select_target_by_bfd_name(const char* name)
125 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
127 const char* pname = p->bfd_name();
128 if (pname == NULL || strcmp(pname, name) == 0)
130 Target* ret = p->recognize_by_bfd_name(name);
131 if (ret != NULL)
132 return ret;
135 return NULL;
138 // Find a target using a GNU linker emulation. This is used to
139 // support the -m option.
141 Target*
142 select_target_by_emulation(const char* name)
144 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
146 const char* pname = p->emulation();
147 if (pname == NULL || strcmp(pname, name) == 0)
149 Target* ret = p->recognize_by_emulation(name);
150 if (ret != NULL)
151 return ret;
154 return NULL;
157 // Push all the supported BFD names onto a vector.
159 void
160 supported_target_names(std::vector<const char*>* names)
162 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
163 p->supported_bfd_names(names);
166 // Push all the supported emulations onto a vector.
168 void
169 supported_emulation_names(std::vector<const char*>* names)
171 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
172 p->supported_emulations(names);
175 // Implement the --print-output-format option.
177 void
178 print_output_format()
180 if (!parameters->target_valid())
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())
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;
200 parameters_force_valid_target();
203 const Target* target = &parameters->target();
204 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
206 const char* bfd_name = p->target_bfd_name(target);
207 if (bfd_name != NULL)
209 printf("%s\n", bfd_name);
210 return;
214 gold_unreachable();
217 } // End namespace gold.