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
04bf7072
ILT
25#include <cstring>
26
14bfc3f5
ILT
27#include "elfcpp.h"
28#include "target-select.h"
29
30namespace
31{
32
33// The start of the list of target selectors.
34
35gold::Target_selector* target_selectors;
36
37} // End anonymous namespace.
38
39namespace gold
40{
41
114dfbe1
ILT
42// Class Set_target_once.
43
44void
45Set_target_once::do_run_once(void*)
46{
47 this->target_selector_->set_target();
48}
49
14bfc3f5
ILT
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
2ea97941 54Target_selector::Target_selector(int machine, int size, bool is_big_endian,
03ef7571 55 const char* bfd_name, const char* emulation)
2ea97941 56 : machine_(machine), size_(size), is_big_endian_(is_big_endian),
03ef7571
ILT
57 bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
58 set_target_once_(this)
14bfc3f5
ILT
59{
60 this->next_ = target_selectors;
61 target_selectors = this;
62}
63
114dfbe1
ILT
64// Instantiate the target and return it. Use SET_TARGET_ONCE_ to
65// avoid instantiating two instances of the same target.
7f055c20
ILT
66
67Target*
68Target_selector::instantiate_target()
69{
114dfbe1 70 this->set_target_once_.run_once(NULL);
7f055c20
ILT
71 return this->instantiated_target_;
72}
73
114dfbe1
ILT
74// Instantiate the target. This is called at most once.
75
76void
77Target_selector::set_target()
78{
79 gold_assert(this->instantiated_target_ == NULL);
80 this->instantiated_target_ = this->do_instantiate_target();
81}
82
14bfc3f5
ILT
83// Find the target for an ELF file.
84
0daa6f62 85Target*
6340166c 86select_target(int machine, int size, bool is_big_endian, int osabi,
14bfc3f5
ILT
87 int abiversion)
88{
ead1e424 89 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
14bfc3f5
ILT
90 {
91 int pmach = p->machine();
92 if ((pmach == machine || pmach == elfcpp::EM_NONE)
6340166c
ILT
93 && p->get_size() == size
94 && (p->is_big_endian() ? is_big_endian : !is_big_endian))
14bfc3f5
ILT
95 {
96 Target* ret = p->recognize(machine, osabi, abiversion);
97 if (ret != NULL)
98 return ret;
99 }
100 }
101 return NULL;
102}
103
0daa6f62
ILT
104// Find a target using a BFD name. This is used to support the
105// --oformat option.
106
107Target*
03ef7571 108select_target_by_bfd_name(const char* name)
0daa6f62
ILT
109{
110 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
111 {
e96caa79
ILT
112 const char* pname = p->bfd_name();
113 if (pname == NULL || strcmp(pname, name) == 0)
114 {
03ef7571
ILT
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
126Target*
127select_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);
e96caa79
ILT
135 if (ret != NULL)
136 return ret;
137 }
0daa6f62
ILT
138 }
139 return NULL;
140}
141
e96caa79
ILT
142// Push all the supported BFD names onto a vector.
143
144void
145supported_target_names(std::vector<const char*>* names)
146{
147 for (Target_selector* p = target_selectors; p != NULL; p = p->next())
03ef7571
ILT
148 p->supported_bfd_names(names);
149}
150
151// Push all the supported emulations onto a vector.
152
153void
154supported_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);
e96caa79
ILT
158}
159
14bfc3f5 160} // End namespace gold.
This page took 0.207121 seconds and 4 git commands to generate.