PR gold/12934
[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 <cstring>
26
27 #include "elfcpp.h"
28 #include "target-select.h"
29
30 namespace
31 {
32
33 // The start of the list of target selectors.
34
35 gold::Target_selector* target_selectors;
36
37 } // End anonymous namespace.
38
39 namespace gold
40 {
41
42 // Class Set_target_once.
43
44 void
45 Set_target_once::do_run_once(void*)
46 {
47 this->target_selector_->set_target();
48 }
49
50 // Construct a Target_selector, which means adding it to the linked
51 // list. This runs at global constructor time, so we want it to be
52 // fast.
53
54 Target_selector::Target_selector(int machine, int size, bool is_big_endian,
55 const char* bfd_name, const char* emulation)
56 : machine_(machine), size_(size), is_big_endian_(is_big_endian),
57 bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
58 set_target_once_(this)
59 {
60 this->next_ = target_selectors;
61 target_selectors = this;
62 }
63
64 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
65 // avoid instantiating two instances of the same target.
66
67 Target*
68 Target_selector::instantiate_target()
69 {
70 this->set_target_once_.run_once(NULL);
71 return this->instantiated_target_;
72 }
73
74 // Instantiate the target. This is called at most once.
75
76 void
77 Target_selector::set_target()
78 {
79 gold_assert(this->instantiated_target_ == NULL);
80 this->instantiated_target_ = this->do_instantiate_target();
81 }
82
83 // Find the target for an ELF file.
84
85 Target*
86 select_target(int machine, int size, bool is_big_endian, int osabi,
87 int abiversion)
88 {
89 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
90 {
91 int pmach = p->machine();
92 if ((pmach == machine || pmach == elfcpp::EM_NONE)
93 && p->get_size() == size
94 && (p->is_big_endian() ? is_big_endian : !is_big_endian))
95 {
96 Target* ret = p->recognize(machine, osabi, abiversion);
97 if (ret != NULL)
98 return ret;
99 }
100 }
101 return NULL;
102 }
103
104 // Find a target using a BFD name. This is used to support the
105 // --oformat option.
106
107 Target*
108 select_target_by_bfd_name(const char* name)
109 {
110 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
111 {
112 const char* pname = p->bfd_name();
113 if (pname == NULL || strcmp(pname, name) == 0)
114 {
115 Target* ret = p->recognize_by_bfd_name(name);
116 if (ret != NULL)
117 return ret;
118 }
119 }
120 return NULL;
121 }
122
123 // Find a target using a GNU linker emulation. This is used to
124 // support the -m option.
125
126 Target*
127 select_target_by_emulation(const char* name)
128 {
129 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
130 {
131 const char* pname = p->emulation();
132 if (pname == NULL || strcmp(pname, name) == 0)
133 {
134 Target* ret = p->recognize_by_emulation(name);
135 if (ret != NULL)
136 return ret;
137 }
138 }
139 return NULL;
140 }
141
142 // Push all the supported BFD names onto a vector.
143
144 void
145 supported_target_names(std::vector<const char*>* names)
146 {
147 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
148 p->supported_bfd_names(names);
149 }
150
151 // Push all the supported emulations onto a vector.
152
153 void
154 supported_emulation_names(std::vector<const char*>* names)
155 {
156 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
157 p->supported_emulations(names);
158 }
159
160 } // End namespace gold.
This page took 0.033594 seconds and 5 git commands to generate.