* config.sub, config.guess: Update from upstream sources.
[binutils.git] / gold / common.cc
blob286a11288550df4a2d51726217b559530af70915
1 // common.cc -- handle common symbols for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <algorithm>
27 #include "workqueue.h"
28 #include "layout.h"
29 #include "output.h"
30 #include "symtab.h"
31 #include "common.h"
33 namespace gold
36 // Allocate_commons_task methods.
38 // This task allocates the common symbols. We need a lock on the
39 // symbol table.
41 Task_token*
42 Allocate_commons_task::is_runnable()
44 if (!this->symtab_lock_->is_writable())
45 return this->symtab_lock_;
46 return NULL;
49 // Return the locks we hold: one on the symbol table, and one blocker.
51 void
52 Allocate_commons_task::locks(Task_locker* tl)
54 tl->add(this, this->blocker_);
55 tl->add(this, this->symtab_lock_);
58 // Allocate the common symbols.
60 void
61 Allocate_commons_task::run(Workqueue*)
63 this->symtab_->allocate_commons(this->options_, this->layout_);
66 // This class is used to sort the common symbol by size. We put the
67 // larger common symbols first.
69 template<int size>
70 class Sort_commons
72 public:
73 Sort_commons(const Symbol_table* symtab)
74 : symtab_(symtab)
75 { }
77 bool operator()(const Symbol* a, const Symbol* b) const;
79 private:
80 const Symbol_table* symtab_;
83 template<int size>
84 bool
85 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
87 if (pa == NULL)
88 return false;
89 if (pb == NULL)
90 return true;
92 const Symbol_table* symtab = this->symtab_;
93 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
94 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
96 typename Sized_symbol<size>::Size_type sa = psa->symsize();
97 typename Sized_symbol<size>::Size_type sb = psb->symsize();
98 if (sa < sb)
99 return false;
100 else if (sb > sa)
101 return true;
103 // When the symbols are the same size, we sort them by alignment.
104 typename Sized_symbol<size>::Value_type va = psa->value();
105 typename Sized_symbol<size>::Value_type vb = psb->value();
106 if (va < vb)
107 return false;
108 else if (vb > va)
109 return true;
111 // Otherwise we stabilize the sort by sorting by name.
112 return strcmp(psa->name(), psb->name()) < 0;
115 // Allocate the common symbols.
117 void
118 Symbol_table::allocate_commons(const General_options& options, Layout* layout)
120 if (parameters->target().get_size() == 32)
122 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
123 this->do_allocate_commons<32>(options, layout);
124 #else
125 gold_unreachable();
126 #endif
128 else if (parameters->target().get_size() == 64)
130 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
131 this->do_allocate_commons<64>(options, layout);
132 #else
133 gold_unreachable();
134 #endif
136 else
137 gold_unreachable();
140 // Allocated the common symbols, sized version.
142 template<int size>
143 void
144 Symbol_table::do_allocate_commons(const General_options&,
145 Layout* layout)
147 typedef typename Sized_symbol<size>::Value_type Value_type;
148 typedef typename Sized_symbol<size>::Size_type Size_type;
150 // We've kept a list of all the common symbols. But the symbol may
151 // have been resolved to a defined symbol by now. And it may be a
152 // forwarder. First remove all non-common symbols.
153 bool any = false;
154 uint64_t addralign = 0;
155 for (Commons_type::iterator p = this->commons_.begin();
156 p != this->commons_.end();
157 ++p)
159 Symbol* sym = *p;
160 if (sym->is_forwarder())
162 sym = this->resolve_forwards(sym);
163 *p = sym;
165 if (!sym->is_common())
166 *p = NULL;
167 else
169 any = true;
170 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
171 if (ssym->value() > addralign)
172 addralign = ssym->value();
175 if (!any)
176 return;
178 // Sort the common symbols by size, so that they pack better into
179 // memory.
180 std::sort(this->commons_.begin(), this->commons_.end(),
181 Sort_commons<size>(this));
183 // Place them in a newly allocated .bss section.
185 Output_data_space *poc = new Output_data_space(addralign);
187 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
188 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
189 poc);
191 // Allocate them all.
193 off_t off = 0;
194 for (Commons_type::iterator p = this->commons_.begin();
195 p != this->commons_.end();
196 ++p)
198 Symbol* sym = *p;
199 if (sym == NULL)
200 break;
201 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
202 off = align_address(off, ssym->value());
203 ssym->allocate_common(poc, off);
204 off += ssym->symsize();
207 poc->set_current_data_size(off);
209 this->commons_.clear();
212 } // End namespace gold.