1*fae548d3Szrj // common.cc -- handle common symbols for gold
2*fae548d3Szrj
3*fae548d3Szrj // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*fae548d3Szrj
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj
23*fae548d3Szrj #include "gold.h"
24*fae548d3Szrj
25*fae548d3Szrj #include <algorithm>
26*fae548d3Szrj
27*fae548d3Szrj #include "workqueue.h"
28*fae548d3Szrj #include "mapfile.h"
29*fae548d3Szrj #include "layout.h"
30*fae548d3Szrj #include "output.h"
31*fae548d3Szrj #include "symtab.h"
32*fae548d3Szrj #include "common.h"
33*fae548d3Szrj
34*fae548d3Szrj namespace gold
35*fae548d3Szrj {
36*fae548d3Szrj
37*fae548d3Szrj // Allocate_commons_task methods.
38*fae548d3Szrj
39*fae548d3Szrj // This task allocates the common symbols. We arrange to run it
40*fae548d3Szrj // before anything else which needs to access the symbol table.
41*fae548d3Szrj
42*fae548d3Szrj Task_token*
is_runnable()43*fae548d3Szrj Allocate_commons_task::is_runnable()
44*fae548d3Szrj {
45*fae548d3Szrj return NULL;
46*fae548d3Szrj }
47*fae548d3Szrj
48*fae548d3Szrj // Release a blocker.
49*fae548d3Szrj
50*fae548d3Szrj void
locks(Task_locker * tl)51*fae548d3Szrj Allocate_commons_task::locks(Task_locker* tl)
52*fae548d3Szrj {
53*fae548d3Szrj tl->add(this, this->blocker_);
54*fae548d3Szrj }
55*fae548d3Szrj
56*fae548d3Szrj // Allocate the common symbols.
57*fae548d3Szrj
58*fae548d3Szrj void
run(Workqueue *)59*fae548d3Szrj Allocate_commons_task::run(Workqueue*)
60*fae548d3Szrj {
61*fae548d3Szrj this->symtab_->allocate_commons(this->layout_, this->mapfile_);
62*fae548d3Szrj }
63*fae548d3Szrj
64*fae548d3Szrj // This class is used to sort the common symbol. We normally put the
65*fae548d3Szrj // larger common symbols first. This can be changed by using
66*fae548d3Szrj // --sort-commons, which tells the linker to sort by alignment.
67*fae548d3Szrj
68*fae548d3Szrj template<int size>
69*fae548d3Szrj class Sort_commons
70*fae548d3Szrj {
71*fae548d3Szrj public:
Sort_commons(const Symbol_table * symtab,Symbol_table::Sort_commons_order sort_order)72*fae548d3Szrj Sort_commons(const Symbol_table* symtab,
73*fae548d3Szrj Symbol_table::Sort_commons_order sort_order)
74*fae548d3Szrj : symtab_(symtab), sort_order_(sort_order)
75*fae548d3Szrj { }
76*fae548d3Szrj
77*fae548d3Szrj bool operator()(const Symbol* a, const Symbol* b) const;
78*fae548d3Szrj
79*fae548d3Szrj private:
80*fae548d3Szrj // The symbol table.
81*fae548d3Szrj const Symbol_table* symtab_;
82*fae548d3Szrj // How to sort.
83*fae548d3Szrj Symbol_table::Sort_commons_order sort_order_;
84*fae548d3Szrj };
85*fae548d3Szrj
86*fae548d3Szrj template<int size>
87*fae548d3Szrj bool
operator ()(const Symbol * pa,const Symbol * pb) const88*fae548d3Szrj Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
89*fae548d3Szrj {
90*fae548d3Szrj if (pa == NULL)
91*fae548d3Szrj return false;
92*fae548d3Szrj if (pb == NULL)
93*fae548d3Szrj return true;
94*fae548d3Szrj
95*fae548d3Szrj const Symbol_table* symtab = this->symtab_;
96*fae548d3Szrj const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
97*fae548d3Szrj const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
98*fae548d3Szrj
99*fae548d3Szrj // The size.
100*fae548d3Szrj typename Sized_symbol<size>::Size_type sa = psa->symsize();
101*fae548d3Szrj typename Sized_symbol<size>::Size_type sb = psb->symsize();
102*fae548d3Szrj
103*fae548d3Szrj // The alignment.
104*fae548d3Szrj typename Sized_symbol<size>::Value_type aa = psa->value();
105*fae548d3Szrj typename Sized_symbol<size>::Value_type ab = psb->value();
106*fae548d3Szrj
107*fae548d3Szrj if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
108*fae548d3Szrj {
109*fae548d3Szrj if (aa < ab)
110*fae548d3Szrj return false;
111*fae548d3Szrj else if (ab < aa)
112*fae548d3Szrj return true;
113*fae548d3Szrj }
114*fae548d3Szrj else if (this->sort_order_
115*fae548d3Szrj == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
116*fae548d3Szrj {
117*fae548d3Szrj if (aa < ab)
118*fae548d3Szrj return true;
119*fae548d3Szrj else if (ab < aa)
120*fae548d3Szrj return false;
121*fae548d3Szrj }
122*fae548d3Szrj else
123*fae548d3Szrj gold_assert(this->sort_order_
124*fae548d3Szrj == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
125*fae548d3Szrj
126*fae548d3Szrj // Sort by descending size.
127*fae548d3Szrj if (sa < sb)
128*fae548d3Szrj return false;
129*fae548d3Szrj else if (sb < sa)
130*fae548d3Szrj return true;
131*fae548d3Szrj
132*fae548d3Szrj if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
133*fae548d3Szrj {
134*fae548d3Szrj // When the symbols are the same size, we sort them by
135*fae548d3Szrj // alignment, largest alignment first.
136*fae548d3Szrj if (aa < ab)
137*fae548d3Szrj return false;
138*fae548d3Szrj else if (ab < aa)
139*fae548d3Szrj return true;
140*fae548d3Szrj }
141*fae548d3Szrj
142*fae548d3Szrj // Otherwise we stabilize the sort by sorting by name.
143*fae548d3Szrj return strcmp(psa->name(), psb->name()) < 0;
144*fae548d3Szrj }
145*fae548d3Szrj
146*fae548d3Szrj // Allocate the common symbols.
147*fae548d3Szrj
148*fae548d3Szrj void
allocate_commons(Layout * layout,Mapfile * mapfile)149*fae548d3Szrj Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
150*fae548d3Szrj {
151*fae548d3Szrj Sort_commons_order sort_order;
152*fae548d3Szrj if (!parameters->options().user_set_sort_common())
153*fae548d3Szrj sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
154*fae548d3Szrj else
155*fae548d3Szrj {
156*fae548d3Szrj const char* order = parameters->options().sort_common();
157*fae548d3Szrj if (*order == '\0' || strcmp(order, "descending") == 0)
158*fae548d3Szrj sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING;
159*fae548d3Szrj else if (strcmp(order, "ascending") == 0)
160*fae548d3Szrj sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING;
161*fae548d3Szrj else
162*fae548d3Szrj {
163*fae548d3Szrj gold_error("invalid --sort-common argument: %s", order);
164*fae548d3Szrj sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
165*fae548d3Szrj }
166*fae548d3Szrj }
167*fae548d3Szrj
168*fae548d3Szrj if (parameters->target().get_size() == 32)
169*fae548d3Szrj {
170*fae548d3Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
171*fae548d3Szrj this->do_allocate_commons<32>(layout, mapfile, sort_order);
172*fae548d3Szrj #else
173*fae548d3Szrj gold_unreachable();
174*fae548d3Szrj #endif
175*fae548d3Szrj }
176*fae548d3Szrj else if (parameters->target().get_size() == 64)
177*fae548d3Szrj {
178*fae548d3Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
179*fae548d3Szrj this->do_allocate_commons<64>(layout, mapfile, sort_order);
180*fae548d3Szrj #else
181*fae548d3Szrj gold_unreachable();
182*fae548d3Szrj #endif
183*fae548d3Szrj }
184*fae548d3Szrj else
185*fae548d3Szrj gold_unreachable();
186*fae548d3Szrj }
187*fae548d3Szrj
188*fae548d3Szrj // Allocated the common symbols, sized version.
189*fae548d3Szrj
190*fae548d3Szrj template<int size>
191*fae548d3Szrj void
do_allocate_commons(Layout * layout,Mapfile * mapfile,Sort_commons_order sort_order)192*fae548d3Szrj Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
193*fae548d3Szrj Sort_commons_order sort_order)
194*fae548d3Szrj {
195*fae548d3Szrj if (!this->commons_.empty())
196*fae548d3Szrj this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
197*fae548d3Szrj &this->commons_, mapfile,
198*fae548d3Szrj sort_order);
199*fae548d3Szrj if (!this->tls_commons_.empty())
200*fae548d3Szrj this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
201*fae548d3Szrj &this->tls_commons_, mapfile,
202*fae548d3Szrj sort_order);
203*fae548d3Szrj if (!this->small_commons_.empty())
204*fae548d3Szrj this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
205*fae548d3Szrj &this->small_commons_, mapfile,
206*fae548d3Szrj sort_order);
207*fae548d3Szrj if (!this->large_commons_.empty())
208*fae548d3Szrj this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
209*fae548d3Szrj &this->large_commons_, mapfile,
210*fae548d3Szrj sort_order);
211*fae548d3Szrj }
212*fae548d3Szrj
213*fae548d3Szrj // Allocate the common symbols in a list. IS_TLS indicates whether
214*fae548d3Szrj // these are TLS common symbols.
215*fae548d3Szrj
216*fae548d3Szrj template<int size>
217*fae548d3Szrj void
do_allocate_commons_list(Layout * layout,Commons_section_type commons_section_type,Commons_type * commons,Mapfile * mapfile,Sort_commons_order sort_order)218*fae548d3Szrj Symbol_table::do_allocate_commons_list(
219*fae548d3Szrj Layout* layout,
220*fae548d3Szrj Commons_section_type commons_section_type,
221*fae548d3Szrj Commons_type* commons,
222*fae548d3Szrj Mapfile* mapfile,
223*fae548d3Szrj Sort_commons_order sort_order)
224*fae548d3Szrj {
225*fae548d3Szrj // We've kept a list of all the common symbols. But the symbol may
226*fae548d3Szrj // have been resolved to a defined symbol by now. And it may be a
227*fae548d3Szrj // forwarder. First remove all non-common symbols.
228*fae548d3Szrj bool any = false;
229*fae548d3Szrj uint64_t addralign = 0;
230*fae548d3Szrj for (Commons_type::iterator p = commons->begin();
231*fae548d3Szrj p != commons->end();
232*fae548d3Szrj ++p)
233*fae548d3Szrj {
234*fae548d3Szrj Symbol* sym = *p;
235*fae548d3Szrj if (sym->is_forwarder())
236*fae548d3Szrj {
237*fae548d3Szrj sym = this->resolve_forwards(sym);
238*fae548d3Szrj *p = sym;
239*fae548d3Szrj }
240*fae548d3Szrj if (!sym->is_common())
241*fae548d3Szrj *p = NULL;
242*fae548d3Szrj else
243*fae548d3Szrj {
244*fae548d3Szrj any = true;
245*fae548d3Szrj Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
246*fae548d3Szrj if (ssym->value() > addralign)
247*fae548d3Szrj addralign = ssym->value();
248*fae548d3Szrj }
249*fae548d3Szrj }
250*fae548d3Szrj if (!any)
251*fae548d3Szrj return;
252*fae548d3Szrj
253*fae548d3Szrj // Sort the common symbols.
254*fae548d3Szrj std::sort(commons->begin(), commons->end(),
255*fae548d3Szrj Sort_commons<size>(this, sort_order));
256*fae548d3Szrj
257*fae548d3Szrj // Place them in a newly allocated BSS section.
258*fae548d3Szrj elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
259*fae548d3Szrj const char* name;
260*fae548d3Szrj const char* ds_name;
261*fae548d3Szrj switch (commons_section_type)
262*fae548d3Szrj {
263*fae548d3Szrj case COMMONS_NORMAL:
264*fae548d3Szrj name = ".bss";
265*fae548d3Szrj ds_name = "** common";
266*fae548d3Szrj break;
267*fae548d3Szrj case COMMONS_TLS:
268*fae548d3Szrj flags |= elfcpp::SHF_TLS;
269*fae548d3Szrj name = ".tbss";
270*fae548d3Szrj ds_name = "** tls common";
271*fae548d3Szrj break;
272*fae548d3Szrj case COMMONS_SMALL:
273*fae548d3Szrj flags |= parameters->target().small_common_section_flags();
274*fae548d3Szrj name = ".sbss";
275*fae548d3Szrj ds_name = "** small common";
276*fae548d3Szrj break;
277*fae548d3Szrj case COMMONS_LARGE:
278*fae548d3Szrj flags |= parameters->target().large_common_section_flags();
279*fae548d3Szrj name = ".lbss";
280*fae548d3Szrj ds_name = "** large common";
281*fae548d3Szrj break;
282*fae548d3Szrj default:
283*fae548d3Szrj gold_unreachable();
284*fae548d3Szrj }
285*fae548d3Szrj
286*fae548d3Szrj Output_data_space* poc;
287*fae548d3Szrj Output_section* os;
288*fae548d3Szrj
289*fae548d3Szrj if (!parameters->incremental_update())
290*fae548d3Szrj {
291*fae548d3Szrj poc = new Output_data_space(addralign, ds_name);
292*fae548d3Szrj os = layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags,
293*fae548d3Szrj poc, ORDER_INVALID, false);
294*fae548d3Szrj }
295*fae548d3Szrj else
296*fae548d3Szrj {
297*fae548d3Szrj // When doing an incremental update, we need to allocate each common
298*fae548d3Szrj // directly from the output section's free list.
299*fae548d3Szrj poc = NULL;
300*fae548d3Szrj os = layout->find_output_section(name);
301*fae548d3Szrj }
302*fae548d3Szrj
303*fae548d3Szrj if (os != NULL)
304*fae548d3Szrj {
305*fae548d3Szrj if (commons_section_type == COMMONS_SMALL)
306*fae548d3Szrj os->set_is_small_section();
307*fae548d3Szrj else if (commons_section_type == COMMONS_LARGE)
308*fae548d3Szrj os->set_is_large_section();
309*fae548d3Szrj }
310*fae548d3Szrj
311*fae548d3Szrj // Allocate them all.
312*fae548d3Szrj
313*fae548d3Szrj off_t off = 0;
314*fae548d3Szrj for (Commons_type::iterator p = commons->begin();
315*fae548d3Szrj p != commons->end();
316*fae548d3Szrj ++p)
317*fae548d3Szrj {
318*fae548d3Szrj Symbol* sym = *p;
319*fae548d3Szrj if (sym == NULL)
320*fae548d3Szrj break;
321*fae548d3Szrj
322*fae548d3Szrj // Because we followed forwarding symbols above, but we didn't
323*fae548d3Szrj // do it reliably before adding symbols to the list, it is
324*fae548d3Szrj // possible for us to have the same symbol on the list twice.
325*fae548d3Szrj // This can happen in the horrible case where a program defines
326*fae548d3Szrj // a common symbol with the same name as a versioned libc
327*fae548d3Szrj // symbol. That will show up here as a symbol which has already
328*fae548d3Szrj // been allocated and is therefore no longer a common symbol.
329*fae548d3Szrj if (!sym->is_common())
330*fae548d3Szrj continue;
331*fae548d3Szrj
332*fae548d3Szrj Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
333*fae548d3Szrj
334*fae548d3Szrj // Record the symbol in the map file now, before we change its
335*fae548d3Szrj // value. Pass the size in separately so that we don't have to
336*fae548d3Szrj // templatize the map code, which is not performance sensitive.
337*fae548d3Szrj if (mapfile != NULL)
338*fae548d3Szrj mapfile->report_allocate_common(sym, ssym->symsize());
339*fae548d3Szrj
340*fae548d3Szrj if (poc != NULL)
341*fae548d3Szrj {
342*fae548d3Szrj off = align_address(off, ssym->value());
343*fae548d3Szrj ssym->allocate_common(poc, off);
344*fae548d3Szrj off += ssym->symsize();
345*fae548d3Szrj }
346*fae548d3Szrj else
347*fae548d3Szrj {
348*fae548d3Szrj // For an incremental update, allocate from the free list.
349*fae548d3Szrj off = os->allocate(ssym->symsize(), ssym->value());
350*fae548d3Szrj if (off == -1)
351*fae548d3Szrj gold_fallback(_("out of patch space in section %s; "
352*fae548d3Szrj "relink with --incremental-full"),
353*fae548d3Szrj os->name());
354*fae548d3Szrj ssym->allocate_common(os, off);
355*fae548d3Szrj }
356*fae548d3Szrj }
357*fae548d3Szrj
358*fae548d3Szrj if (poc != NULL)
359*fae548d3Szrj poc->set_current_data_size(off);
360*fae548d3Szrj
361*fae548d3Szrj commons->clear();
362*fae548d3Szrj }
363*fae548d3Szrj
364*fae548d3Szrj } // End namespace gold.
365