2008-05-21 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / gold / common.cc
CommitLineData
ead1e424
ILT
1// common.cc -- handle common symbols for gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 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"
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{
155a0dd7 63 this->symtab_->allocate_commons(this->layout_);
ead1e424
ILT
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_;
7d1a9ebb
ILT
93 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
94 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
ead1e424 95
49bdd526 96 // Sort by largest size first.
ead1e424
ILT
97 typename Sized_symbol<size>::Size_type sa = psa->symsize();
98 typename Sized_symbol<size>::Size_type sb = psb->symsize();
99 if (sa < sb)
100 return false;
49bdd526 101 else if (sb < sa)
ead1e424
ILT
102 return true;
103
49bdd526
ILT
104 // When the symbols are the same size, we sort them by alignment,
105 // largest alignment first.
ead1e424
ILT
106 typename Sized_symbol<size>::Value_type va = psa->value();
107 typename Sized_symbol<size>::Value_type vb = psb->value();
108 if (va < vb)
109 return false;
49bdd526 110 else if (vb < va)
ead1e424
ILT
111 return true;
112
113 // Otherwise we stabilize the sort by sorting by name.
114 return strcmp(psa->name(), psb->name()) < 0;
115}
116
117// Allocate the common symbols.
118
119void
155a0dd7 120Symbol_table::allocate_commons(Layout* layout)
ead1e424 121{
8851ecca 122 if (parameters->target().get_size() == 32)
8ae3da90
ILT
123 {
124#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
155a0dd7 125 this->do_allocate_commons<32>(layout);
8ae3da90
ILT
126#else
127 gold_unreachable();
128#endif
129 }
8851ecca 130 else if (parameters->target().get_size() == 64)
8ae3da90
ILT
131 {
132#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
155a0dd7 133 this->do_allocate_commons<64>(layout);
8ae3da90
ILT
134#else
135 gold_unreachable();
136#endif
137 }
ead1e424 138 else
a3ad94ed 139 gold_unreachable();
ead1e424
ILT
140}
141
142// Allocated the common symbols, sized version.
143
144template<int size>
145void
155a0dd7
ILT
146Symbol_table::do_allocate_commons(Layout* layout)
147{
148 this->do_allocate_commons_list<size>(layout, false, &this->commons_);
149 this->do_allocate_commons_list<size>(layout, true, &this->tls_commons_);
150}
151
152// Allocate the common symbols in a list. IS_TLS indicates whether
153// these are TLS common symbols.
154
155template<int size>
156void
157Symbol_table::do_allocate_commons_list(Layout* layout, bool is_tls,
158 Commons_type* commons)
ead1e424
ILT
159{
160 typedef typename Sized_symbol<size>::Value_type Value_type;
161 typedef typename Sized_symbol<size>::Size_type Size_type;
162
163 // We've kept a list of all the common symbols. But the symbol may
164 // have been resolved to a defined symbol by now. And it may be a
165 // forwarder. First remove all non-common symbols.
166 bool any = false;
167 uint64_t addralign = 0;
155a0dd7
ILT
168 for (Commons_type::iterator p = commons->begin();
169 p != commons->end();
ead1e424
ILT
170 ++p)
171 {
172 Symbol* sym = *p;
173 if (sym->is_forwarder())
174 {
175 sym = this->resolve_forwards(sym);
176 *p = sym;
177 }
178 if (!sym->is_common())
179 *p = NULL;
180 else
181 {
182 any = true;
7d1a9ebb 183 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424
ILT
184 if (ssym->value() > addralign)
185 addralign = ssym->value();
186 }
187 }
188 if (!any)
189 return;
190
191 // Sort the common symbols by size, so that they pack better into
192 // memory.
155a0dd7 193 std::sort(commons->begin(), commons->end(),
ead1e424
ILT
194 Sort_commons<size>(this));
195
155a0dd7 196 // Place them in a newly allocated BSS section.
ead1e424 197
a3ad94ed 198 Output_data_space *poc = new Output_data_space(addralign);
ead1e424 199
155a0dd7
ILT
200 const char* name = ".bss";
201 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
202 if (is_tls)
203 {
204 name = ".tbss";
205 flags |= elfcpp::SHF_TLS;
206 }
207 layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags, poc);
ead1e424
ILT
208
209 // Allocate them all.
210
211 off_t off = 0;
155a0dd7
ILT
212 for (Commons_type::iterator p = commons->begin();
213 p != commons->end();
ead1e424
ILT
214 ++p)
215 {
216 Symbol* sym = *p;
217 if (sym == NULL)
218 break;
7d1a9ebb 219 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424 220 off = align_address(off, ssym->value());
c7912668 221 ssym->allocate_common(poc, off);
c7912668 222 off += ssym->symsize();
ead1e424
ILT
223 }
224
27bc2bce 225 poc->set_current_data_size(off);
ead1e424 226
155a0dd7 227 commons->clear();
ead1e424
ILT
228}
229
230} // End namespace gold.
This page took 0.086724 seconds and 4 git commands to generate.