1 // common.cc -- handle common symbols for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
28 #include "workqueue.h"
38 // Allocate_commons_task methods.
40 // This task allocates the common symbols. We arrange to run it
41 // before anything else which needs to access the symbol table.
44 Allocate_commons_task::is_runnable()
52 Allocate_commons_task::locks(Task_locker* tl)
54 tl->add(this, this->blocker_);
57 // Allocate the common symbols.
60 Allocate_commons_task::run(Workqueue*)
62 this->symtab_->allocate_commons(this->layout_, this->mapfile_);
65 // This class is used to sort the common symbol. We normally put the
66 // larger common symbols first. This can be changed by using
67 // --sort-commons, which tells the linker to sort by alignment.
73 Sort_commons(const Symbol_table* symtab,
74 Symbol_table::Sort_commons_order sort_order)
75 : symtab_(symtab), sort_order_(sort_order)
78 bool operator()(const Symbol* a, const Symbol* b) const;
82 const Symbol_table* symtab_;
84 Symbol_table::Sort_commons_order sort_order_;
89 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
96 const Symbol_table* symtab = this->symtab_;
97 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
98 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
101 typename Sized_symbol<size>::Size_type sa = psa->symsize();
102 typename Sized_symbol<size>::Size_type sb = psb->symsize();
105 typename Sized_symbol<size>::Value_type aa = psa->value();
106 typename Sized_symbol<size>::Value_type ab = psb->value();
108 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
115 else if (this->sort_order_
116 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
124 gold_assert(this->sort_order_
125 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
127 // Sort by descending size.
133 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
135 // When the symbols are the same size, we sort them by
136 // alignment, largest alignment first.
143 // Otherwise we stabilize the sort by sorting by name.
144 return strcmp(psa->name(), psb->name()) < 0;
147 // Allocate the common symbols.
150 Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
152 Sort_commons_order sort_order;
153 if (!parameters->options().user_set_sort_common())
154 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
157 const char* order = parameters->options().sort_common();
158 if (*order == '\0' || strcmp(order, "descending") == 0)
159 sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING;
160 else if (strcmp(order, "ascending") == 0)
161 sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING;
164 gold_error("invalid --sort-common argument: %s", order);
165 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
169 if (parameters->target().get_size() == 32)
171 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
172 this->do_allocate_commons<32>(layout, mapfile, sort_order);
177 else if (parameters->target().get_size() == 64)
179 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
180 this->do_allocate_commons<64>(layout, mapfile, sort_order);
189 // Allocated the common symbols, sized version.
193 Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
194 Sort_commons_order sort_order)
196 if (!this->commons_.empty())
197 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
198 &this->commons_, mapfile,
200 if (!this->tls_commons_.empty())
201 this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
202 &this->tls_commons_, mapfile,
204 if (!this->small_commons_.empty())
205 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
206 &this->small_commons_, mapfile,
208 if (!this->large_commons_.empty())
209 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
210 &this->large_commons_, mapfile,
214 // Allocate the common symbols in a list. IS_TLS indicates whether
215 // these are TLS common symbols.
219 Symbol_table::do_allocate_commons_list(
221 Commons_section_type commons_section_type,
222 Commons_type* commons,
224 Sort_commons_order sort_order)
226 // We've kept a list of all the common symbols. But the symbol may
227 // have been resolved to a defined symbol by now. And it may be a
228 // forwarder. First remove all non-common symbols.
230 uint64_t addralign = 0;
231 for (Commons_type::iterator p = commons->begin();
236 if (sym->is_forwarder())
238 sym = this->resolve_forwards(sym);
241 if (!sym->is_common())
246 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
247 if (ssym->value() > addralign)
248 addralign = ssym->value();
254 // Sort the common symbols.
255 std::sort(commons->begin(), commons->end(),
256 Sort_commons<size>(this, sort_order));
258 // Place them in a newly allocated BSS section.
259 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
262 switch (commons_section_type)
266 ds_name = "** common";
269 flags |= elfcpp::SHF_TLS;
271 ds_name = "** tls common";
274 flags |= parameters->target().small_common_section_flags();
276 ds_name = "** small common";
279 flags |= parameters->target().large_common_section_flags();
281 ds_name = "** large common";
287 Output_data_space* poc;
290 if (!parameters->incremental_update())
292 poc = new Output_data_space(addralign, ds_name);
293 os = layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags,
294 poc, ORDER_INVALID, false);
298 // When doing an incremental update, we need to allocate each common
299 // directly from the output section's free list.
301 os = layout->find_output_section(name);
306 if (commons_section_type == COMMONS_SMALL)
307 os->set_is_small_section();
308 else if (commons_section_type == COMMONS_LARGE)
309 os->set_is_large_section();
312 // Allocate them all.
315 for (Commons_type::iterator p = commons->begin();
323 // Because we followed forwarding symbols above, but we didn't
324 // do it reliably before adding symbols to the list, it is
325 // possible for us to have the same symbol on the list twice.
326 // This can happen in the horrible case where a program defines
327 // a common symbol with the same name as a versioned libc
328 // symbol. That will show up here as a symbol which has already
329 // been allocated and is therefore no longer a common symbol.
330 if (!sym->is_common())
333 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
335 // Record the symbol in the map file now, before we change its
336 // value. Pass the size in separately so that we don't have to
337 // templatize the map code, which is not performance sensitive.
339 mapfile->report_allocate_common(sym, ssym->symsize());
343 off = align_address(off, ssym->value());
344 ssym->allocate_common(poc, off);
345 off += ssym->symsize();
349 // For an incremental update, allocate from the free list.
350 off = os->allocate(ssym->symsize(), ssym->value());
352 gold_fallback(_("out of patch space in section %s; "
353 "relink with --incremental-full"),
355 ssym->allocate_common(os, off);
360 poc->set_current_data_size(off);
365 } // End namespace gold.