* defs.h (directory_command): Don't declare.
[deliverable/binutils-gdb.git] / gold / common.cc
CommitLineData
ead1e424
ILT
1// common.cc -- handle common symbols for gold
2
1f3212db
ILT
3// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4// Free Software Foundation, Inc.
6cb15b7f
ILT
5// Written by Ian Lance Taylor <iant@google.com>.
6
7// This file is part of gold.
8
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.
13
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.
18
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.
23
ead1e424
ILT
24#include "gold.h"
25
26#include <algorithm>
27
28#include "workqueue.h"
7d9e3d98 29#include "mapfile.h"
ead1e424
ILT
30#include "layout.h"
31#include "output.h"
f6ce93d6 32#include "symtab.h"
ead1e424
ILT
33#include "common.h"
34
35namespace gold
36{
37
38// Allocate_commons_task methods.
39
93ceb764
ILT
40// This task allocates the common symbols. We arrange to run it
41// before anything else which needs to access the symbol table.
ead1e424 42
17a1d0a9
ILT
43Task_token*
44Allocate_commons_task::is_runnable()
ead1e424 45{
17a1d0a9 46 return NULL;
ead1e424
ILT
47}
48
93ceb764 49// Release a blocker.
ead1e424 50
17a1d0a9
ILT
51void
52Allocate_commons_task::locks(Task_locker* tl)
ead1e424 53{
17a1d0a9 54 tl->add(this, this->blocker_);
ead1e424
ILT
55}
56
57// Allocate the common symbols.
58
59void
60Allocate_commons_task::run(Workqueue*)
61{
7d9e3d98 62 this->symtab_->allocate_commons(this->layout_, this->mapfile_);
ead1e424
ILT
63}
64
fc59c572
ILT
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.
ead1e424
ILT
68
69template<int size>
70class Sort_commons
71{
72 public:
fc59c572
ILT
73 Sort_commons(const Symbol_table* symtab,
74 Symbol_table::Sort_commons_order sort_order)
75 : symtab_(symtab), sort_order_(sort_order)
ead1e424
ILT
76 { }
77
78 bool operator()(const Symbol* a, const Symbol* b) const;
79
80 private:
fc59c572 81 // The symbol table.
ead1e424 82 const Symbol_table* symtab_;
fc59c572
ILT
83 // How to sort.
84 Symbol_table::Sort_commons_order sort_order_;
ead1e424
ILT
85};
86
87template<int size>
88bool
89Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
90{
91 if (pa == NULL)
55455f89 92 return false;
ead1e424
ILT
93 if (pb == NULL)
94 return true;
95
96 const Symbol_table* symtab = this->symtab_;
7d1a9ebb
ILT
97 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
98 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
ead1e424 99
fc59c572 100 // The size.
ead1e424
ILT
101 typename Sized_symbol<size>::Size_type sa = psa->symsize();
102 typename Sized_symbol<size>::Size_type sb = psb->symsize();
fc59c572
ILT
103
104 // The alignment.
105 typename Sized_symbol<size>::Value_type aa = psa->value();
106 typename Sized_symbol<size>::Value_type ab = psb->value();
107
108 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
109 {
110 if (aa < ab)
111 return false;
112 else if (ab < aa)
113 return true;
114 }
115 else if (this->sort_order_
116 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
117 {
118 if (aa < ab)
119 return true;
120 else if (ab < aa)
121 return false;
122 }
123 else
124 gold_assert(this->sort_order_
125 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
126
127 // Sort by descending size.
ead1e424
ILT
128 if (sa < sb)
129 return false;
49bdd526 130 else if (sb < sa)
ead1e424
ILT
131 return true;
132
fc59c572
ILT
133 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
134 {
135 // When the symbols are the same size, we sort them by
136 // alignment, largest alignment first.
137 if (aa < ab)
138 return false;
139 else if (ab < aa)
140 return true;
141 }
ead1e424
ILT
142
143 // Otherwise we stabilize the sort by sorting by name.
144 return strcmp(psa->name(), psb->name()) < 0;
145}
146
147// Allocate the common symbols.
148
149void
7d9e3d98 150Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
ead1e424 151{
fc59c572
ILT
152 Sort_commons_order sort_order;
153 if (!parameters->options().user_set_sort_common())
154 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
155 else
156 {
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;
162 else
163 {
164 gold_error("invalid --sort-common argument: %s", order);
165 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
166 }
167 }
168
8851ecca 169 if (parameters->target().get_size() == 32)
8ae3da90
ILT
170 {
171#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
fc59c572 172 this->do_allocate_commons<32>(layout, mapfile, sort_order);
8ae3da90
ILT
173#else
174 gold_unreachable();
175#endif
176 }
8851ecca 177 else if (parameters->target().get_size() == 64)
8ae3da90
ILT
178 {
179#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
fc59c572 180 this->do_allocate_commons<64>(layout, mapfile, sort_order);
8ae3da90
ILT
181#else
182 gold_unreachable();
183#endif
184 }
ead1e424 185 else
a3ad94ed 186 gold_unreachable();
ead1e424
ILT
187}
188
189// Allocated the common symbols, sized version.
190
191template<int size>
192void
fc59c572
ILT
193Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
194 Sort_commons_order sort_order)
155a0dd7 195{
8a5e3e08
ILT
196 if (!this->commons_.empty())
197 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
fc59c572
ILT
198 &this->commons_, mapfile,
199 sort_order);
8a5e3e08
ILT
200 if (!this->tls_commons_.empty())
201 this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
fc59c572
ILT
202 &this->tls_commons_, mapfile,
203 sort_order);
8a5e3e08
ILT
204 if (!this->small_commons_.empty())
205 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
fc59c572
ILT
206 &this->small_commons_, mapfile,
207 sort_order);
8a5e3e08
ILT
208 if (!this->large_commons_.empty())
209 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
fc59c572
ILT
210 &this->large_commons_, mapfile,
211 sort_order);
155a0dd7
ILT
212}
213
214// Allocate the common symbols in a list. IS_TLS indicates whether
215// these are TLS common symbols.
216
217template<int size>
218void
8a5e3e08
ILT
219Symbol_table::do_allocate_commons_list(
220 Layout* layout,
221 Commons_section_type commons_section_type,
222 Commons_type* commons,
fc59c572
ILT
223 Mapfile* mapfile,
224 Sort_commons_order sort_order)
ead1e424 225{
ead1e424
ILT
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.
229 bool any = false;
230 uint64_t addralign = 0;
155a0dd7
ILT
231 for (Commons_type::iterator p = commons->begin();
232 p != commons->end();
ead1e424
ILT
233 ++p)
234 {
235 Symbol* sym = *p;
236 if (sym->is_forwarder())
237 {
238 sym = this->resolve_forwards(sym);
239 *p = sym;
240 }
241 if (!sym->is_common())
242 *p = NULL;
243 else
244 {
245 any = true;
7d1a9ebb 246 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424
ILT
247 if (ssym->value() > addralign)
248 addralign = ssym->value();
249 }
250 }
251 if (!any)
252 return;
253
fc59c572 254 // Sort the common symbols.
155a0dd7 255 std::sort(commons->begin(), commons->end(),
fc59c572 256 Sort_commons<size>(this, sort_order));
ead1e424 257
155a0dd7 258 // Place them in a newly allocated BSS section.
155a0dd7 259 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
8a5e3e08
ILT
260 const char* name;
261 const char* ds_name;
262 switch (commons_section_type)
155a0dd7 263 {
8a5e3e08
ILT
264 case COMMONS_NORMAL:
265 name = ".bss";
266 ds_name = "** common";
267 break;
268 case COMMONS_TLS:
155a0dd7 269 flags |= elfcpp::SHF_TLS;
8a5e3e08
ILT
270 name = ".tbss";
271 ds_name = "** tls common";
272 break;
273 case COMMONS_SMALL:
274 flags |= parameters->target().small_common_section_flags();
275 name = ".sbss";
276 ds_name = "** small common";
277 break;
278 case COMMONS_LARGE:
279 flags |= parameters->target().large_common_section_flags();
280 name = ".lbss";
281 ds_name = "** large common";
282 break;
283 default:
284 gold_unreachable();
285 }
286
5146f448
CC
287 Output_data_space* poc;
288 Output_section* os;
289
290 if (!parameters->incremental_update())
291 {
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);
295 }
296 else
297 {
298 // When doing an incremental update, we need to allocate each common
299 // directly from the output section's free list.
300 poc = NULL;
301 os = layout->find_output_section(name);
302 }
303
8a5e3e08
ILT
304 if (os != NULL)
305 {
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();
155a0dd7 310 }
ead1e424
ILT
311
312 // Allocate them all.
313
314 off_t off = 0;
155a0dd7
ILT
315 for (Commons_type::iterator p = commons->begin();
316 p != commons->end();
ead1e424
ILT
317 ++p)
318 {
319 Symbol* sym = *p;
320 if (sym == NULL)
321 break;
eda294df
ILT
322
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())
331 continue;
332
7d1a9ebb 333 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
7d9e3d98
ILT
334
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.
338 if (mapfile != NULL)
339 mapfile->report_allocate_common(sym, ssym->symsize());
340
5146f448
CC
341 if (poc != NULL)
342 {
343 off = align_address(off, ssym->value());
344 ssym->allocate_common(poc, off);
345 off += ssym->symsize();
346 }
347 else
348 {
349 // For an incremental update, allocate from the free list.
350 off = os->allocate(ssym->symsize(), ssym->value());
351 if (off == -1)
e6455dfb
CC
352 gold_fallback(_("out of patch space in section %s; "
353 "relink with --incremental-full"),
354 os->name());
5146f448
CC
355 ssym->allocate_common(os, off);
356 }
ead1e424
ILT
357 }
358
5146f448
CC
359 if (poc != NULL)
360 poc->set_current_data_size(off);
ead1e424 361
155a0dd7 362 commons->clear();
ead1e424
ILT
363}
364
365} // End namespace gold.
This page took 0.288608 seconds and 4 git commands to generate.