2007-04-18 Matthias Klose <doko@ubuntu.com>
[binutils.git] / gold / common.cc
blobe83219c901f0d8624d3ab219fc12837f0f59a227
1 // common.cc -- handle common symbols for gold
3 #include "gold.h"
5 #include <algorithm>
7 #include "workqueue.h"
8 #include "layout.h"
9 #include "output.h"
10 #include "symtab.h"
11 #include "common.h"
13 namespace gold
16 // Allocate_commons_task methods.
18 // This task allocates the common symbols. We need a lock on the
19 // symbol table.
21 Task::Is_runnable_type
22 Allocate_commons_task::is_runnable(Workqueue*)
24 if (!this->symtab_lock_->is_writable())
25 return IS_LOCKED;
26 return IS_RUNNABLE;
29 // Return the locks we hold: one on the symbol table, and one blocker.
31 class Allocate_commons_task::Allocate_commons_locker : public Task_locker
33 public:
34 Allocate_commons_locker(Task_token& symtab_lock, Task* task,
35 Task_token& blocker, Workqueue* workqueue)
36 : symtab_locker_(symtab_lock, task),
37 blocker_(blocker, workqueue)
38 { }
40 private:
41 Task_locker_write symtab_locker_;
42 Task_locker_block blocker_;
45 Task_locker*
46 Allocate_commons_task::locks(Workqueue* workqueue)
48 return new Allocate_commons_locker(*this->symtab_lock_, this,
49 *this->blocker_, workqueue);
52 // Allocate the common symbols.
54 void
55 Allocate_commons_task::run(Workqueue*)
57 this->symtab_->allocate_commons(this->options_, this->layout_);
60 // This class is used to sort the common symbol by size. We put the
61 // larger common symbols first.
63 template<int size>
64 class Sort_commons
66 public:
67 Sort_commons(const Symbol_table* symtab)
68 : symtab_(symtab)
69 { }
71 bool operator()(const Symbol* a, const Symbol* b) const;
73 private:
74 const Symbol_table* symtab_;
77 template<int size>
78 bool
79 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
81 if (pa == NULL)
82 return false;
83 if (pb == NULL)
84 return true;
86 const Symbol_table* symtab = this->symtab_;
87 const Sized_symbol<size>* psa;
88 psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa
89 SELECT_SIZE(size));
90 const Sized_symbol<size>* psb;
91 psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb
92 SELECT_SIZE(size));
94 typename Sized_symbol<size>::Size_type sa = psa->symsize();
95 typename Sized_symbol<size>::Size_type sb = psb->symsize();
96 if (sa < sb)
97 return false;
98 else if (sb > sa)
99 return true;
101 // When the symbols are the same size, we sort them by alignment.
102 typename Sized_symbol<size>::Value_type va = psa->value();
103 typename Sized_symbol<size>::Value_type vb = psb->value();
104 if (va < vb)
105 return false;
106 else if (vb > va)
107 return true;
109 // Otherwise we stabilize the sort by sorting by name.
110 return strcmp(psa->name(), psb->name()) < 0;
113 // Allocate the common symbols.
115 void
116 Symbol_table::allocate_commons(const General_options& options, Layout* layout)
118 if (this->get_size() == 32)
119 this->do_allocate_commons<32>(options, layout);
120 else if (this->get_size() == 64)
121 this->do_allocate_commons<64>(options, layout);
122 else
123 gold_unreachable();
126 // Allocated the common symbols, sized version.
128 template<int size>
129 void
130 Symbol_table::do_allocate_commons(const General_options&,
131 Layout* layout)
133 typedef typename Sized_symbol<size>::Value_type Value_type;
134 typedef typename Sized_symbol<size>::Size_type Size_type;
136 // We've kept a list of all the common symbols. But the symbol may
137 // have been resolved to a defined symbol by now. And it may be a
138 // forwarder. First remove all non-common symbols.
139 bool any = false;
140 uint64_t addralign = 0;
141 for (Commons_type::iterator p = this->commons_.begin();
142 p != this->commons_.end();
143 ++p)
145 Symbol* sym = *p;
146 if (sym->is_forwarder())
148 sym = this->resolve_forwards(sym);
149 *p = sym;
151 if (!sym->is_common())
152 *p = NULL;
153 else
155 any = true;
156 Sized_symbol<size>* ssym;
157 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
159 SELECT_SIZE(size));
160 if (ssym->value() > addralign)
161 addralign = ssym->value();
164 if (!any)
165 return;
167 // Sort the common symbols by size, so that they pack better into
168 // memory.
169 std::sort(this->commons_.begin(), this->commons_.end(),
170 Sort_commons<size>(this));
172 // Place them in a newly allocated .bss section.
174 Output_data_space *poc = new Output_data_space(addralign);
176 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
177 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
178 poc);
180 // Allocate them all.
182 off_t off = 0;
183 for (Commons_type::iterator p = this->commons_.begin();
184 p != this->commons_.end();
185 ++p)
187 Symbol* sym = *p;
188 if (sym == NULL)
189 break;
191 Sized_symbol<size>* ssym;
192 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
193 SELECT_SIZE(size));
195 off = align_address(off, ssym->value());
197 Size_type symsize = ssym->symsize();
198 ssym->init(ssym->name(), poc, off, symsize, ssym->type(),
199 ssym->binding(), ssym->visibility(), ssym->nonvis(),
200 false);
202 off += symsize;
205 poc->set_space_size(off);
207 this->commons_.clear();
210 } // End namespace gold.