gdb/
[deliverable/binutils-gdb.git] / gold / target-select.cc
CommitLineData
14bfc3f5
ILT
1// target-select.cc -- select a target for an object file
2
03ef7571 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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
14bfc3f5
ILT
23#include "gold.h"
24
f1ddb600 25#include <cstdio>
04bf7072
ILT
26#include <cstring>
27
14bfc3f5 28#include "elfcpp.h"
f1ddb600
ILT
29#include "options.h"
30#include "parameters.h"
14bfc3f5
ILT
31#include "target-select.h"
32
33namespace
34{
35
36// The start of the list of target selectors.
37
38gold::Target_selector* target_selectors;
39
40} // End anonymous namespace.
41
42namespace gold
43{
44
114dfbe1
ILT
45// Class Set_target_once.
46
47void
48Set_target_once::do_run_once(void*)
49{
50 this->target_selector_->set_target();
51}
52
14bfc3f5
ILT
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
2ea97941 57Target_selector::Target_selector(int machine, int size, bool is_big_endian,
03ef7571 58 const char* bfd_name, const char* emulation)
2ea97941 59 : machine_(machine), size_(size), is_big_endian_(is_big_endian),
03ef7571
ILT
60 bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
61 set_target_once_(this)
14bfc3f5
ILT
62{
63 this->next_ = target_selectors;
64 target_selectors = this;
65}
66
114dfbe1
ILT
67// Instantiate the target and return it. Use SET_TARGET_ONCE_ to
68// avoid instantiating two instances of the same target.
7f055c20
ILT
69
70Target*
71Target_selector::instantiate_target()
72{
114dfbe1 73 this->set_target_once_.run_once(NULL);
7f055c20
ILT
74 return this->instantiated_target_;
75}
76
114dfbe1
ILT
77// Instantiate the target. This is called at most once.
78
79void
80Target_selector::set_target()
81{
82 gold_assert(this->instantiated_target_ == NULL);
83 this->instantiated_target_ = this->do_instantiate_target();
84}
85
f1ddb600
ILT
86// If we instantiated TARGET, return the corresponding BFD name.
87
88const char*
89Target_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
14bfc3f5
ILT
98// Find the target for an ELF file.
99
0daa6f62 100Target*
6340166c 101select_target(int machine, int size, bool is_big_endian, int osabi,
14bfc3f5
ILT
102 int abiversion)
103{
ead1e424 104 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
14bfc3f5
ILT
105 {
106 int pmach = p->machine();
107 if ((pmach == machine || pmach == elfcpp::EM_NONE)
6340166c
ILT
108 && p->get_size() == size
109 && (p->is_big_endian() ? is_big_endian : !is_big_endian))
14bfc3f5
ILT
110 {
111 Target* ret = p->recognize(machine, osabi, abiversion);
112 if (ret != NULL)
113 return ret;
114 }
115 }
116 return NULL;
117}
118
0daa6f62
ILT
119// Find a target using a BFD name. This is used to support the
120// --oformat option.
121
122Target*
03ef7571 123select_target_by_bfd_name(const char* name)
0daa6f62
ILT
124{
125 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
126 {
e96caa79
ILT
127 const char* pname = p->bfd_name();
128 if (pname == NULL || strcmp(pname, name) == 0)
129 {
03ef7571
ILT
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
141Target*
142select_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);
e96caa79
ILT
150 if (ret != NULL)
151 return ret;
152 }
0daa6f62
ILT
153 }
154 return NULL;
155}
156
e96caa79
ILT
157// Push all the supported BFD names onto a vector.
158
159void
160supported_target_names(std::vector<const char*>* names)
161{
162 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
03ef7571
ILT
163 p->supported_bfd_names(names);
164}
165
166// Push all the supported emulations onto a vector.
167
168void
169supported_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);
e96caa79
ILT
173}
174
f1ddb600
ILT
175// Implement the --print-output-format option.
176
177void
178print_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
14bfc3f5 217} // End namespace gold.
This page took 0.251791 seconds and 4 git commands to generate.