*** empty log message ***
[deliverable/binutils-gdb.git] / gold / common.cc
CommitLineData
ead1e424
ILT
1// common.cc -- handle common symbols for gold
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
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"
28#include "layout.h"
29#include "output.h"
f6ce93d6 30#include "symtab.h"
ead1e424
ILT
31#include "common.h"
32
33namespace gold
34{
35
36// Allocate_commons_task methods.
37
38// This task allocates the common symbols. We need a lock on the
39// symbol table.
40
17a1d0a9
ILT
41Task_token*
42Allocate_commons_task::is_runnable()
ead1e424
ILT
43{
44 if (!this->symtab_lock_->is_writable())
17a1d0a9
ILT
45 return this->symtab_lock_;
46 return NULL;
ead1e424
ILT
47}
48
49// Return the locks we hold: one on the symbol table, and one blocker.
50
17a1d0a9
ILT
51void
52Allocate_commons_task::locks(Task_locker* tl)
ead1e424 53{
17a1d0a9
ILT
54 tl->add(this, this->blocker_);
55 tl->add(this, this->symtab_lock_);
ead1e424
ILT
56}
57
58// Allocate the common symbols.
59
60void
61Allocate_commons_task::run(Workqueue*)
62{
63 this->symtab_->allocate_commons(this->options_, this->layout_);
64}
65
66// This class is used to sort the common symbol by size. We put the
67// larger common symbols first.
68
69template<int size>
70class Sort_commons
71{
72 public:
73 Sort_commons(const Symbol_table* symtab)
74 : symtab_(symtab)
75 { }
76
77 bool operator()(const Symbol* a, const Symbol* b) const;
78
79 private:
80 const Symbol_table* symtab_;
81};
82
83template<int size>
84bool
85Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
86{
87 if (pa == NULL)
88 return false;
89 if (pb == NULL)
90 return true;
91
92 const Symbol_table* symtab = this->symtab_;
93 const Sized_symbol<size>* psa;
593f47df
ILT
94 psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa
95 SELECT_SIZE(size));
ead1e424 96 const Sized_symbol<size>* psb;
593f47df
ILT
97 psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb
98 SELECT_SIZE(size));
ead1e424
ILT
99
100 typename Sized_symbol<size>::Size_type sa = psa->symsize();
101 typename Sized_symbol<size>::Size_type sb = psb->symsize();
102 if (sa < sb)
103 return false;
104 else if (sb > sa)
105 return true;
106
107 // When the symbols are the same size, we sort them by alignment.
108 typename Sized_symbol<size>::Value_type va = psa->value();
109 typename Sized_symbol<size>::Value_type vb = psb->value();
110 if (va < vb)
111 return false;
112 else if (vb > va)
113 return true;
114
115 // Otherwise we stabilize the sort by sorting by name.
116 return strcmp(psa->name(), psb->name()) < 0;
117}
118
119// Allocate the common symbols.
120
121void
122Symbol_table::allocate_commons(const General_options& options, Layout* layout)
123{
9025d29d 124 if (parameters->get_size() == 32)
8ae3da90
ILT
125 {
126#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
127 this->do_allocate_commons<32>(options, layout);
128#else
129 gold_unreachable();
130#endif
131 }
9025d29d 132 else if (parameters->get_size() == 64)
8ae3da90
ILT
133 {
134#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
135 this->do_allocate_commons<64>(options, layout);
136#else
137 gold_unreachable();
138#endif
139 }
ead1e424 140 else
a3ad94ed 141 gold_unreachable();
ead1e424
ILT
142}
143
144// Allocated the common symbols, sized version.
145
146template<int size>
147void
148Symbol_table::do_allocate_commons(const General_options&,
149 Layout* layout)
150{
151 typedef typename Sized_symbol<size>::Value_type Value_type;
152 typedef typename Sized_symbol<size>::Size_type Size_type;
153
154 // We've kept a list of all the common symbols. But the symbol may
155 // have been resolved to a defined symbol by now. And it may be a
156 // forwarder. First remove all non-common symbols.
157 bool any = false;
158 uint64_t addralign = 0;
159 for (Commons_type::iterator p = this->commons_.begin();
160 p != this->commons_.end();
161 ++p)
162 {
163 Symbol* sym = *p;
164 if (sym->is_forwarder())
165 {
166 sym = this->resolve_forwards(sym);
167 *p = sym;
168 }
169 if (!sym->is_common())
170 *p = NULL;
171 else
172 {
173 any = true;
174 Sized_symbol<size>* ssym;
593f47df
ILT
175 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
176 sym
177 SELECT_SIZE(size));
ead1e424
ILT
178 if (ssym->value() > addralign)
179 addralign = ssym->value();
180 }
181 }
182 if (!any)
183 return;
184
185 // Sort the common symbols by size, so that they pack better into
186 // memory.
187 std::sort(this->commons_.begin(), this->commons_.end(),
188 Sort_commons<size>(this));
189
190 // Place them in a newly allocated .bss section.
191
a3ad94ed 192 Output_data_space *poc = new Output_data_space(addralign);
ead1e424
ILT
193
194 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
195 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
196 poc);
197
198 // Allocate them all.
199
200 off_t off = 0;
201 for (Commons_type::iterator p = this->commons_.begin();
202 p != this->commons_.end();
203 ++p)
204 {
205 Symbol* sym = *p;
206 if (sym == NULL)
207 break;
208
209 Sized_symbol<size>* ssym;
593f47df
ILT
210 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
211 SELECT_SIZE(size));
ead1e424
ILT
212
213 off = align_address(off, ssym->value());
214
c7912668 215 ssym->allocate_common(poc, off);
ead1e424 216
c7912668 217 off += ssym->symsize();
ead1e424
ILT
218 }
219
27bc2bce 220 poc->set_current_data_size(off);
ead1e424
ILT
221
222 this->commons_.clear();
223}
224
225} // End namespace gold.
This page took 0.077681 seconds and 4 git commands to generate.