]>
Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // target-select.cc -- select a target for an object file |
2 | ||
114dfbe1 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
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 | ||
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 | ||
114dfbe1 ILT |
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 | ||
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 ILT |
54 | Target_selector::Target_selector(int machine, int size, bool is_big_endian, |
55 | const char* bfd_name) | |
56 | : machine_(machine), size_(size), is_big_endian_(is_big_endian), | |
114dfbe1 | 57 | bfd_name_(bfd_name), instantiated_target_(NULL), set_target_once_(this) |
14bfc3f5 ILT |
58 | { |
59 | this->next_ = target_selectors; | |
60 | target_selectors = this; | |
61 | } | |
62 | ||
114dfbe1 ILT |
63 | // Instantiate the target and return it. Use SET_TARGET_ONCE_ to |
64 | // avoid instantiating two instances of the same target. | |
7f055c20 ILT |
65 | |
66 | Target* | |
67 | Target_selector::instantiate_target() | |
68 | { | |
114dfbe1 | 69 | this->set_target_once_.run_once(NULL); |
7f055c20 ILT |
70 | return this->instantiated_target_; |
71 | } | |
72 | ||
114dfbe1 ILT |
73 | // Instantiate the target. This is called at most once. |
74 | ||
75 | void | |
76 | Target_selector::set_target() | |
77 | { | |
78 | gold_assert(this->instantiated_target_ == NULL); | |
79 | this->instantiated_target_ = this->do_instantiate_target(); | |
80 | } | |
81 | ||
14bfc3f5 ILT |
82 | // Find the target for an ELF file. |
83 | ||
0daa6f62 | 84 | Target* |
6340166c | 85 | select_target(int machine, int size, bool is_big_endian, int osabi, |
14bfc3f5 ILT |
86 | int abiversion) |
87 | { | |
ead1e424 | 88 | for (Target_selector* p = target_selectors; p != NULL; p = p->next()) |
14bfc3f5 ILT |
89 | { |
90 | int pmach = p->machine(); | |
91 | if ((pmach == machine || pmach == elfcpp::EM_NONE) | |
6340166c ILT |
92 | && p->get_size() == size |
93 | && (p->is_big_endian() ? is_big_endian : !is_big_endian)) | |
14bfc3f5 ILT |
94 | { |
95 | Target* ret = p->recognize(machine, osabi, abiversion); | |
96 | if (ret != NULL) | |
97 | return ret; | |
98 | } | |
99 | } | |
100 | return NULL; | |
101 | } | |
102 | ||
0daa6f62 ILT |
103 | // Find a target using a BFD name. This is used to support the |
104 | // --oformat option. | |
105 | ||
106 | Target* | |
107 | select_target_by_name(const char* name) | |
108 | { | |
109 | for (Target_selector* p = target_selectors; p != NULL; p = p->next()) | |
110 | { | |
e96caa79 ILT |
111 | const char* pname = p->bfd_name(); |
112 | if (pname == NULL || strcmp(pname, name) == 0) | |
113 | { | |
114 | Target* ret = p->recognize_by_name(name); | |
115 | if (ret != NULL) | |
116 | return ret; | |
117 | } | |
0daa6f62 ILT |
118 | } |
119 | return NULL; | |
120 | } | |
121 | ||
e96caa79 ILT |
122 | // Push all the supported BFD names onto a vector. |
123 | ||
124 | void | |
125 | supported_target_names(std::vector<const char*>* names) | |
126 | { | |
127 | for (Target_selector* p = target_selectors; p != NULL; p = p->next()) | |
128 | p->supported_names(names); | |
129 | } | |
130 | ||
14bfc3f5 | 131 | } // End namespace gold. |