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