Commit | Line | Data |
---|---|---|
ead1e424 ILT |
1 | // common.cc -- handle common symbols for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <algorithm> | |
6 | ||
7 | #include "workqueue.h" | |
8 | #include "layout.h" | |
9 | #include "output.h" | |
f6ce93d6 | 10 | #include "symtab.h" |
ead1e424 ILT |
11 | #include "common.h" |
12 | ||
13 | namespace gold | |
14 | { | |
15 | ||
16 | // Allocate_commons_task methods. | |
17 | ||
18 | // This task allocates the common symbols. We need a lock on the | |
19 | // symbol table. | |
20 | ||
21 | Task::Is_runnable_type | |
22 | Allocate_commons_task::is_runnable(Workqueue*) | |
23 | { | |
24 | if (!this->symtab_lock_->is_writable()) | |
25 | return IS_LOCKED; | |
26 | return IS_RUNNABLE; | |
27 | } | |
28 | ||
29 | // Return the locks we hold: one on the symbol table, and one blocker. | |
30 | ||
31 | class Allocate_commons_task::Allocate_commons_locker : public Task_locker | |
32 | { | |
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 | { } | |
39 | ||
40 | private: | |
41 | Task_locker_write symtab_locker_; | |
42 | Task_locker_block blocker_; | |
43 | }; | |
44 | ||
45 | Task_locker* | |
46 | Allocate_commons_task::locks(Workqueue* workqueue) | |
47 | { | |
48 | return new Allocate_commons_locker(*this->symtab_lock_, this, | |
49 | *this->blocker_, workqueue); | |
50 | } | |
51 | ||
52 | // Allocate the common symbols. | |
53 | ||
54 | void | |
55 | Allocate_commons_task::run(Workqueue*) | |
56 | { | |
57 | this->symtab_->allocate_commons(this->options_, this->layout_); | |
58 | } | |
59 | ||
60 | // This class is used to sort the common symbol by size. We put the | |
61 | // larger common symbols first. | |
62 | ||
63 | template<int size> | |
64 | class Sort_commons | |
65 | { | |
66 | public: | |
67 | Sort_commons(const Symbol_table* symtab) | |
68 | : symtab_(symtab) | |
69 | { } | |
70 | ||
71 | bool operator()(const Symbol* a, const Symbol* b) const; | |
72 | ||
73 | private: | |
74 | const Symbol_table* symtab_; | |
75 | }; | |
76 | ||
77 | template<int size> | |
78 | bool | |
79 | Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const | |
80 | { | |
81 | if (pa == NULL) | |
82 | return false; | |
83 | if (pb == NULL) | |
84 | return true; | |
85 | ||
86 | const Symbol_table* symtab = this->symtab_; | |
87 | const Sized_symbol<size>* psa; | |
593f47df ILT |
88 | psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa |
89 | SELECT_SIZE(size)); | |
ead1e424 | 90 | const Sized_symbol<size>* psb; |
593f47df ILT |
91 | psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb |
92 | SELECT_SIZE(size)); | |
ead1e424 ILT |
93 | |
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; | |
100 | ||
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; | |
108 | ||
109 | // Otherwise we stabilize the sort by sorting by name. | |
110 | return strcmp(psa->name(), psb->name()) < 0; | |
111 | } | |
112 | ||
113 | // Allocate the common symbols. | |
114 | ||
115 | void | |
116 | Symbol_table::allocate_commons(const General_options& options, Layout* layout) | |
117 | { | |
118 | if (this->get_size() == 32) | |
8ae3da90 ILT |
119 | { |
120 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
121 | this->do_allocate_commons<32>(options, layout); | |
122 | #else | |
123 | gold_unreachable(); | |
124 | #endif | |
125 | } | |
ead1e424 | 126 | else if (this->get_size() == 64) |
8ae3da90 ILT |
127 | { |
128 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
129 | this->do_allocate_commons<64>(options, layout); | |
130 | #else | |
131 | gold_unreachable(); | |
132 | #endif | |
133 | } | |
ead1e424 | 134 | else |
a3ad94ed | 135 | gold_unreachable(); |
ead1e424 ILT |
136 | } |
137 | ||
138 | // Allocated the common symbols, sized version. | |
139 | ||
140 | template<int size> | |
141 | void | |
142 | Symbol_table::do_allocate_commons(const General_options&, | |
143 | Layout* layout) | |
144 | { | |
145 | typedef typename Sized_symbol<size>::Value_type Value_type; | |
146 | typedef typename Sized_symbol<size>::Size_type Size_type; | |
147 | ||
148 | // We've kept a list of all the common symbols. But the symbol may | |
149 | // have been resolved to a defined symbol by now. And it may be a | |
150 | // forwarder. First remove all non-common symbols. | |
151 | bool any = false; | |
152 | uint64_t addralign = 0; | |
153 | for (Commons_type::iterator p = this->commons_.begin(); | |
154 | p != this->commons_.end(); | |
155 | ++p) | |
156 | { | |
157 | Symbol* sym = *p; | |
158 | if (sym->is_forwarder()) | |
159 | { | |
160 | sym = this->resolve_forwards(sym); | |
161 | *p = sym; | |
162 | } | |
163 | if (!sym->is_common()) | |
164 | *p = NULL; | |
165 | else | |
166 | { | |
167 | any = true; | |
168 | Sized_symbol<size>* ssym; | |
593f47df ILT |
169 | ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
170 | sym | |
171 | SELECT_SIZE(size)); | |
ead1e424 ILT |
172 | if (ssym->value() > addralign) |
173 | addralign = ssym->value(); | |
174 | } | |
175 | } | |
176 | if (!any) | |
177 | return; | |
178 | ||
179 | // Sort the common symbols by size, so that they pack better into | |
180 | // memory. | |
181 | std::sort(this->commons_.begin(), this->commons_.end(), | |
182 | Sort_commons<size>(this)); | |
183 | ||
184 | // Place them in a newly allocated .bss section. | |
185 | ||
a3ad94ed | 186 | Output_data_space *poc = new Output_data_space(addralign); |
ead1e424 ILT |
187 | |
188 | layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS, | |
189 | elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC, | |
190 | poc); | |
191 | ||
192 | // Allocate them all. | |
193 | ||
194 | off_t off = 0; | |
195 | for (Commons_type::iterator p = this->commons_.begin(); | |
196 | p != this->commons_.end(); | |
197 | ++p) | |
198 | { | |
199 | Symbol* sym = *p; | |
200 | if (sym == NULL) | |
201 | break; | |
202 | ||
203 | Sized_symbol<size>* ssym; | |
593f47df ILT |
204 | ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym |
205 | SELECT_SIZE(size)); | |
ead1e424 ILT |
206 | |
207 | off = align_address(off, ssym->value()); | |
208 | ||
209 | Size_type symsize = ssym->symsize(); | |
210 | ssym->init(ssym->name(), poc, off, symsize, ssym->type(), | |
211 | ssym->binding(), ssym->visibility(), ssym->nonvis(), | |
212 | false); | |
213 | ||
214 | off += symsize; | |
215 | } | |
216 | ||
a3ad94ed | 217 | poc->set_space_size(off); |
ead1e424 ILT |
218 | |
219 | this->commons_.clear(); | |
220 | } | |
221 | ||
222 | } // End namespace gold. |