PR 10979
[deliverable/binutils-gdb.git] / gold / common.cc
CommitLineData
ead1e424
ILT
1// common.cc -- handle common symbols for gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
ead1e424
ILT
23#include "gold.h"
24
25#include <algorithm>
26
27#include "workqueue.h"
7d9e3d98 28#include "mapfile.h"
ead1e424
ILT
29#include "layout.h"
30#include "output.h"
f6ce93d6 31#include "symtab.h"
ead1e424
ILT
32#include "common.h"
33
34namespace gold
35{
36
37// Allocate_commons_task methods.
38
39// This task allocates the common symbols. We need a lock on the
40// symbol table.
41
17a1d0a9
ILT
42Task_token*
43Allocate_commons_task::is_runnable()
ead1e424
ILT
44{
45 if (!this->symtab_lock_->is_writable())
17a1d0a9
ILT
46 return this->symtab_lock_;
47 return NULL;
ead1e424
ILT
48}
49
50// Return the locks we hold: one on the symbol table, and one blocker.
51
17a1d0a9
ILT
52void
53Allocate_commons_task::locks(Task_locker* tl)
ead1e424 54{
17a1d0a9
ILT
55 tl->add(this, this->blocker_);
56 tl->add(this, this->symtab_lock_);
ead1e424
ILT
57}
58
59// Allocate the common symbols.
60
61void
62Allocate_commons_task::run(Workqueue*)
63{
7d9e3d98 64 this->symtab_->allocate_commons(this->layout_, this->mapfile_);
ead1e424
ILT
65}
66
fc59c572
ILT
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.
ead1e424
ILT
70
71template<int size>
72class Sort_commons
73{
74 public:
fc59c572
ILT
75 Sort_commons(const Symbol_table* symtab,
76 Symbol_table::Sort_commons_order sort_order)
77 : symtab_(symtab), sort_order_(sort_order)
ead1e424
ILT
78 { }
79
80 bool operator()(const Symbol* a, const Symbol* b) const;
81
82 private:
fc59c572 83 // The symbol table.
ead1e424 84 const Symbol_table* symtab_;
fc59c572
ILT
85 // How to sort.
86 Symbol_table::Sort_commons_order sort_order_;
ead1e424
ILT
87};
88
89template<int size>
90bool
91Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
92{
93 if (pa == NULL)
eda294df
ILT
94 {
95 if (pb == NULL)
96 {
97 // Stabilize sort. The order really doesn't matter, because
98 // these entries will be discarded, but we want to return
99 // the same result every time we compare pa and pb.
100 return pa < pb;
101 }
102 return false;
103 }
ead1e424
ILT
104 if (pb == NULL)
105 return true;
106
107 const Symbol_table* symtab = this->symtab_;
7d1a9ebb
ILT
108 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
109 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
ead1e424 110
fc59c572 111 // The size.
ead1e424
ILT
112 typename Sized_symbol<size>::Size_type sa = psa->symsize();
113 typename Sized_symbol<size>::Size_type sb = psb->symsize();
fc59c572
ILT
114
115 // The alignment.
116 typename Sized_symbol<size>::Value_type aa = psa->value();
117 typename Sized_symbol<size>::Value_type ab = psb->value();
118
119 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
120 {
121 if (aa < ab)
122 return false;
123 else if (ab < aa)
124 return true;
125 }
126 else if (this->sort_order_
127 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
128 {
129 if (aa < ab)
130 return true;
131 else if (ab < aa)
132 return false;
133 }
134 else
135 gold_assert(this->sort_order_
136 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
137
138 // Sort by descending size.
ead1e424
ILT
139 if (sa < sb)
140 return false;
49bdd526 141 else if (sb < sa)
ead1e424
ILT
142 return true;
143
fc59c572
ILT
144 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
145 {
146 // When the symbols are the same size, we sort them by
147 // alignment, largest alignment first.
148 if (aa < ab)
149 return false;
150 else if (ab < aa)
151 return true;
152 }
ead1e424
ILT
153
154 // Otherwise we stabilize the sort by sorting by name.
155 return strcmp(psa->name(), psb->name()) < 0;
156}
157
158// Allocate the common symbols.
159
160void
7d9e3d98 161Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
ead1e424 162{
fc59c572
ILT
163 Sort_commons_order sort_order;
164 if (!parameters->options().user_set_sort_common())
165 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
166 else
167 {
168 const char* order = parameters->options().sort_common();
169 if (*order == '\0' || strcmp(order, "descending") == 0)
170 sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING;
171 else if (strcmp(order, "ascending") == 0)
172 sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING;
173 else
174 {
175 gold_error("invalid --sort-common argument: %s", order);
176 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
177 }
178 }
179
8851ecca 180 if (parameters->target().get_size() == 32)
8ae3da90
ILT
181 {
182#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
fc59c572 183 this->do_allocate_commons<32>(layout, mapfile, sort_order);
8ae3da90
ILT
184#else
185 gold_unreachable();
186#endif
187 }
8851ecca 188 else if (parameters->target().get_size() == 64)
8ae3da90
ILT
189 {
190#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
fc59c572 191 this->do_allocate_commons<64>(layout, mapfile, sort_order);
8ae3da90
ILT
192#else
193 gold_unreachable();
194#endif
195 }
ead1e424 196 else
a3ad94ed 197 gold_unreachable();
ead1e424
ILT
198}
199
200// Allocated the common symbols, sized version.
201
202template<int size>
203void
fc59c572
ILT
204Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
205 Sort_commons_order sort_order)
155a0dd7 206{
8a5e3e08
ILT
207 if (!this->commons_.empty())
208 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
fc59c572
ILT
209 &this->commons_, mapfile,
210 sort_order);
8a5e3e08
ILT
211 if (!this->tls_commons_.empty())
212 this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
fc59c572
ILT
213 &this->tls_commons_, mapfile,
214 sort_order);
8a5e3e08
ILT
215 if (!this->small_commons_.empty())
216 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
fc59c572
ILT
217 &this->small_commons_, mapfile,
218 sort_order);
8a5e3e08
ILT
219 if (!this->large_commons_.empty())
220 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
fc59c572
ILT
221 &this->large_commons_, mapfile,
222 sort_order);
155a0dd7
ILT
223}
224
225// Allocate the common symbols in a list. IS_TLS indicates whether
226// these are TLS common symbols.
227
228template<int size>
229void
8a5e3e08
ILT
230Symbol_table::do_allocate_commons_list(
231 Layout* layout,
232 Commons_section_type commons_section_type,
233 Commons_type* commons,
fc59c572
ILT
234 Mapfile* mapfile,
235 Sort_commons_order sort_order)
ead1e424
ILT
236{
237 typedef typename Sized_symbol<size>::Value_type Value_type;
238 typedef typename Sized_symbol<size>::Size_type Size_type;
239
240 // We've kept a list of all the common symbols. But the symbol may
241 // have been resolved to a defined symbol by now. And it may be a
242 // forwarder. First remove all non-common symbols.
243 bool any = false;
244 uint64_t addralign = 0;
155a0dd7
ILT
245 for (Commons_type::iterator p = commons->begin();
246 p != commons->end();
ead1e424
ILT
247 ++p)
248 {
249 Symbol* sym = *p;
250 if (sym->is_forwarder())
251 {
252 sym = this->resolve_forwards(sym);
253 *p = sym;
254 }
255 if (!sym->is_common())
256 *p = NULL;
257 else
258 {
259 any = true;
7d1a9ebb 260 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424
ILT
261 if (ssym->value() > addralign)
262 addralign = ssym->value();
263 }
264 }
265 if (!any)
266 return;
267
fc59c572 268 // Sort the common symbols.
155a0dd7 269 std::sort(commons->begin(), commons->end(),
fc59c572 270 Sort_commons<size>(this, sort_order));
ead1e424 271
155a0dd7 272 // Place them in a newly allocated BSS section.
155a0dd7 273 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
8a5e3e08
ILT
274 const char* name;
275 const char* ds_name;
276 switch (commons_section_type)
155a0dd7 277 {
8a5e3e08
ILT
278 case COMMONS_NORMAL:
279 name = ".bss";
280 ds_name = "** common";
281 break;
282 case COMMONS_TLS:
155a0dd7 283 flags |= elfcpp::SHF_TLS;
8a5e3e08
ILT
284 name = ".tbss";
285 ds_name = "** tls common";
286 break;
287 case COMMONS_SMALL:
288 flags |= parameters->target().small_common_section_flags();
289 name = ".sbss";
290 ds_name = "** small common";
291 break;
292 case COMMONS_LARGE:
293 flags |= parameters->target().large_common_section_flags();
294 name = ".lbss";
295 ds_name = "** large common";
296 break;
297 default:
298 gold_unreachable();
299 }
300
301 Output_data_space *poc = new Output_data_space(addralign, ds_name);
302 Output_section *os = layout->add_output_section_data(name,
303 elfcpp::SHT_NOBITS,
1a2dff53
ILT
304 flags, poc, false,
305 false, false, false);
8a5e3e08
ILT
306 if (os != NULL)
307 {
308 if (commons_section_type == COMMONS_SMALL)
309 os->set_is_small_section();
310 else if (commons_section_type == COMMONS_LARGE)
311 os->set_is_large_section();
155a0dd7 312 }
ead1e424
ILT
313
314 // Allocate them all.
315
316 off_t off = 0;
155a0dd7
ILT
317 for (Commons_type::iterator p = commons->begin();
318 p != commons->end();
ead1e424
ILT
319 ++p)
320 {
321 Symbol* sym = *p;
322 if (sym == NULL)
323 break;
eda294df
ILT
324
325 // Because we followed forwarding symbols above, but we didn't
326 // do it reliably before adding symbols to the list, it is
327 // possible for us to have the same symbol on the list twice.
328 // This can happen in the horrible case where a program defines
329 // a common symbol with the same name as a versioned libc
330 // symbol. That will show up here as a symbol which has already
331 // been allocated and is therefore no longer a common symbol.
332 if (!sym->is_common())
333 continue;
334
7d1a9ebb 335 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
7d9e3d98
ILT
336
337 // Record the symbol in the map file now, before we change its
338 // value. Pass the size in separately so that we don't have to
339 // templatize the map code, which is not performance sensitive.
340 if (mapfile != NULL)
341 mapfile->report_allocate_common(sym, ssym->symsize());
342
ead1e424 343 off = align_address(off, ssym->value());
c7912668 344 ssym->allocate_common(poc, off);
c7912668 345 off += ssym->symsize();
ead1e424
ILT
346 }
347
27bc2bce 348 poc->set_current_data_size(off);
ead1e424 349
155a0dd7 350 commons->clear();
ead1e424
ILT
351}
352
353} // End namespace gold.
This page took 0.147282 seconds and 4 git commands to generate.