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