* common.cc (Sort_commons::operator): Correct sorting algorithm.
[deliverable/binutils-gdb.git] / gold / common.cc
1 // common.cc -- handle common symbols for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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
23 #include "gold.h"
24
25 #include <algorithm>
26
27 #include "workqueue.h"
28 #include "layout.h"
29 #include "output.h"
30 #include "symtab.h"
31 #include "common.h"
32
33 namespace 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
41 Task_token*
42 Allocate_commons_task::is_runnable()
43 {
44 if (!this->symtab_lock_->is_writable())
45 return this->symtab_lock_;
46 return NULL;
47 }
48
49 // Return the locks we hold: one on the symbol table, and one blocker.
50
51 void
52 Allocate_commons_task::locks(Task_locker* tl)
53 {
54 tl->add(this, this->blocker_);
55 tl->add(this, this->symtab_lock_);
56 }
57
58 // Allocate the common symbols.
59
60 void
61 Allocate_commons_task::run(Workqueue*)
62 {
63 this->symtab_->allocate_commons(this->options_, this->layout_);
64 }
65
66 // This class is used to sort the common symbol by size. We put the
67 // larger common symbols first.
68
69 template<int size>
70 class 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
83 template<int size>
84 bool
85 Sort_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_;
93 const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
94 const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
95
96 // Sort by largest size first.
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;
101 else if (sb < sa)
102 return true;
103
104 // When the symbols are the same size, we sort them by alignment,
105 // largest alignment first.
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;
110 else if (vb < va)
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
119 void
120 Symbol_table::allocate_commons(const General_options& options, Layout* layout)
121 {
122 if (parameters->target().get_size() == 32)
123 {
124 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
125 this->do_allocate_commons<32>(options, layout);
126 #else
127 gold_unreachable();
128 #endif
129 }
130 else if (parameters->target().get_size() == 64)
131 {
132 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
133 this->do_allocate_commons<64>(options, layout);
134 #else
135 gold_unreachable();
136 #endif
137 }
138 else
139 gold_unreachable();
140 }
141
142 // Allocated the common symbols, sized version.
143
144 template<int size>
145 void
146 Symbol_table::do_allocate_commons(const General_options&,
147 Layout* layout)
148 {
149 typedef typename Sized_symbol<size>::Value_type Value_type;
150 typedef typename Sized_symbol<size>::Size_type Size_type;
151
152 // We've kept a list of all the common symbols. But the symbol may
153 // have been resolved to a defined symbol by now. And it may be a
154 // forwarder. First remove all non-common symbols.
155 bool any = false;
156 uint64_t addralign = 0;
157 for (Commons_type::iterator p = this->commons_.begin();
158 p != this->commons_.end();
159 ++p)
160 {
161 Symbol* sym = *p;
162 if (sym->is_forwarder())
163 {
164 sym = this->resolve_forwards(sym);
165 *p = sym;
166 }
167 if (!sym->is_common())
168 *p = NULL;
169 else
170 {
171 any = true;
172 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
173 if (ssym->value() > addralign)
174 addralign = ssym->value();
175 }
176 }
177 if (!any)
178 return;
179
180 // Sort the common symbols by size, so that they pack better into
181 // memory.
182 std::sort(this->commons_.begin(), this->commons_.end(),
183 Sort_commons<size>(this));
184
185 // Place them in a newly allocated .bss section.
186
187 Output_data_space *poc = new Output_data_space(addralign);
188
189 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
190 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
191 poc);
192
193 // Allocate them all.
194
195 off_t off = 0;
196 for (Commons_type::iterator p = this->commons_.begin();
197 p != this->commons_.end();
198 ++p)
199 {
200 Symbol* sym = *p;
201 if (sym == NULL)
202 break;
203 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
204 off = align_address(off, ssym->value());
205 ssym->allocate_common(poc, off);
206 off += ssym->symsize();
207 }
208
209 poc->set_current_data_size(off);
210
211 this->commons_.clear();
212 }
213
214 } // End namespace gold.
This page took 0.037001 seconds and 5 git commands to generate.