1 // common.cc -- handle common symbols for gold
15 // Allocate_commons_task methods.
17 // This task allocates the common symbols. We need a lock on the
20 Task::Is_runnable_type
21 Allocate_commons_task::is_runnable(Workqueue
*)
23 if (!this->symtab_lock_
->is_writable())
28 // Return the locks we hold: one on the symbol table, and one blocker.
30 class Allocate_commons_task::Allocate_commons_locker
: public Task_locker
33 Allocate_commons_locker(Task_token
& symtab_lock
, Task
* task
,
34 Task_token
& blocker
, Workqueue
* workqueue
)
35 : symtab_locker_(symtab_lock
, task
),
36 blocker_(blocker
, workqueue
)
40 Task_locker_write symtab_locker_
;
41 Task_locker_block blocker_
;
45 Allocate_commons_task::locks(Workqueue
* workqueue
)
47 return new Allocate_commons_locker(*this->symtab_lock_
, this,
48 *this->blocker_
, workqueue
);
51 // Allocate the common symbols.
54 Allocate_commons_task::run(Workqueue
*)
56 this->symtab_
->allocate_commons(this->options_
, this->layout_
);
59 // This class is used to sort the common symbol by size. We put the
60 // larger common symbols first.
66 Sort_commons(const Symbol_table
* symtab
)
70 bool operator()(const Symbol
* a
, const Symbol
* b
) const;
73 const Symbol_table
* symtab_
;
78 Sort_commons
<size
>::operator()(const Symbol
* pa
, const Symbol
* pb
) const
85 const Symbol_table
* symtab
= this->symtab_
;
86 const Sized_symbol
<size
>* psa
;
87 psa
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(size
) (pa
89 const Sized_symbol
<size
>* psb
;
90 psb
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(size
) (pb
93 typename Sized_symbol
<size
>::Size_type sa
= psa
->symsize();
94 typename Sized_symbol
<size
>::Size_type sb
= psb
->symsize();
100 // When the symbols are the same size, we sort them by alignment.
101 typename Sized_symbol
<size
>::Value_type va
= psa
->value();
102 typename Sized_symbol
<size
>::Value_type vb
= psb
->value();
108 // Otherwise we stabilize the sort by sorting by name.
109 return strcmp(psa
->name(), psb
->name()) < 0;
112 // Allocate the common symbols.
115 Symbol_table::allocate_commons(const General_options
& options
, Layout
* layout
)
117 if (this->get_size() == 32)
118 this->do_allocate_commons
<32>(options
, layout
);
119 else if (this->get_size() == 64)
120 this->do_allocate_commons
<64>(options
, layout
);
125 // Allocated the common symbols, sized version.
129 Symbol_table::do_allocate_commons(const General_options
&,
132 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
133 typedef typename Sized_symbol
<size
>::Size_type Size_type
;
135 // We've kept a list of all the common symbols. But the symbol may
136 // have been resolved to a defined symbol by now. And it may be a
137 // forwarder. First remove all non-common symbols.
139 uint64_t addralign
= 0;
140 for (Commons_type::iterator p
= this->commons_
.begin();
141 p
!= this->commons_
.end();
145 if (sym
->is_forwarder())
147 sym
= this->resolve_forwards(sym
);
150 if (!sym
->is_common())
155 Sized_symbol
<size
>* ssym
;
156 ssym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
159 if (ssym
->value() > addralign
)
160 addralign
= ssym
->value();
166 // Sort the common symbols by size, so that they pack better into
168 std::sort(this->commons_
.begin(), this->commons_
.end(),
169 Sort_commons
<size
>(this));
171 // Place them in a newly allocated .bss section.
173 Output_section_common
*poc
= new Output_section_common(addralign
);
175 layout
->add_output_section_data(".bss", elfcpp::SHT_NOBITS
,
176 elfcpp::SHF_WRITE
| elfcpp::SHF_ALLOC
,
179 // Allocate them all.
182 for (Commons_type::iterator p
= this->commons_
.begin();
183 p
!= this->commons_
.end();
190 Sized_symbol
<size
>* ssym
;
191 ssym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (sym
194 off
= align_address(off
, ssym
->value());
196 Size_type symsize
= ssym
->symsize();
197 ssym
->init(ssym
->name(), poc
, off
, symsize
, ssym
->type(),
198 ssym
->binding(), ssym
->visibility(), ssym
->nonvis(),
204 poc
->set_common_size(off
);
206 this->commons_
.clear();
209 } // End namespace gold.