More dynamic object support, initial scripting support.
[external/binutils.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 "symtab.h"
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;
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));
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)
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     abort();
124 }
125
126 // Allocated the common symbols, sized version.
127
128 template<int size>
129 void
130 Symbol_table::do_allocate_commons(const General_options&,
131                                   Layout* layout)
132 {
133   typedef typename Sized_symbol<size>::Value_type Value_type;
134   typedef typename Sized_symbol<size>::Size_type Size_type;
135
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)
144     {
145       Symbol* sym = *p;
146       if (sym->is_forwarder())
147         {
148           sym = this->resolve_forwards(sym);
149           *p = sym;
150         }
151       if (!sym->is_common())
152         *p = NULL;
153       else
154         {
155           any = true;
156           Sized_symbol<size>* ssym;
157           ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
158               sym
159               SELECT_SIZE(size));
160           if (ssym->value() > addralign)
161             addralign = ssym->value();
162         }
163     }
164   if (!any)
165     return;
166
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));
171
172   // Place them in a newly allocated .bss section.
173
174   Output_data_common *poc = new Output_data_common(addralign);
175
176   layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
177                                   elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
178                                   poc);
179
180   // Allocate them all.
181
182   off_t off = 0;
183   for (Commons_type::iterator p = this->commons_.begin();
184        p != this->commons_.end();
185        ++p)
186     {
187       Symbol* sym = *p;
188       if (sym == NULL)
189         break;
190
191       Sized_symbol<size>* ssym;
192       ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
193                                                             SELECT_SIZE(size));
194
195       off = align_address(off, ssym->value());
196
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);
201
202       off += symsize;
203     }
204
205   poc->set_common_size(off);
206
207   this->commons_.clear();
208 }
209
210 } // End namespace gold.