28ca2f6dab85e73862068e214dc3551a69c249ab
[deliverable/binutils-gdb.git] / gold / common.cc
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"
10 #include "common.h"
11
12 namespace gold
13 {
14
15 // Allocate_commons_task methods.
16
17 // This task allocates the common symbols. We need a lock on the
18 // symbol table.
19
20 Task::Is_runnable_type
21 Allocate_commons_task::is_runnable(Workqueue*)
22 {
23 if (!this->symtab_lock_->is_writable())
24 return IS_LOCKED;
25 return IS_RUNNABLE;
26 }
27
28 // Return the locks we hold: one on the symbol table, and one blocker.
29
30 class Allocate_commons_task::Allocate_commons_locker : public Task_locker
31 {
32 public:
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)
37 { }
38
39 private:
40 Task_locker_write symtab_locker_;
41 Task_locker_block blocker_;
42 };
43
44 Task_locker*
45 Allocate_commons_task::locks(Workqueue* workqueue)
46 {
47 return new Allocate_commons_locker(*this->symtab_lock_, this,
48 *this->blocker_, workqueue);
49 }
50
51 // Allocate the common symbols.
52
53 void
54 Allocate_commons_task::run(Workqueue*)
55 {
56 this->symtab_->allocate_commons(this->options_, this->layout_);
57 }
58
59 // This class is used to sort the common symbol by size. We put the
60 // larger common symbols first.
61
62 template<int size>
63 class Sort_commons
64 {
65 public:
66 Sort_commons(const Symbol_table* symtab)
67 : symtab_(symtab)
68 { }
69
70 bool operator()(const Symbol* a, const Symbol* b) const;
71
72 private:
73 const Symbol_table* symtab_;
74 };
75
76 template<int size>
77 bool
78 Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
79 {
80 if (pa == NULL)
81 return false;
82 if (pb == NULL)
83 return true;
84
85 const Symbol_table* symtab = this->symtab_;
86 const Sized_symbol<size>* psa;
87 psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa
88 SELECT_SIZE(size));
89 const Sized_symbol<size>* psb;
90 psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb
91 SELECT_SIZE(size));
92
93 typename Sized_symbol<size>::Size_type sa = psa->symsize();
94 typename Sized_symbol<size>::Size_type sb = psb->symsize();
95 if (sa < sb)
96 return false;
97 else if (sb > sa)
98 return true;
99
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();
103 if (va < vb)
104 return false;
105 else if (vb > va)
106 return true;
107
108 // Otherwise we stabilize the sort by sorting by name.
109 return strcmp(psa->name(), psb->name()) < 0;
110 }
111
112 // Allocate the common symbols.
113
114 void
115 Symbol_table::allocate_commons(const General_options& options, Layout* layout)
116 {
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);
121 else
122 abort();
123 }
124
125 // Allocated the common symbols, sized version.
126
127 template<int size>
128 void
129 Symbol_table::do_allocate_commons(const General_options&,
130 Layout* layout)
131 {
132 typedef typename Sized_symbol<size>::Value_type Value_type;
133 typedef typename Sized_symbol<size>::Size_type Size_type;
134
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.
138 bool any = false;
139 uint64_t addralign = 0;
140 for (Commons_type::iterator p = this->commons_.begin();
141 p != this->commons_.end();
142 ++p)
143 {
144 Symbol* sym = *p;
145 if (sym->is_forwarder())
146 {
147 sym = this->resolve_forwards(sym);
148 *p = sym;
149 }
150 if (!sym->is_common())
151 *p = NULL;
152 else
153 {
154 any = true;
155 Sized_symbol<size>* ssym;
156 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
157 sym
158 SELECT_SIZE(size));
159 if (ssym->value() > addralign)
160 addralign = ssym->value();
161 }
162 }
163 if (!any)
164 return;
165
166 // Sort the common symbols by size, so that they pack better into
167 // memory.
168 std::sort(this->commons_.begin(), this->commons_.end(),
169 Sort_commons<size>(this));
170
171 // Place them in a newly allocated .bss section.
172
173 Output_section_common *poc = new Output_section_common(addralign);
174
175 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
176 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
177 poc);
178
179 // Allocate them all.
180
181 off_t off = 0;
182 for (Commons_type::iterator p = this->commons_.begin();
183 p != this->commons_.end();
184 ++p)
185 {
186 Symbol* sym = *p;
187 if (sym == NULL)
188 break;
189
190 Sized_symbol<size>* ssym;
191 ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
192 SELECT_SIZE(size));
193
194 off = align_address(off, ssym->value());
195
196 Size_type symsize = ssym->symsize();
197 ssym->init(ssym->name(), poc, off, symsize, ssym->type(),
198 ssym->binding(), ssym->visibility(), ssym->nonvis(),
199 false);
200
201 off += symsize;
202 }
203
204 poc->set_common_size(off);
205
206 this->commons_.clear();
207 }
208
209 } // End namespace gold.
This page took 0.033603 seconds and 4 git commands to generate.