1 // common.cc -- handle common symbols for gold
16 // Allocate_commons_task methods.
18 // This task allocates the common symbols. We need a lock on the
21 Task::Is_runnable_type
22 Allocate_commons_task::is_runnable(Workqueue*)
24 if (!this->symtab_lock_->is_writable())
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
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)
41 Task_locker_write symtab_locker_;
42 Task_locker_block blocker_;
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.
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.
67 Sort_commons(const Symbol_table* symtab)
71 bool operator()(const Symbol* a, const Symbol* b) const;
74 const Symbol_table* symtab_;
79 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
86 const Symbol_table* symtab = this->symtab_;
87 const Sized_symbol<size>* psa;
88 psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa
90 const Sized_symbol<size>* psb;
91 psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb
94 typename Sized_symbol<size>::Size_type sa = psa->symsize();
95 typename Sized_symbol<size>::Size_type sb = psb->symsize();
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();
109 // Otherwise we stabilize the sort by sorting by name.
110 return strcmp(psa->name(), psb->name()) < 0;
113 // Allocate the common symbols.
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);
126 // Allocated the common symbols, sized version.
130 Symbol_table::do_allocate_commons(const General_options&,
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.
140 uint64_t addralign = 0;
141 for (Commons_type::iterator p = this->commons_.begin();
142 p != this->commons_.end();
146 if (sym->is_forwarder())
148 sym = this->resolve_forwards(sym);
151 if (!sym->is_common())
156 Sized_symbol<size>* ssym;
157 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
160 if (ssym->value() > addralign)
161 addralign = ssym->value();
167 // Sort the common symbols by size, so that they pack better into
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_common *poc = new Output_data_common(addralign);
176 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
177 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
180 // Allocate them all.
183 for (Commons_type::iterator p = this->commons_.begin();
184 p != this->commons_.end();
191 Sized_symbol<size>* ssym;
192 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
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(),
205 poc->set_common_size(off);
207 this->commons_.clear();
210 } // End namespace gold.