1 // target-select.cc -- select a target for an object file
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
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.
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.
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.
31 #include "parameters.h"
32 #include "target-select.h"
37 // The start of the list of target selectors.
39 gold::Target_selector* target_selectors;
41 } // End anonymous namespace.
46 // Class Set_target_once.
49 Set_target_once::do_run_once(void*)
51 this->target_selector_->set_target();
54 // Construct a Target_selector, which means adding it to the linked
55 // list. This runs at global constructor time, so we want it to be
58 Target_selector::Target_selector(int machine, int size, bool is_big_endian,
59 const char* bfd_name, const char* emulation)
60 : machine_(machine), size_(size), is_big_endian_(is_big_endian),
61 bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
62 set_target_once_(this)
64 this->next_ = target_selectors;
65 target_selectors = this;
68 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
69 // avoid instantiating two instances of the same target.
72 Target_selector::instantiate_target()
74 this->set_target_once_.run_once(NULL);
75 return this->instantiated_target_;
78 // Instantiate the target. This is called at most once.
81 Target_selector::set_target()
83 gold_assert(this->instantiated_target_ == NULL);
84 this->instantiated_target_ = this->do_instantiate_target();
87 // If we instantiated TARGET, return the corresponding BFD name.
90 Target_selector::do_target_bfd_name(const Target* target)
92 if (!this->is_our_target(target))
94 const char* my_bfd_name = this->bfd_name();
95 gold_assert(my_bfd_name != NULL);
99 // Find the target for an ELF file.
102 select_target(Input_file* input_file, off_t offset,
103 int machine, int size, bool is_big_endian,
104 int osabi, int abiversion)
106 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
108 int pmach = p->machine();
109 if ((pmach == machine || pmach == elfcpp::EM_NONE)
110 && p->get_size() == size
111 && (p->is_big_endian() ? is_big_endian : !is_big_endian))
113 Target* ret = p->recognize(input_file, offset,
114 machine, osabi, abiversion);
122 // Find a target using a BFD name. This is used to support the
126 select_target_by_bfd_name(const char* name)
128 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
130 const char* pname = p->bfd_name();
131 if (pname == NULL || strcmp(pname, name) == 0)
133 Target* ret = p->recognize_by_bfd_name(name);
141 // Find a target using a GNU linker emulation. This is used to
142 // support the -m option.
145 select_target_by_emulation(const char* name)
147 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
149 const char* pname = p->emulation();
150 if (pname == NULL || strcmp(pname, name) == 0)
152 Target* ret = p->recognize_by_emulation(name);
160 // Push all the supported BFD names onto a vector.
163 supported_target_names(std::vector<const char*>* names)
165 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
166 p->supported_bfd_names(names);
169 // Push all the supported emulations onto a vector.
172 supported_emulation_names(std::vector<const char*>* names)
174 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
175 p->supported_emulations(names);
178 // Implement the --print-output-format option.
181 print_output_format()
183 if (!parameters->target_valid())
185 // This case arises when --print-output-format is used with no
186 // input files. We need to come up with the right string to
187 // print based on the other options. If the user specified the
188 // format using a --oformat option, use that. That saves each
189 // target from having to remember the name that was used to
190 // select it. In other cases, we will just have to ask the
192 if (parameters->options().user_set_oformat())
194 const char* bfd_name = parameters->options().oformat();
195 Target* target = select_target_by_bfd_name(bfd_name);
197 printf("%s\n", bfd_name);
199 gold_error(_("unrecognized output format %s"), bfd_name);
203 parameters_force_valid_target();
206 const Target* target = ¶meters->target();
207 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
209 const char* bfd_name = p->target_bfd_name(target);
210 if (bfd_name != NULL)
212 printf("%s\n", bfd_name);
220 } // End namespace gold.