1 // target-select.cc -- select a target for an object file
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "target-select.h"
33 // The start of the list of target selectors.
35 gold::Target_selector
* target_selectors
;
37 } // End anonymous namespace.
42 // Class Set_target_once.
45 Set_target_once::do_run_once(void*)
47 this->target_selector_
->set_target();
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
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)
60 this->next_
= target_selectors
;
61 target_selectors
= this;
64 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
65 // avoid instantiating two instances of the same target.
68 Target_selector::instantiate_target()
70 this->set_target_once_
.run_once(NULL
);
71 return this->instantiated_target_
;
74 // Instantiate the target. This is called at most once.
77 Target_selector::set_target()
79 gold_assert(this->instantiated_target_
== NULL
);
80 this->instantiated_target_
= this->do_instantiate_target();
83 // Find the target for an ELF file.
86 select_target(int machine
, int size
, bool is_big_endian
, int osabi
,
89 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
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
))
96 Target
* ret
= p
->recognize(machine
, osabi
, abiversion
);
104 // Find a target using a BFD name. This is used to support the
108 select_target_by_bfd_name(const char* name
)
110 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
112 const char* pname
= p
->bfd_name();
113 if (pname
== NULL
|| strcmp(pname
, name
) == 0)
115 Target
* ret
= p
->recognize_by_bfd_name(name
);
123 // Find a target using a GNU linker emulation. This is used to
124 // support the -m option.
127 select_target_by_emulation(const char* name
)
129 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
131 const char* pname
= p
->emulation();
132 if (pname
== NULL
|| strcmp(pname
, name
) == 0)
134 Target
* ret
= p
->recognize_by_emulation(name
);
142 // Push all the supported BFD names onto a vector.
145 supported_target_names(std::vector
<const char*>* names
)
147 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
148 p
->supported_bfd_names(names
);
151 // Push all the supported emulations onto a vector.
154 supported_emulation_names(std::vector
<const char*>* names
)
156 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
157 p
->supported_emulations(names
);
160 } // End namespace gold.