1 // common.cc -- handle common symbols for gold
3 // Copyright 2006, 2007, 2008 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.
27 #include "workqueue.h"
37 // Allocate_commons_task methods.
39 // This task allocates the common symbols. We need a lock on the
43 Allocate_commons_task::is_runnable()
45 if (!this->symtab_lock_
->is_writable())
46 return this->symtab_lock_
;
50 // Return the locks we hold: one on the symbol table, and one blocker.
53 Allocate_commons_task::locks(Task_locker
* tl
)
55 tl
->add(this, this->blocker_
);
56 tl
->add(this, this->symtab_lock_
);
59 // Allocate the common symbols.
62 Allocate_commons_task::run(Workqueue
*)
64 this->symtab_
->allocate_commons(this->layout_
, this->mapfile_
);
67 // This class is used to sort the common symbol. We normally put the
68 // larger common symbols first. This can be changed by using
69 // --sort-commons, which tells the linker to sort by alignment.
75 Sort_commons(const Symbol_table
* symtab
,
76 Symbol_table::Sort_commons_order sort_order
)
77 : symtab_(symtab
), sort_order_(sort_order
)
80 bool operator()(const Symbol
* a
, const Symbol
* b
) const;
84 const Symbol_table
* symtab_
;
86 Symbol_table::Sort_commons_order sort_order_
;
91 Sort_commons
<size
>::operator()(const Symbol
* pa
, const Symbol
* pb
) const
98 const Symbol_table
* symtab
= this->symtab_
;
99 const Sized_symbol
<size
>* psa
= symtab
->get_sized_symbol
<size
>(pa
);
100 const Sized_symbol
<size
>* psb
= symtab
->get_sized_symbol
<size
>(pb
);
103 typename Sized_symbol
<size
>::Size_type sa
= psa
->symsize();
104 typename Sized_symbol
<size
>::Size_type sb
= psb
->symsize();
107 typename Sized_symbol
<size
>::Value_type aa
= psa
->value();
108 typename Sized_symbol
<size
>::Value_type ab
= psb
->value();
110 if (this->sort_order_
== Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING
)
117 else if (this->sort_order_
118 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING
)
126 gold_assert(this->sort_order_
127 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING
);
129 // Sort by descending size.
135 if (this->sort_order_
== Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING
)
137 // When the symbols are the same size, we sort them by
138 // alignment, largest alignment first.
145 // Otherwise we stabilize the sort by sorting by name.
146 return strcmp(psa
->name(), psb
->name()) < 0;
149 // Allocate the common symbols.
152 Symbol_table::allocate_commons(Layout
* layout
, Mapfile
* mapfile
)
154 Sort_commons_order sort_order
;
155 if (!parameters
->options().user_set_sort_common())
156 sort_order
= SORT_COMMONS_BY_SIZE_DESCENDING
;
159 const char* order
= parameters
->options().sort_common();
160 if (*order
== '\0' || strcmp(order
, "descending") == 0)
161 sort_order
= SORT_COMMONS_BY_ALIGNMENT_DESCENDING
;
162 else if (strcmp(order
, "ascending") == 0)
163 sort_order
= SORT_COMMONS_BY_ALIGNMENT_ASCENDING
;
166 gold_error("invalid --sort-common argument: %s", order
);
167 sort_order
= SORT_COMMONS_BY_SIZE_DESCENDING
;
171 if (parameters
->target().get_size() == 32)
173 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
174 this->do_allocate_commons
<32>(layout
, mapfile
, sort_order
);
179 else if (parameters
->target().get_size() == 64)
181 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
182 this->do_allocate_commons
<64>(layout
, mapfile
, sort_order
);
191 // Allocated the common symbols, sized version.
195 Symbol_table::do_allocate_commons(Layout
* layout
, Mapfile
* mapfile
,
196 Sort_commons_order sort_order
)
198 if (!this->commons_
.empty())
199 this->do_allocate_commons_list
<size
>(layout
, COMMONS_NORMAL
,
200 &this->commons_
, mapfile
,
202 if (!this->tls_commons_
.empty())
203 this->do_allocate_commons_list
<size
>(layout
, COMMONS_TLS
,
204 &this->tls_commons_
, mapfile
,
206 if (!this->small_commons_
.empty())
207 this->do_allocate_commons_list
<size
>(layout
, COMMONS_SMALL
,
208 &this->small_commons_
, mapfile
,
210 if (!this->large_commons_
.empty())
211 this->do_allocate_commons_list
<size
>(layout
, COMMONS_LARGE
,
212 &this->large_commons_
, mapfile
,
216 // Allocate the common symbols in a list. IS_TLS indicates whether
217 // these are TLS common symbols.
221 Symbol_table::do_allocate_commons_list(
223 Commons_section_type commons_section_type
,
224 Commons_type
* commons
,
226 Sort_commons_order sort_order
)
228 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
229 typedef typename Sized_symbol
<size
>::Size_type Size_type
;
231 // We've kept a list of all the common symbols. But the symbol may
232 // have been resolved to a defined symbol by now. And it may be a
233 // forwarder. First remove all non-common symbols.
235 uint64_t addralign
= 0;
236 for (Commons_type::iterator p
= commons
->begin();
241 if (sym
->is_forwarder())
243 sym
= this->resolve_forwards(sym
);
246 if (!sym
->is_common())
251 Sized_symbol
<size
>* ssym
= this->get_sized_symbol
<size
>(sym
);
252 if (ssym
->value() > addralign
)
253 addralign
= ssym
->value();
259 // Sort the common symbols.
260 std::sort(commons
->begin(), commons
->end(),
261 Sort_commons
<size
>(this, sort_order
));
263 // Place them in a newly allocated BSS section.
264 elfcpp::Elf_Xword flags
= elfcpp::SHF_WRITE
| elfcpp::SHF_ALLOC
;
267 switch (commons_section_type
)
271 ds_name
= "** common";
274 flags
|= elfcpp::SHF_TLS
;
276 ds_name
= "** tls common";
279 flags
|= parameters
->target().small_common_section_flags();
281 ds_name
= "** small common";
284 flags
|= parameters
->target().large_common_section_flags();
286 ds_name
= "** large common";
292 Output_data_space
*poc
= new Output_data_space(addralign
, ds_name
);
293 Output_section
*os
= layout
->add_output_section_data(name
,
296 false, false, false);
299 if (commons_section_type
== COMMONS_SMALL
)
300 os
->set_is_small_section();
301 else if (commons_section_type
== COMMONS_LARGE
)
302 os
->set_is_large_section();
305 // Allocate them all.
308 for (Commons_type::iterator p
= commons
->begin();
315 Sized_symbol
<size
>* ssym
= this->get_sized_symbol
<size
>(sym
);
317 // Record the symbol in the map file now, before we change its
318 // value. Pass the size in separately so that we don't have to
319 // templatize the map code, which is not performance sensitive.
321 mapfile
->report_allocate_common(sym
, ssym
->symsize());
323 off
= align_address(off
, ssym
->value());
324 ssym
->allocate_common(poc
, off
);
325 off
+= ssym
->symsize();
328 poc
->set_current_data_size(off
);
333 } // End namespace gold.