Can now do a full static link of hello, world in C or C++
[deliverable/binutils-gdb.git] / gold / common.cc
CommitLineData
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"
10#include "common.h"
11
12namespace 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
20Task::Is_runnable_type
21Allocate_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
30class 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
44Task_locker*
45Allocate_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
53void
54Allocate_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
62template<int size>
63class 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
76template<int size>
77bool
78Sort_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 (pa
88 SELECT_SIZE(size));
89 const Sized_symbol<size>* psb;
90 psb = symtab->get_sized_symbol SELECT_SIZE_NAME (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
114void
115Symbol_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
127template<int size>
128void
129Symbol_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 (sym
157 SELECT_SIZE(size));
158 if (ssym->value() > addralign)
159 addralign = ssym->value();
160 }
161 }
162 if (!any)
163 return;
164
165 // Sort the common symbols by size, so that they pack better into
166 // memory.
167 std::sort(this->commons_.begin(), this->commons_.end(),
168 Sort_commons<size>(this));
169
170 // Place them in a newly allocated .bss section.
171
172 Output_section_common *poc = new Output_section_common(addralign);
173
174 layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
175 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
176 poc);
177
178 // Allocate them all.
179
180 off_t off = 0;
181 for (Commons_type::iterator p = this->commons_.begin();
182 p != this->commons_.end();
183 ++p)
184 {
185 Symbol* sym = *p;
186 if (sym == NULL)
187 break;
188
189 Sized_symbol<size>* ssym;
190 ssym = this->get_sized_symbol SELECT_SIZE_NAME (sym
191 SELECT_SIZE(size));
192
193 off = align_address(off, ssym->value());
194
195 Size_type symsize = ssym->symsize();
196 ssym->init(ssym->name(), poc, off, symsize, ssym->type(),
197 ssym->binding(), ssym->visibility(), ssym->nonvis(),
198 false);
199
200 off += symsize;
201 }
202
203 poc->set_common_size(off);
204
205 this->commons_.clear();
206}
207
208} // End namespace gold.
This page took 0.030386 seconds and 4 git commands to generate.