1 // common.cc -- handle common symbols for gold
3 // Copyright (C) 2006-2019 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 arrange to run it
40 // before anything else which needs to access the symbol table.
43 Allocate_commons_task::is_runnable()
51 Allocate_commons_task::locks(Task_locker* tl)
53 tl->add(this, this->blocker_);
56 // Allocate the common symbols.
59 Allocate_commons_task::run(Workqueue*)
61 this->symtab_->allocate_commons(this->layout_, this->mapfile_);
64 // This class is used to sort the common symbol. We normally put the
65 // larger common symbols first. This can be changed by using
66 // --sort-commons, which tells the linker to sort by alignment.
72 Sort_commons(const Symbol_table* symtab,
73 Symbol_table::Sort_commons_order sort_order)
74 : symtab_(symtab), sort_order_(sort_order)
77 bool operator()(const Symbol* a, const Symbol* b) const;
81 const Symbol_table* symtab_;
83 Symbol_table::Sort_commons_order sort_order_;
88 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
95 const Symbol_table* symtab = this->symtab_;
96 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
97 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
100 typename Sized_symbol<size>::Size_type sa = psa->symsize();
101 typename Sized_symbol<size>::Size_type sb = psb->symsize();
104 typename Sized_symbol<size>::Value_type aa = psa->value();
105 typename Sized_symbol<size>::Value_type ab = psb->value();
107 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
114 else if (this->sort_order_
115 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
123 gold_assert(this->sort_order_
124 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
126 // Sort by descending size.
132 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
134 // When the symbols are the same size, we sort them by
135 // alignment, largest alignment first.
142 // Otherwise we stabilize the sort by sorting by name.
143 return strcmp(psa->name(), psb->name()) < 0;
146 // Allocate the common symbols.
149 Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
151 Sort_commons_order sort_order;
152 if (!parameters->options().user_set_sort_common())
153 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
156 const char* order = parameters->options().sort_common();
157 if (*order == '\0' || strcmp(order, "descending") == 0)
158 sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING;
159 else if (strcmp(order, "ascending") == 0)
160 sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING;
163 gold_error("invalid --sort-common argument: %s", order);
164 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
168 if (parameters->target().get_size() == 32)
170 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
171 this->do_allocate_commons<32>(layout, mapfile, sort_order);
176 else if (parameters->target().get_size() == 64)
178 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
179 this->do_allocate_commons<64>(layout, mapfile, sort_order);
188 // Allocated the common symbols, sized version.
192 Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
193 Sort_commons_order sort_order)
195 if (!this->commons_.empty())
196 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
197 &this->commons_, mapfile,
199 if (!this->tls_commons_.empty())
200 this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
201 &this->tls_commons_, mapfile,
203 if (!this->small_commons_.empty())
204 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
205 &this->small_commons_, mapfile,
207 if (!this->large_commons_.empty())
208 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
209 &this->large_commons_, mapfile,
213 // Allocate the common symbols in a list. IS_TLS indicates whether
214 // these are TLS common symbols.
218 Symbol_table::do_allocate_commons_list(
220 Commons_section_type commons_section_type,
221 Commons_type* commons,
223 Sort_commons_order sort_order)
225 // We've kept a list of all the common symbols. But the symbol may
226 // have been resolved to a defined symbol by now. And it may be a
227 // forwarder. First remove all non-common symbols.
229 uint64_t addralign = 0;
230 for (Commons_type::iterator p = commons->begin();
235 if (sym->is_forwarder())
237 sym = this->resolve_forwards(sym);
240 if (!sym->is_common())
245 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
246 if (ssym->value() > addralign)
247 addralign = ssym->value();
253 // Sort the common symbols.
254 std::sort(commons->begin(), commons->end(),
255 Sort_commons<size>(this, sort_order));
257 // Place them in a newly allocated BSS section.
258 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
261 switch (commons_section_type)
265 ds_name = "** common";
268 flags |= elfcpp::SHF_TLS;
270 ds_name = "** tls common";
273 flags |= parameters->target().small_common_section_flags();
275 ds_name = "** small common";
278 flags |= parameters->target().large_common_section_flags();
280 ds_name = "** large common";
286 Output_data_space* poc;
289 if (!parameters->incremental_update())
291 poc = new Output_data_space(addralign, ds_name);
292 os = layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags,
293 poc, ORDER_INVALID, false);
297 // When doing an incremental update, we need to allocate each common
298 // directly from the output section's free list.
300 os = layout->find_output_section(name);
305 if (commons_section_type == COMMONS_SMALL)
306 os->set_is_small_section();
307 else if (commons_section_type == COMMONS_LARGE)
308 os->set_is_large_section();
311 // Allocate them all.
314 for (Commons_type::iterator p = commons->begin();
322 // Because we followed forwarding symbols above, but we didn't
323 // do it reliably before adding symbols to the list, it is
324 // possible for us to have the same symbol on the list twice.
325 // This can happen in the horrible case where a program defines
326 // a common symbol with the same name as a versioned libc
327 // symbol. That will show up here as a symbol which has already
328 // been allocated and is therefore no longer a common symbol.
329 if (!sym->is_common())
332 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
334 // Record the symbol in the map file now, before we change its
335 // value. Pass the size in separately so that we don't have to
336 // templatize the map code, which is not performance sensitive.
338 mapfile->report_allocate_common(sym, ssym->symsize());
342 off = align_address(off, ssym->value());
343 ssym->allocate_common(poc, off);
344 off += ssym->symsize();
348 // For an incremental update, allocate from the free list.
349 off = os->allocate(ssym->symsize(), ssym->value());
351 gold_fallback(_("out of patch space in section %s; "
352 "relink with --incremental-full"),
354 ssym->allocate_common(os, off);
359 poc->set_current_data_size(off);
364 } // End namespace gold.