cpu/
[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"
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
34namespace gold
35{
36
37// Allocate_commons_task methods.
38
39// This task allocates the common symbols. We need a lock on the
40// symbol table.
41
17a1d0a9
ILT
42Task_token*
43Allocate_commons_task::is_runnable()
ead1e424
ILT
44{
45 if (!this->symtab_lock_->is_writable())
17a1d0a9
ILT
46 return this->symtab_lock_;
47 return NULL;
ead1e424
ILT
48}
49
50// Return the locks we hold: one on the symbol table, and one blocker.
51
17a1d0a9
ILT
52void
53Allocate_commons_task::locks(Task_locker* tl)
ead1e424 54{
17a1d0a9
ILT
55 tl->add(this, this->blocker_);
56 tl->add(this, this->symtab_lock_);
ead1e424
ILT
57}
58
59// Allocate the common symbols.
60
61void
62Allocate_commons_task::run(Workqueue*)
63{
7d9e3d98 64 this->symtab_->allocate_commons(this->layout_, this->mapfile_);
ead1e424
ILT
65}
66
67// This class is used to sort the common symbol by size. We put the
68// larger common symbols first.
69
70template<int size>
71class Sort_commons
72{
73 public:
74 Sort_commons(const Symbol_table* symtab)
75 : symtab_(symtab)
76 { }
77
78 bool operator()(const Symbol* a, const Symbol* b) const;
79
80 private:
81 const Symbol_table* symtab_;
82};
83
84template<int size>
85bool
86Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
87{
88 if (pa == NULL)
89 return false;
90 if (pb == NULL)
91 return true;
92
93 const Symbol_table* symtab = this->symtab_;
7d1a9ebb
ILT
94 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
95 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
ead1e424 96
49bdd526 97 // Sort by largest size first.
ead1e424
ILT
98 typename Sized_symbol<size>::Size_type sa = psa->symsize();
99 typename Sized_symbol<size>::Size_type sb = psb->symsize();
100 if (sa < sb)
101 return false;
49bdd526 102 else if (sb < sa)
ead1e424
ILT
103 return true;
104
49bdd526
ILT
105 // When the symbols are the same size, we sort them by alignment,
106 // largest alignment first.
ead1e424
ILT
107 typename Sized_symbol<size>::Value_type va = psa->value();
108 typename Sized_symbol<size>::Value_type vb = psb->value();
109 if (va < vb)
110 return false;
49bdd526 111 else if (vb < va)
ead1e424
ILT
112 return true;
113
114 // Otherwise we stabilize the sort by sorting by name.
115 return strcmp(psa->name(), psb->name()) < 0;
116}
117
118// Allocate the common symbols.
119
120void
7d9e3d98 121Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
ead1e424 122{
8851ecca 123 if (parameters->target().get_size() == 32)
8ae3da90
ILT
124 {
125#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
7d9e3d98 126 this->do_allocate_commons<32>(layout, mapfile);
8ae3da90
ILT
127#else
128 gold_unreachable();
129#endif
130 }
8851ecca 131 else if (parameters->target().get_size() == 64)
8ae3da90
ILT
132 {
133#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
7d9e3d98 134 this->do_allocate_commons<64>(layout, mapfile);
8ae3da90
ILT
135#else
136 gold_unreachable();
137#endif
138 }
ead1e424 139 else
a3ad94ed 140 gold_unreachable();
ead1e424
ILT
141}
142
143// Allocated the common symbols, sized version.
144
145template<int size>
146void
7d9e3d98 147Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile)
155a0dd7 148{
7d9e3d98
ILT
149 this->do_allocate_commons_list<size>(layout, false, &this->commons_,
150 mapfile);
151 this->do_allocate_commons_list<size>(layout, true, &this->tls_commons_,
152 mapfile);
155a0dd7
ILT
153}
154
155// Allocate the common symbols in a list. IS_TLS indicates whether
156// these are TLS common symbols.
157
158template<int size>
159void
160Symbol_table::do_allocate_commons_list(Layout* layout, bool is_tls,
7d9e3d98
ILT
161 Commons_type* commons,
162 Mapfile* mapfile)
ead1e424
ILT
163{
164 typedef typename Sized_symbol<size>::Value_type Value_type;
165 typedef typename Sized_symbol<size>::Size_type Size_type;
166
167 // We've kept a list of all the common symbols. But the symbol may
168 // have been resolved to a defined symbol by now. And it may be a
169 // forwarder. First remove all non-common symbols.
170 bool any = false;
171 uint64_t addralign = 0;
155a0dd7
ILT
172 for (Commons_type::iterator p = commons->begin();
173 p != commons->end();
ead1e424
ILT
174 ++p)
175 {
176 Symbol* sym = *p;
177 if (sym->is_forwarder())
178 {
179 sym = this->resolve_forwards(sym);
180 *p = sym;
181 }
182 if (!sym->is_common())
183 *p = NULL;
184 else
185 {
186 any = true;
7d1a9ebb 187 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
ead1e424
ILT
188 if (ssym->value() > addralign)
189 addralign = ssym->value();
190 }
191 }
192 if (!any)
193 return;
194
195 // Sort the common symbols by size, so that they pack better into
196 // memory.
155a0dd7 197 std::sort(commons->begin(), commons->end(),
ead1e424
ILT
198 Sort_commons<size>(this));
199
155a0dd7 200 // Place them in a newly allocated BSS section.
ead1e424 201
7d9e3d98
ILT
202 Output_data_space *poc = new Output_data_space(addralign,
203 (is_tls
204 ? "** tls common"
205 : "** common"));
ead1e424 206
155a0dd7
ILT
207 const char* name = ".bss";
208 elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
209 if (is_tls)
210 {
211 name = ".tbss";
212 flags |= elfcpp::SHF_TLS;
213 }
214 layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags, poc);
ead1e424
ILT
215
216 // Allocate them all.
217
218 off_t off = 0;
155a0dd7
ILT
219 for (Commons_type::iterator p = commons->begin();
220 p != commons->end();
ead1e424
ILT
221 ++p)
222 {
223 Symbol* sym = *p;
224 if (sym == NULL)
225 break;
7d1a9ebb 226 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
7d9e3d98
ILT
227
228 // Record the symbol in the map file now, before we change its
229 // value. Pass the size in separately so that we don't have to
230 // templatize the map code, which is not performance sensitive.
231 if (mapfile != NULL)
232 mapfile->report_allocate_common(sym, ssym->symsize());
233
ead1e424 234 off = align_address(off, ssym->value());
c7912668 235 ssym->allocate_common(poc, off);
c7912668 236 off += ssym->symsize();
ead1e424
ILT
237 }
238
27bc2bce 239 poc->set_current_data_size(off);
ead1e424 240
155a0dd7 241 commons->clear();
ead1e424
ILT
242}
243
244} // End namespace gold.
This page took 0.166758 seconds and 4 git commands to generate.