PR 10931
[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)
94 return false;
95 if (pb == NULL)
96 return true;
97
98 const Symbol_table* symtab = this->symtab_;
7d1a9ebb
ILT
99 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
100 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
ead1e424 101
fc59c572 102 // The size.
ead1e424
ILT
103 typename Sized_symbol<size>::Size_type sa = psa->symsize();
104 typename Sized_symbol<size>::Size_type sb = psb->symsize();
fc59c572
ILT
105
106 // The alignment.
107 typename Sized_symbol<size>::Value_type aa = psa->value();
108 typename Sized_symbol<size>::Value_type ab = psb->value();
109
110 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
111 {
112 if (aa < ab)
113 return false;
114 else if (ab < aa)
115 return true;
116 }
117 else if (this->sort_order_
118 == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
119 {
120 if (aa < ab)
121 return true;
122 else if (ab < aa)
123 return false;
124 }
125 else
126 gold_assert(this->sort_order_
127 == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
128
129 // Sort by descending size.
ead1e424
ILT
130 if (sa < sb)
131 return false;
49bdd526 132 else if (sb < sa)
ead1e424
ILT
133 return true;
134
fc59c572
ILT
135 if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
136 {
137 // When the symbols are the same size, we sort them by
138 // alignment, largest alignment first.
139 if (aa < ab)
140 return false;
141 else if (ab < aa)
142 return true;
143 }
ead1e424
ILT
144
145 // Otherwise we stabilize the sort by sorting by name.
146 return strcmp(psa->name(), psb->name()) < 0;
147}
148
149// Allocate the common symbols.
150
151void
7d9e3d98 152Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
ead1e424 153{
fc59c572
ILT
154 Sort_commons_order sort_order;
155 if (!parameters->options().user_set_sort_common())
156 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
157 else
158 {
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;
164 else
165 {
166 gold_error("invalid --sort-common argument: %s", order);
167 sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
168 }
169 }
170
8851ecca 171 if (parameters->target().get_size() == 32)
8ae3da90
ILT
172 {
173#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
fc59c572 174 this->do_allocate_commons<32>(layout, mapfile, sort_order);
8ae3da90
ILT
175#else
176 gold_unreachable();
177#endif
178 }
8851ecca 179 else if (parameters->target().get_size() == 64)
8ae3da90
ILT
180 {
181#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
fc59c572 182 this->do_allocate_commons<64>(layout, mapfile, sort_order);
8ae3da90
ILT
183#else
184 gold_unreachable();
185#endif
186 }
ead1e424 187 else
a3ad94ed 188 gold_unreachable();
ead1e424
ILT
189}
190
191// Allocated the common symbols, sized version.
192
193template<int size>
194void
fc59c572
ILT
195Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
196 Sort_commons_order sort_order)
155a0dd7 197{
8a5e3e08
ILT
198 if (!this->commons_.empty())
199 this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
fc59c572
ILT
200 &this->commons_, mapfile,
201 sort_order);
8a5e3e08
ILT
202 if (!this->tls_commons_.empty())
203 this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
fc59c572
ILT
204 &this->tls_commons_, mapfile,
205 sort_order);
8a5e3e08
ILT
206 if (!this->small_commons_.empty())
207 this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
fc59c572
ILT
208 &this->small_commons_, mapfile,
209 sort_order);
8a5e3e08
ILT
210 if (!this->large_commons_.empty())
211 this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
fc59c572
ILT
212 &this->large_commons_, mapfile,
213 sort_order);
155a0dd7
ILT
214}
215
216// Allocate the common symbols in a list. IS_TLS indicates whether
217// these are TLS common symbols.
218
219template<int size>
220void
8a5e3e08
ILT
221Symbol_table::do_allocate_commons_list(
222 Layout* layout,
223 Commons_section_type commons_section_type,
224 Commons_type* commons,
fc59c572
ILT
225 Mapfile* mapfile,
226 Sort_commons_order sort_order)
ead1e424
ILT
227{
228 typedef typename Sized_symbol<size>::Value_type Value_type;
229 typedef typename Sized_symbol<size>::Size_type Size_type;
230
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.
234 bool any = false;
235 uint64_t addralign = 0;
155a0dd7
ILT
236 for (Commons_type::iterator p = commons->begin();
237 p != commons->end();
ead1e424
ILT
238 ++p)
239 {
240 Symbol* sym = *p;
241 if (sym->is_forwarder())
242 {
243 sym = this->resolve_forwards(sym);
244 *p = sym;
245 }
246 if (!sym->is_common())
247 *p = NULL;
248 else
249 {
250 any = true;
7d1a9ebb 251 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424
ILT
252 if (ssym->value() > addralign)
253 addralign = ssym->value();
254 }
255 }
256 if (!any)
257 return;
258
fc59c572 259 // Sort the common symbols.
155a0dd7 260 std::sort(commons->begin(), commons->end(),
fc59c572 261 Sort_commons<size>(this, sort_order));
ead1e424 262
155a0dd7 263 // Place them in a newly allocated BSS section.
155a0dd7 264 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
8a5e3e08
ILT
265 const char* name;
266 const char* ds_name;
267 switch (commons_section_type)
155a0dd7 268 {
8a5e3e08
ILT
269 case COMMONS_NORMAL:
270 name = ".bss";
271 ds_name = "** common";
272 break;
273 case COMMONS_TLS:
155a0dd7 274 flags |= elfcpp::SHF_TLS;
8a5e3e08
ILT
275 name = ".tbss";
276 ds_name = "** tls common";
277 break;
278 case COMMONS_SMALL:
279 flags |= parameters->target().small_common_section_flags();
280 name = ".sbss";
281 ds_name = "** small common";
282 break;
283 case COMMONS_LARGE:
284 flags |= parameters->target().large_common_section_flags();
285 name = ".lbss";
286 ds_name = "** large common";
287 break;
288 default:
289 gold_unreachable();
290 }
291
292 Output_data_space *poc = new Output_data_space(addralign, ds_name);
293 Output_section *os = layout->add_output_section_data(name,
294 elfcpp::SHT_NOBITS,
1a2dff53
ILT
295 flags, poc, false,
296 false, false, false);
8a5e3e08
ILT
297 if (os != NULL)
298 {
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();
155a0dd7 303 }
ead1e424
ILT
304
305 // Allocate them all.
306
307 off_t off = 0;
155a0dd7
ILT
308 for (Commons_type::iterator p = commons->begin();
309 p != commons->end();
ead1e424
ILT
310 ++p)
311 {
312 Symbol* sym = *p;
313 if (sym == NULL)
314 break;
7d1a9ebb 315 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
7d9e3d98
ILT
316
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.
320 if (mapfile != NULL)
321 mapfile->report_allocate_common(sym, ssym->symsize());
322
ead1e424 323 off = align_address(off, ssym->value());
c7912668 324 ssym->allocate_common(poc, off);
c7912668 325 off += ssym->symsize();
ead1e424
ILT
326 }
327
27bc2bce 328 poc->set_current_data_size(off);
ead1e424 329
155a0dd7 330 commons->clear();
ead1e424
ILT
331}
332
333} // End namespace gold.
This page took 0.15239 seconds and 4 git commands to generate.