Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // symtab.cc -- the gold symbol table |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
14bfc3f5 ILT |
23 | #include "gold.h" |
24 | ||
14bfc3f5 ILT |
25 | #include <stdint.h> |
26 | #include <string> | |
27 | #include <utility> | |
28 | ||
29 | #include "object.h" | |
dbe717ef | 30 | #include "dynobj.h" |
75f65a3e | 31 | #include "output.h" |
61ba1cf9 | 32 | #include "target.h" |
645f8123 | 33 | #include "workqueue.h" |
14bfc3f5 ILT |
34 | #include "symtab.h" |
35 | ||
36 | namespace gold | |
37 | { | |
38 | ||
39 | // Class Symbol. | |
40 | ||
ead1e424 ILT |
41 | // Initialize fields in Symbol. This initializes everything except u_ |
42 | // and source_. | |
14bfc3f5 | 43 | |
14bfc3f5 | 44 | void |
ead1e424 ILT |
45 | Symbol::init_fields(const char* name, const char* version, |
46 | elfcpp::STT type, elfcpp::STB binding, | |
47 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
48 | { |
49 | this->name_ = name; | |
50 | this->version_ = version; | |
c06b7b0b ILT |
51 | this->symtab_index_ = 0; |
52 | this->dynsym_index_ = 0; | |
ead1e424 | 53 | this->got_offset_ = 0; |
f4151f89 | 54 | this->plt_offset_ = 0; |
ead1e424 ILT |
55 | this->type_ = type; |
56 | this->binding_ = binding; | |
57 | this->visibility_ = visibility; | |
58 | this->nonvis_ = nonvis; | |
59 | this->is_target_special_ = false; | |
1564db8d ILT |
60 | this->is_def_ = false; |
61 | this->is_forwarder_ = false; | |
aeddab66 | 62 | this->has_alias_ = false; |
c06b7b0b | 63 | this->needs_dynsym_entry_ = false; |
008db82e | 64 | this->in_reg_ = false; |
ead1e424 ILT |
65 | this->in_dyn_ = false; |
66 | this->has_got_offset_ = false; | |
f4151f89 | 67 | this->has_plt_offset_ = false; |
f6ce93d6 | 68 | this->has_warning_ = false; |
46fe1623 | 69 | this->is_copied_from_dynobj_ = false; |
d61c6bd4 | 70 | this->needs_value_in_got_ = false; |
ead1e424 ILT |
71 | } |
72 | ||
73 | // Initialize the fields in the base class Symbol for SYM in OBJECT. | |
74 | ||
75 | template<int size, bool big_endian> | |
76 | void | |
77 | Symbol::init_base(const char* name, const char* version, Object* object, | |
78 | const elfcpp::Sym<size, big_endian>& sym) | |
79 | { | |
80 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
81 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
82 | this->u_.from_object.object = object; | |
83 | // FIXME: Handle SHN_XINDEX. | |
16649710 | 84 | this->u_.from_object.shndx = sym.get_st_shndx(); |
ead1e424 | 85 | this->source_ = FROM_OBJECT; |
008db82e | 86 | this->in_reg_ = !object->is_dynamic(); |
1564db8d | 87 | this->in_dyn_ = object->is_dynamic(); |
14bfc3f5 ILT |
88 | } |
89 | ||
ead1e424 ILT |
90 | // Initialize the fields in the base class Symbol for a symbol defined |
91 | // in an Output_data. | |
92 | ||
93 | void | |
94 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, | |
95 | elfcpp::STB binding, elfcpp::STV visibility, | |
96 | unsigned char nonvis, bool offset_is_from_end) | |
97 | { | |
98 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
99 | this->u_.in_output_data.output_data = od; | |
100 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
101 | this->source_ = IN_OUTPUT_DATA; | |
008db82e | 102 | this->in_reg_ = true; |
ead1e424 ILT |
103 | } |
104 | ||
105 | // Initialize the fields in the base class Symbol for a symbol defined | |
106 | // in an Output_segment. | |
107 | ||
108 | void | |
109 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
110 | elfcpp::STB binding, elfcpp::STV visibility, | |
111 | unsigned char nonvis, Segment_offset_base offset_base) | |
112 | { | |
113 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
114 | this->u_.in_output_segment.output_segment = os; | |
115 | this->u_.in_output_segment.offset_base = offset_base; | |
116 | this->source_ = IN_OUTPUT_SEGMENT; | |
008db82e | 117 | this->in_reg_ = true; |
ead1e424 ILT |
118 | } |
119 | ||
120 | // Initialize the fields in the base class Symbol for a symbol defined | |
121 | // as a constant. | |
122 | ||
123 | void | |
124 | Symbol::init_base(const char* name, elfcpp::STT type, | |
125 | elfcpp::STB binding, elfcpp::STV visibility, | |
126 | unsigned char nonvis) | |
127 | { | |
128 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
129 | this->source_ = CONSTANT; | |
008db82e | 130 | this->in_reg_ = true; |
ead1e424 ILT |
131 | } |
132 | ||
133 | // Initialize the fields in Sized_symbol for SYM in OBJECT. | |
14bfc3f5 ILT |
134 | |
135 | template<int size> | |
136 | template<bool big_endian> | |
137 | void | |
138 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, | |
139 | const elfcpp::Sym<size, big_endian>& sym) | |
140 | { | |
141 | this->init_base(name, version, object, sym); | |
142 | this->value_ = sym.get_st_value(); | |
ead1e424 ILT |
143 | this->symsize_ = sym.get_st_size(); |
144 | } | |
145 | ||
146 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
147 | // Output_data. | |
148 | ||
149 | template<int size> | |
150 | void | |
151 | Sized_symbol<size>::init(const char* name, Output_data* od, | |
152 | Value_type value, Size_type symsize, | |
153 | elfcpp::STT type, elfcpp::STB binding, | |
154 | elfcpp::STV visibility, unsigned char nonvis, | |
155 | bool offset_is_from_end) | |
156 | { | |
157 | this->init_base(name, od, type, binding, visibility, nonvis, | |
158 | offset_is_from_end); | |
159 | this->value_ = value; | |
160 | this->symsize_ = symsize; | |
161 | } | |
162 | ||
163 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
164 | // Output_segment. | |
165 | ||
166 | template<int size> | |
167 | void | |
168 | Sized_symbol<size>::init(const char* name, Output_segment* os, | |
169 | Value_type value, Size_type symsize, | |
170 | elfcpp::STT type, elfcpp::STB binding, | |
171 | elfcpp::STV visibility, unsigned char nonvis, | |
172 | Segment_offset_base offset_base) | |
173 | { | |
174 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); | |
175 | this->value_ = value; | |
176 | this->symsize_ = symsize; | |
177 | } | |
178 | ||
179 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
180 | // constant. | |
181 | ||
182 | template<int size> | |
183 | void | |
184 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, | |
185 | elfcpp::STT type, elfcpp::STB binding, | |
186 | elfcpp::STV visibility, unsigned char nonvis) | |
187 | { | |
188 | this->init_base(name, type, binding, visibility, nonvis); | |
189 | this->value_ = value; | |
190 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
191 | } |
192 | ||
436ca963 ILT |
193 | // Return true if this symbol should be added to the dynamic symbol |
194 | // table. | |
195 | ||
196 | inline bool | |
197 | Symbol::should_add_dynsym_entry() const | |
198 | { | |
199 | // If the symbol is used by a dynamic relocation, we need to add it. | |
200 | if (this->needs_dynsym_entry()) | |
201 | return true; | |
202 | ||
203 | // If exporting all symbols or building a shared library, | |
204 | // and the symbol is defined in a regular object and is | |
205 | // externally visible, we need to add it. | |
206 | if ((parameters->export_dynamic() || parameters->output_is_shared()) | |
207 | && !this->is_from_dynobj() | |
208 | && this->is_externally_visible()) | |
209 | return true; | |
210 | ||
211 | return false; | |
212 | } | |
213 | ||
b3b74ddc ILT |
214 | // Return true if the final value of this symbol is known at link |
215 | // time. | |
216 | ||
217 | bool | |
218 | Symbol::final_value_is_known() const | |
219 | { | |
220 | // If we are not generating an executable, then no final values are | |
221 | // known, since they will change at runtime. | |
222 | if (!parameters->output_is_executable()) | |
223 | return false; | |
224 | ||
225 | // If the symbol is not from an object file, then it is defined, and | |
226 | // known. | |
227 | if (this->source_ != FROM_OBJECT) | |
228 | return true; | |
229 | ||
230 | // If the symbol is from a dynamic object, then the final value is | |
231 | // not known. | |
232 | if (this->object()->is_dynamic()) | |
233 | return false; | |
234 | ||
235 | // If the symbol is not undefined (it is defined or common), then | |
236 | // the final value is known. | |
237 | if (!this->is_undefined()) | |
238 | return true; | |
239 | ||
240 | // If the symbol is undefined, then whether the final value is known | |
241 | // depends on whether we are doing a static link. If we are doing a | |
242 | // dynamic link, then the final value could be filled in at runtime. | |
243 | // This could reasonably be the case for a weak undefined symbol. | |
244 | return parameters->doing_static_link(); | |
245 | } | |
246 | ||
14bfc3f5 ILT |
247 | // Class Symbol_table. |
248 | ||
249 | Symbol_table::Symbol_table() | |
9025d29d | 250 | : saw_undefined_(0), offset_(0), table_(), namepool_(), |
f6ce93d6 | 251 | forwarders_(), commons_(), warnings_() |
14bfc3f5 ILT |
252 | { |
253 | } | |
254 | ||
255 | Symbol_table::~Symbol_table() | |
256 | { | |
257 | } | |
258 | ||
259 | // The hash function. The key is always canonicalized, so we use a | |
260 | // simple combination of the pointers. | |
261 | ||
262 | size_t | |
263 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const | |
264 | { | |
f0641a0b | 265 | return key.first ^ key.second; |
14bfc3f5 ILT |
266 | } |
267 | ||
268 | // The symbol table key equality function. This is only called with | |
269 | // canonicalized name and version strings, so we can use pointer | |
270 | // comparison. | |
271 | ||
272 | bool | |
273 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, | |
274 | const Symbol_table_key& k2) const | |
275 | { | |
276 | return k1.first == k2.first && k1.second == k2.second; | |
277 | } | |
278 | ||
279 | // Make TO a symbol which forwards to FROM. | |
280 | ||
281 | void | |
282 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
283 | { | |
a3ad94ed ILT |
284 | gold_assert(from != to); |
285 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); | |
14bfc3f5 ILT |
286 | this->forwarders_[from] = to; |
287 | from->set_forwarder(); | |
288 | } | |
289 | ||
61ba1cf9 ILT |
290 | // Resolve the forwards from FROM, returning the real symbol. |
291 | ||
14bfc3f5 | 292 | Symbol* |
c06b7b0b | 293 | Symbol_table::resolve_forwards(const Symbol* from) const |
14bfc3f5 | 294 | { |
a3ad94ed | 295 | gold_assert(from->is_forwarder()); |
c06b7b0b | 296 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
14bfc3f5 | 297 | this->forwarders_.find(from); |
a3ad94ed | 298 | gold_assert(p != this->forwarders_.end()); |
14bfc3f5 ILT |
299 | return p->second; |
300 | } | |
301 | ||
61ba1cf9 ILT |
302 | // Look up a symbol by name. |
303 | ||
304 | Symbol* | |
305 | Symbol_table::lookup(const char* name, const char* version) const | |
306 | { | |
f0641a0b ILT |
307 | Stringpool::Key name_key; |
308 | name = this->namepool_.find(name, &name_key); | |
61ba1cf9 ILT |
309 | if (name == NULL) |
310 | return NULL; | |
f0641a0b ILT |
311 | |
312 | Stringpool::Key version_key = 0; | |
61ba1cf9 ILT |
313 | if (version != NULL) |
314 | { | |
f0641a0b | 315 | version = this->namepool_.find(version, &version_key); |
61ba1cf9 ILT |
316 | if (version == NULL) |
317 | return NULL; | |
318 | } | |
319 | ||
f0641a0b | 320 | Symbol_table_key key(name_key, version_key); |
61ba1cf9 ILT |
321 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
322 | if (p == this->table_.end()) | |
323 | return NULL; | |
324 | return p->second; | |
325 | } | |
326 | ||
14bfc3f5 ILT |
327 | // Resolve a Symbol with another Symbol. This is only used in the |
328 | // unusual case where there are references to both an unversioned | |
329 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
330 | // version is the default version. Because this is unusual, we do |
331 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 332 | |
1564db8d | 333 | template<int size, bool big_endian> |
14bfc3f5 | 334 | void |
14b31740 ILT |
335 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
336 | const char* version ACCEPT_SIZE_ENDIAN) | |
14bfc3f5 | 337 | { |
1564db8d ILT |
338 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
339 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
340 | // We don't bother to set the st_name field. | |
341 | esym.put_st_value(from->value()); | |
342 | esym.put_st_size(from->symsize()); | |
343 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 344 | esym.put_st_other(from->visibility(), from->nonvis()); |
16649710 | 345 | esym.put_st_shndx(from->shndx()); |
aeddab66 | 346 | this->resolve(to, esym.sym(), from->object(), version); |
1ebd95fd ILT |
347 | if (from->in_reg()) |
348 | to->set_in_reg(); | |
349 | if (from->in_dyn()) | |
350 | to->set_in_dyn(); | |
14bfc3f5 ILT |
351 | } |
352 | ||
353 | // Add one symbol from OBJECT to the symbol table. NAME is symbol | |
354 | // name and VERSION is the version; both are canonicalized. DEF is | |
355 | // whether this is the default version. | |
356 | ||
357 | // If DEF is true, then this is the definition of a default version of | |
358 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
359 | // of NAME/VERSION should always return the same symbol. This is | |
360 | // obvious for references, but in particular we want to do this for | |
361 | // definitions: overriding NAME/NULL should also override | |
362 | // NAME/VERSION. If we don't do that, it would be very hard to | |
363 | // override functions in a shared library which uses versioning. | |
364 | ||
365 | // We implement this by simply making both entries in the hash table | |
366 | // point to the same Symbol structure. That is easy enough if this is | |
367 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
368 | // that we have seen both already, in which case they will both have | |
369 | // independent entries in the symbol table. We can't simply change | |
370 | // the symbol table entry, because we have pointers to the entries | |
371 | // attached to the object files. So we mark the entry attached to the | |
372 | // object file as a forwarder, and record it in the forwarders_ map. | |
373 | // Note that entries in the hash table will never be marked as | |
374 | // forwarders. | |
375 | ||
376 | template<int size, bool big_endian> | |
aeddab66 | 377 | Sized_symbol<size>* |
f6ce93d6 | 378 | Symbol_table::add_from_object(Object* object, |
14bfc3f5 | 379 | const char *name, |
f0641a0b ILT |
380 | Stringpool::Key name_key, |
381 | const char *version, | |
382 | Stringpool::Key version_key, | |
383 | bool def, | |
14bfc3f5 ILT |
384 | const elfcpp::Sym<size, big_endian>& sym) |
385 | { | |
386 | Symbol* const snull = NULL; | |
387 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
f0641a0b ILT |
388 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
389 | snull)); | |
14bfc3f5 ILT |
390 | |
391 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
392 | std::make_pair(this->table_.end(), false); | |
393 | if (def) | |
394 | { | |
f0641a0b ILT |
395 | const Stringpool::Key vnull_key = 0; |
396 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
397 | vnull_key), | |
14bfc3f5 ILT |
398 | snull)); |
399 | } | |
400 | ||
401 | // ins.first: an iterator, which is a pointer to a pair. | |
402 | // ins.first->first: the key (a pair of name and version). | |
403 | // ins.first->second: the value (Symbol*). | |
404 | // ins.second: true if new entry was inserted, false if not. | |
405 | ||
1564db8d | 406 | Sized_symbol<size>* ret; |
ead1e424 ILT |
407 | bool was_undefined; |
408 | bool was_common; | |
14bfc3f5 ILT |
409 | if (!ins.second) |
410 | { | |
411 | // We already have an entry for NAME/VERSION. | |
593f47df ILT |
412 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second |
413 | SELECT_SIZE(size)); | |
a3ad94ed | 414 | gold_assert(ret != NULL); |
ead1e424 ILT |
415 | |
416 | was_undefined = ret->is_undefined(); | |
417 | was_common = ret->is_common(); | |
418 | ||
aeddab66 | 419 | this->resolve(ret, sym, object, version); |
14bfc3f5 ILT |
420 | |
421 | if (def) | |
422 | { | |
423 | if (insdef.second) | |
424 | { | |
425 | // This is the first time we have seen NAME/NULL. Make | |
426 | // NAME/NULL point to NAME/VERSION. | |
427 | insdef.first->second = ret; | |
428 | } | |
dbe717ef | 429 | else if (insdef.first->second != ret) |
14bfc3f5 ILT |
430 | { |
431 | // This is the unfortunate case where we already have | |
432 | // entries for both NAME/VERSION and NAME/NULL. | |
274e99f9 | 433 | const Sized_symbol<size>* sym2; |
593f47df | 434 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d ILT |
435 | insdef.first->second |
436 | SELECT_SIZE(size)); | |
593f47df | 437 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
14b31740 | 438 | ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian)); |
14bfc3f5 ILT |
439 | this->make_forwarder(insdef.first->second, ret); |
440 | insdef.first->second = ret; | |
441 | } | |
442 | } | |
443 | } | |
444 | else | |
445 | { | |
446 | // This is the first time we have seen NAME/VERSION. | |
a3ad94ed | 447 | gold_assert(ins.first->second == NULL); |
ead1e424 ILT |
448 | |
449 | was_undefined = false; | |
450 | was_common = false; | |
451 | ||
14bfc3f5 ILT |
452 | if (def && !insdef.second) |
453 | { | |
14b31740 ILT |
454 | // We already have an entry for NAME/NULL. If we override |
455 | // it, then change it to NAME/VERSION. | |
593f47df ILT |
456 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
457 | insdef.first->second | |
458 | SELECT_SIZE(size)); | |
aeddab66 | 459 | this->resolve(ret, sym, object, version); |
14bfc3f5 ILT |
460 | ins.first->second = ret; |
461 | } | |
462 | else | |
463 | { | |
f6ce93d6 ILT |
464 | Sized_target<size, big_endian>* target = |
465 | object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
466 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
1564db8d ILT |
467 | if (!target->has_make_symbol()) |
468 | ret = new Sized_symbol<size>(); | |
469 | else | |
14bfc3f5 | 470 | { |
1564db8d ILT |
471 | ret = target->make_symbol(); |
472 | if (ret == NULL) | |
14bfc3f5 ILT |
473 | { |
474 | // This means that we don't want a symbol table | |
475 | // entry after all. | |
476 | if (!def) | |
477 | this->table_.erase(ins.first); | |
478 | else | |
479 | { | |
480 | this->table_.erase(insdef.first); | |
481 | // Inserting insdef invalidated ins. | |
f0641a0b ILT |
482 | this->table_.erase(std::make_pair(name_key, |
483 | version_key)); | |
14bfc3f5 ILT |
484 | } |
485 | return NULL; | |
486 | } | |
487 | } | |
14bfc3f5 | 488 | |
1564db8d ILT |
489 | ret->init(name, version, object, sym); |
490 | ||
14bfc3f5 ILT |
491 | ins.first->second = ret; |
492 | if (def) | |
493 | { | |
494 | // This is the first time we have seen NAME/NULL. Point | |
495 | // it at the new entry for NAME/VERSION. | |
a3ad94ed | 496 | gold_assert(insdef.second); |
14bfc3f5 ILT |
497 | insdef.first->second = ret; |
498 | } | |
499 | } | |
500 | } | |
501 | ||
ead1e424 ILT |
502 | // Record every time we see a new undefined symbol, to speed up |
503 | // archive groups. | |
504 | if (!was_undefined && ret->is_undefined()) | |
505 | ++this->saw_undefined_; | |
506 | ||
507 | // Keep track of common symbols, to speed up common symbol | |
508 | // allocation. | |
509 | if (!was_common && ret->is_common()) | |
510 | this->commons_.push_back(ret); | |
511 | ||
14bfc3f5 ILT |
512 | return ret; |
513 | } | |
514 | ||
f6ce93d6 | 515 | // Add all the symbols in a relocatable object to the hash table. |
14bfc3f5 ILT |
516 | |
517 | template<int size, bool big_endian> | |
518 | void | |
dbe717ef ILT |
519 | Symbol_table::add_from_relobj( |
520 | Sized_relobj<size, big_endian>* relobj, | |
f6ce93d6 | 521 | const unsigned char* syms, |
14bfc3f5 ILT |
522 | size_t count, |
523 | const char* sym_names, | |
524 | size_t sym_name_size, | |
730cdc88 | 525 | typename Sized_relobj<size, big_endian>::Symbols* sympointers) |
14bfc3f5 | 526 | { |
9025d29d ILT |
527 | gold_assert(size == relobj->target()->get_size()); |
528 | gold_assert(size == parameters->get_size()); | |
14bfc3f5 | 529 | |
a783673b ILT |
530 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
531 | ||
f6ce93d6 | 532 | const unsigned char* p = syms; |
a783673b | 533 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
534 | { |
535 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 536 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 537 | |
a783673b | 538 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
539 | if (st_name >= sym_name_size) |
540 | { | |
75f2446e ILT |
541 | relobj->error(_("bad global symbol name offset %u at %zu"), |
542 | st_name, i); | |
543 | continue; | |
14bfc3f5 ILT |
544 | } |
545 | ||
dbe717ef ILT |
546 | const char* name = sym_names + st_name; |
547 | ||
a783673b ILT |
548 | // A symbol defined in a section which we are not including must |
549 | // be treated as an undefined symbol. | |
550 | unsigned char symbuf[sym_size]; | |
551 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
552 | unsigned int st_shndx = psym->get_st_shndx(); | |
553 | if (st_shndx != elfcpp::SHN_UNDEF | |
554 | && st_shndx < elfcpp::SHN_LORESERVE | |
dbe717ef | 555 | && !relobj->is_section_included(st_shndx)) |
a783673b ILT |
556 | { |
557 | memcpy(symbuf, p, sym_size); | |
558 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
559 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
560 | psym = &sym2; | |
561 | } | |
562 | ||
14bfc3f5 ILT |
563 | // In an object file, an '@' in the name separates the symbol |
564 | // name from the version name. If there are two '@' characters, | |
565 | // this is the default version. | |
566 | const char* ver = strchr(name, '@'); | |
567 | ||
aeddab66 | 568 | Sized_symbol<size>* res; |
14bfc3f5 ILT |
569 | if (ver == NULL) |
570 | { | |
f0641a0b | 571 | Stringpool::Key name_key; |
cfd73a4e | 572 | name = this->namepool_.add(name, true, &name_key); |
dbe717ef | 573 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
f0641a0b | 574 | false, *psym); |
14bfc3f5 ILT |
575 | } |
576 | else | |
577 | { | |
f0641a0b | 578 | Stringpool::Key name_key; |
cfd73a4e | 579 | name = this->namepool_.add_prefix(name, ver - name, &name_key); |
f0641a0b | 580 | |
14bfc3f5 ILT |
581 | bool def = false; |
582 | ++ver; | |
583 | if (*ver == '@') | |
584 | { | |
585 | def = true; | |
586 | ++ver; | |
587 | } | |
f0641a0b ILT |
588 | |
589 | Stringpool::Key ver_key; | |
cfd73a4e | 590 | ver = this->namepool_.add(ver, true, &ver_key); |
f0641a0b | 591 | |
dbe717ef | 592 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
f0641a0b | 593 | def, *psym); |
14bfc3f5 ILT |
594 | } |
595 | ||
730cdc88 | 596 | (*sympointers)[i] = res; |
14bfc3f5 ILT |
597 | } |
598 | } | |
599 | ||
dbe717ef ILT |
600 | // Add all the symbols in a dynamic object to the hash table. |
601 | ||
602 | template<int size, bool big_endian> | |
603 | void | |
604 | Symbol_table::add_from_dynobj( | |
605 | Sized_dynobj<size, big_endian>* dynobj, | |
606 | const unsigned char* syms, | |
607 | size_t count, | |
608 | const char* sym_names, | |
609 | size_t sym_name_size, | |
610 | const unsigned char* versym, | |
611 | size_t versym_size, | |
612 | const std::vector<const char*>* version_map) | |
613 | { | |
9025d29d ILT |
614 | gold_assert(size == dynobj->target()->get_size()); |
615 | gold_assert(size == parameters->get_size()); | |
dbe717ef ILT |
616 | |
617 | if (versym != NULL && versym_size / 2 < count) | |
618 | { | |
75f2446e ILT |
619 | dynobj->error(_("too few symbol versions")); |
620 | return; | |
dbe717ef ILT |
621 | } |
622 | ||
623 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
624 | ||
aeddab66 ILT |
625 | // We keep a list of all STT_OBJECT symbols, so that we can resolve |
626 | // weak aliases. This is necessary because if the dynamic object | |
627 | // provides the same variable under two names, one of which is a | |
628 | // weak definition, and the regular object refers to the weak | |
629 | // definition, we have to put both the weak definition and the | |
630 | // strong definition into the dynamic symbol table. Given a weak | |
631 | // definition, the only way that we can find the corresponding | |
632 | // strong definition, if any, is to search the symbol table. | |
633 | std::vector<Sized_symbol<size>*> object_symbols; | |
634 | ||
dbe717ef ILT |
635 | const unsigned char* p = syms; |
636 | const unsigned char* vs = versym; | |
637 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) | |
638 | { | |
639 | elfcpp::Sym<size, big_endian> sym(p); | |
640 | ||
641 | // Ignore symbols with local binding. | |
642 | if (sym.get_st_bind() == elfcpp::STB_LOCAL) | |
643 | continue; | |
644 | ||
645 | unsigned int st_name = sym.get_st_name(); | |
646 | if (st_name >= sym_name_size) | |
647 | { | |
75f2446e ILT |
648 | dynobj->error(_("bad symbol name offset %u at %zu"), |
649 | st_name, i); | |
650 | continue; | |
dbe717ef ILT |
651 | } |
652 | ||
653 | const char* name = sym_names + st_name; | |
654 | ||
aeddab66 ILT |
655 | Sized_symbol<size>* res; |
656 | ||
dbe717ef ILT |
657 | if (versym == NULL) |
658 | { | |
659 | Stringpool::Key name_key; | |
cfd73a4e | 660 | name = this->namepool_.add(name, true, &name_key); |
aeddab66 ILT |
661 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
662 | false, sym); | |
dbe717ef | 663 | } |
aeddab66 ILT |
664 | else |
665 | { | |
666 | // Read the version information. | |
dbe717ef | 667 | |
aeddab66 | 668 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); |
dbe717ef | 669 | |
aeddab66 ILT |
670 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; |
671 | v &= elfcpp::VERSYM_VERSION; | |
dbe717ef | 672 | |
aeddab66 ILT |
673 | // The Sun documentation says that V can be VER_NDX_LOCAL, |
674 | // or VER_NDX_GLOBAL, or a version index. The meaning of | |
675 | // VER_NDX_LOCAL is defined as "Symbol has local scope." | |
676 | // The old GNU linker will happily generate VER_NDX_LOCAL | |
677 | // for an undefined symbol. I don't know what the Sun | |
678 | // linker will generate. | |
dbe717ef | 679 | |
aeddab66 ILT |
680 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
681 | && sym.get_st_shndx() != elfcpp::SHN_UNDEF) | |
682 | { | |
683 | // This symbol should not be visible outside the object. | |
684 | continue; | |
685 | } | |
64707334 | 686 | |
aeddab66 ILT |
687 | // At this point we are definitely going to add this symbol. |
688 | Stringpool::Key name_key; | |
689 | name = this->namepool_.add(name, true, &name_key); | |
dbe717ef | 690 | |
aeddab66 ILT |
691 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
692 | || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) | |
693 | { | |
694 | // This symbol does not have a version. | |
695 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
696 | false, sym); | |
697 | } | |
698 | else | |
699 | { | |
700 | if (v >= version_map->size()) | |
701 | { | |
702 | dynobj->error(_("versym for symbol %zu out of range: %u"), | |
703 | i, v); | |
704 | continue; | |
705 | } | |
dbe717ef | 706 | |
aeddab66 ILT |
707 | const char* version = (*version_map)[v]; |
708 | if (version == NULL) | |
709 | { | |
710 | dynobj->error(_("versym for symbol %zu has no name: %u"), | |
711 | i, v); | |
712 | continue; | |
713 | } | |
dbe717ef | 714 | |
aeddab66 ILT |
715 | Stringpool::Key version_key; |
716 | version = this->namepool_.add(version, true, &version_key); | |
717 | ||
718 | // If this is an absolute symbol, and the version name | |
719 | // and symbol name are the same, then this is the | |
720 | // version definition symbol. These symbols exist to | |
721 | // support using -u to pull in particular versions. We | |
722 | // do not want to record a version for them. | |
723 | if (sym.get_st_shndx() == elfcpp::SHN_ABS | |
724 | && name_key == version_key) | |
725 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
726 | false, sym); | |
727 | else | |
728 | { | |
729 | const bool def = (!hidden | |
730 | && (sym.get_st_shndx() | |
731 | != elfcpp::SHN_UNDEF)); | |
732 | res = this->add_from_object(dynobj, name, name_key, version, | |
733 | version_key, def, sym); | |
734 | } | |
735 | } | |
dbe717ef ILT |
736 | } |
737 | ||
aeddab66 ILT |
738 | if (sym.get_st_shndx() != elfcpp::SHN_UNDEF |
739 | && sym.get_st_type() == elfcpp::STT_OBJECT) | |
740 | object_symbols.push_back(res); | |
741 | } | |
742 | ||
743 | this->record_weak_aliases(&object_symbols); | |
744 | } | |
745 | ||
746 | // This is used to sort weak aliases. We sort them first by section | |
747 | // index, then by offset, then by weak ahead of strong. | |
748 | ||
749 | template<int size> | |
750 | class Weak_alias_sorter | |
751 | { | |
752 | public: | |
753 | bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const; | |
754 | }; | |
755 | ||
756 | template<int size> | |
757 | bool | |
758 | Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1, | |
759 | const Sized_symbol<size>* s2) const | |
760 | { | |
761 | if (s1->shndx() != s2->shndx()) | |
762 | return s1->shndx() < s2->shndx(); | |
763 | if (s1->value() != s2->value()) | |
764 | return s1->value() < s2->value(); | |
765 | if (s1->binding() != s2->binding()) | |
766 | { | |
767 | if (s1->binding() == elfcpp::STB_WEAK) | |
768 | return true; | |
769 | if (s2->binding() == elfcpp::STB_WEAK) | |
770 | return false; | |
771 | } | |
772 | return std::string(s1->name()) < std::string(s2->name()); | |
773 | } | |
dbe717ef | 774 | |
aeddab66 ILT |
775 | // SYMBOLS is a list of object symbols from a dynamic object. Look |
776 | // for any weak aliases, and record them so that if we add the weak | |
777 | // alias to the dynamic symbol table, we also add the corresponding | |
778 | // strong symbol. | |
dbe717ef | 779 | |
aeddab66 ILT |
780 | template<int size> |
781 | void | |
782 | Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols) | |
783 | { | |
784 | // Sort the vector by section index, then by offset, then by weak | |
785 | // ahead of strong. | |
786 | std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>()); | |
787 | ||
788 | // Walk through the vector. For each weak definition, record | |
789 | // aliases. | |
790 | for (typename std::vector<Sized_symbol<size>*>::const_iterator p = | |
791 | symbols->begin(); | |
792 | p != symbols->end(); | |
793 | ++p) | |
794 | { | |
795 | if ((*p)->binding() != elfcpp::STB_WEAK) | |
796 | continue; | |
797 | ||
798 | // Build a circular list of weak aliases. Each symbol points to | |
799 | // the next one in the circular list. | |
800 | ||
801 | Sized_symbol<size>* from_sym = *p; | |
802 | typename std::vector<Sized_symbol<size>*>::const_iterator q; | |
803 | for (q = p + 1; q != symbols->end(); ++q) | |
dbe717ef | 804 | { |
aeddab66 ILT |
805 | if ((*q)->shndx() != from_sym->shndx() |
806 | || (*q)->value() != from_sym->value()) | |
807 | break; | |
808 | ||
809 | this->weak_aliases_[from_sym] = *q; | |
810 | from_sym->set_has_alias(); | |
811 | from_sym = *q; | |
dbe717ef ILT |
812 | } |
813 | ||
aeddab66 ILT |
814 | if (from_sym != *p) |
815 | { | |
816 | this->weak_aliases_[from_sym] = *p; | |
817 | from_sym->set_has_alias(); | |
818 | } | |
dbe717ef | 819 | |
aeddab66 | 820 | p = q - 1; |
dbe717ef ILT |
821 | } |
822 | } | |
823 | ||
ead1e424 ILT |
824 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
825 | // true, then only create the symbol if there is a reference to it. | |
86f2e683 | 826 | // If this does not return NULL, it sets *POLDSYM to the existing |
306d9ef0 | 827 | // symbol if there is one. This canonicalizes *PNAME and *PVERSION. |
ead1e424 ILT |
828 | |
829 | template<int size, bool big_endian> | |
830 | Sized_symbol<size>* | |
306d9ef0 ILT |
831 | Symbol_table::define_special_symbol(const Target* target, const char** pname, |
832 | const char** pversion, bool only_if_ref, | |
86f2e683 | 833 | Sized_symbol<size>** poldsym |
593f47df | 834 | ACCEPT_SIZE_ENDIAN) |
ead1e424 | 835 | { |
ead1e424 ILT |
836 | Symbol* oldsym; |
837 | Sized_symbol<size>* sym; | |
86f2e683 ILT |
838 | bool add_to_table = false; |
839 | typename Symbol_table_type::iterator add_loc = this->table_.end(); | |
ead1e424 ILT |
840 | |
841 | if (only_if_ref) | |
842 | { | |
306d9ef0 | 843 | oldsym = this->lookup(*pname, *pversion); |
f6ce93d6 | 844 | if (oldsym == NULL || !oldsym->is_undefined()) |
ead1e424 | 845 | return NULL; |
306d9ef0 ILT |
846 | |
847 | *pname = oldsym->name(); | |
848 | *pversion = oldsym->version(); | |
ead1e424 ILT |
849 | } |
850 | else | |
851 | { | |
14b31740 | 852 | // Canonicalize NAME and VERSION. |
f0641a0b | 853 | Stringpool::Key name_key; |
cfd73a4e | 854 | *pname = this->namepool_.add(*pname, true, &name_key); |
ead1e424 | 855 | |
14b31740 | 856 | Stringpool::Key version_key = 0; |
306d9ef0 | 857 | if (*pversion != NULL) |
cfd73a4e | 858 | *pversion = this->namepool_.add(*pversion, true, &version_key); |
14b31740 | 859 | |
ead1e424 | 860 | Symbol* const snull = NULL; |
ead1e424 | 861 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
14b31740 ILT |
862 | this->table_.insert(std::make_pair(std::make_pair(name_key, |
863 | version_key), | |
ead1e424 ILT |
864 | snull)); |
865 | ||
866 | if (!ins.second) | |
867 | { | |
14b31740 | 868 | // We already have a symbol table entry for NAME/VERSION. |
ead1e424 | 869 | oldsym = ins.first->second; |
a3ad94ed | 870 | gold_assert(oldsym != NULL); |
ead1e424 ILT |
871 | } |
872 | else | |
873 | { | |
874 | // We haven't seen this symbol before. | |
a3ad94ed | 875 | gold_assert(ins.first->second == NULL); |
86f2e683 ILT |
876 | add_to_table = true; |
877 | add_loc = ins.first; | |
ead1e424 ILT |
878 | oldsym = NULL; |
879 | } | |
880 | } | |
881 | ||
86f2e683 ILT |
882 | if (!target->has_make_symbol()) |
883 | sym = new Sized_symbol<size>(); | |
884 | else | |
ead1e424 | 885 | { |
86f2e683 ILT |
886 | gold_assert(target->get_size() == size); |
887 | gold_assert(target->is_big_endian() ? big_endian : !big_endian); | |
888 | typedef Sized_target<size, big_endian> My_target; | |
889 | const My_target* sized_target = | |
890 | static_cast<const My_target*>(target); | |
891 | sym = sized_target->make_symbol(); | |
892 | if (sym == NULL) | |
893 | return NULL; | |
894 | } | |
ead1e424 | 895 | |
86f2e683 ILT |
896 | if (add_to_table) |
897 | add_loc->second = sym; | |
898 | else | |
899 | gold_assert(oldsym != NULL); | |
ead1e424 | 900 | |
86f2e683 ILT |
901 | *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym |
902 | SELECT_SIZE(size)); | |
ead1e424 ILT |
903 | |
904 | return sym; | |
905 | } | |
906 | ||
907 | // Define a symbol based on an Output_data. | |
908 | ||
14b31740 ILT |
909 | Symbol* |
910 | Symbol_table::define_in_output_data(const Target* target, const char* name, | |
911 | const char* version, Output_data* od, | |
ead1e424 ILT |
912 | uint64_t value, uint64_t symsize, |
913 | elfcpp::STT type, elfcpp::STB binding, | |
914 | elfcpp::STV visibility, | |
915 | unsigned char nonvis, | |
916 | bool offset_is_from_end, | |
917 | bool only_if_ref) | |
918 | { | |
9025d29d | 919 | if (parameters->get_size() == 32) |
86f2e683 ILT |
920 | { |
921 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
922 | return this->do_define_in_output_data<32>(target, name, version, od, | |
923 | value, symsize, type, binding, | |
924 | visibility, nonvis, | |
925 | offset_is_from_end, | |
926 | only_if_ref); | |
927 | #else | |
928 | gold_unreachable(); | |
929 | #endif | |
930 | } | |
9025d29d | 931 | else if (parameters->get_size() == 64) |
86f2e683 ILT |
932 | { |
933 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
934 | return this->do_define_in_output_data<64>(target, name, version, od, | |
935 | value, symsize, type, binding, | |
936 | visibility, nonvis, | |
937 | offset_is_from_end, | |
938 | only_if_ref); | |
939 | #else | |
940 | gold_unreachable(); | |
941 | #endif | |
942 | } | |
ead1e424 | 943 | else |
a3ad94ed | 944 | gold_unreachable(); |
ead1e424 ILT |
945 | } |
946 | ||
947 | // Define a symbol in an Output_data, sized version. | |
948 | ||
949 | template<int size> | |
14b31740 | 950 | Sized_symbol<size>* |
ead1e424 | 951 | Symbol_table::do_define_in_output_data( |
14b31740 | 952 | const Target* target, |
ead1e424 | 953 | const char* name, |
14b31740 | 954 | const char* version, |
ead1e424 ILT |
955 | Output_data* od, |
956 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
957 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
958 | elfcpp::STT type, | |
959 | elfcpp::STB binding, | |
960 | elfcpp::STV visibility, | |
961 | unsigned char nonvis, | |
962 | bool offset_is_from_end, | |
963 | bool only_if_ref) | |
964 | { | |
965 | Sized_symbol<size>* sym; | |
86f2e683 | 966 | Sized_symbol<size>* oldsym; |
ead1e424 | 967 | |
9025d29d | 968 | if (parameters->is_big_endian()) |
193a53d9 ILT |
969 | { |
970 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
971 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( | |
306d9ef0 | 972 | target, &name, &version, only_if_ref, &oldsym |
193a53d9 ILT |
973 | SELECT_SIZE_ENDIAN(size, true)); |
974 | #else | |
975 | gold_unreachable(); | |
976 | #endif | |
977 | } | |
ead1e424 | 978 | else |
193a53d9 ILT |
979 | { |
980 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
981 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( | |
306d9ef0 | 982 | target, &name, &version, only_if_ref, &oldsym |
193a53d9 ILT |
983 | SELECT_SIZE_ENDIAN(size, false)); |
984 | #else | |
985 | gold_unreachable(); | |
986 | #endif | |
987 | } | |
ead1e424 ILT |
988 | |
989 | if (sym == NULL) | |
14b31740 | 990 | return NULL; |
ead1e424 | 991 | |
d4f5281b | 992 | gold_assert(version == NULL || oldsym != NULL); |
ead1e424 ILT |
993 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, |
994 | offset_is_from_end); | |
14b31740 | 995 | |
86f2e683 ILT |
996 | if (oldsym != NULL |
997 | && Symbol_table::should_override_with_special(oldsym)) | |
aeddab66 | 998 | this->override_with_special(oldsym, sym); |
86f2e683 | 999 | |
14b31740 | 1000 | return sym; |
ead1e424 ILT |
1001 | } |
1002 | ||
1003 | // Define a symbol based on an Output_segment. | |
1004 | ||
14b31740 ILT |
1005 | Symbol* |
1006 | Symbol_table::define_in_output_segment(const Target* target, const char* name, | |
1007 | const char* version, Output_segment* os, | |
ead1e424 ILT |
1008 | uint64_t value, uint64_t symsize, |
1009 | elfcpp::STT type, elfcpp::STB binding, | |
1010 | elfcpp::STV visibility, | |
1011 | unsigned char nonvis, | |
1012 | Symbol::Segment_offset_base offset_base, | |
1013 | bool only_if_ref) | |
1014 | { | |
9025d29d | 1015 | if (parameters->get_size() == 32) |
86f2e683 ILT |
1016 | { |
1017 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
1018 | return this->do_define_in_output_segment<32>(target, name, version, os, | |
1019 | value, symsize, type, | |
1020 | binding, visibility, nonvis, | |
1021 | offset_base, only_if_ref); | |
1022 | #else | |
1023 | gold_unreachable(); | |
1024 | #endif | |
1025 | } | |
9025d29d | 1026 | else if (parameters->get_size() == 64) |
86f2e683 ILT |
1027 | { |
1028 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1029 | return this->do_define_in_output_segment<64>(target, name, version, os, | |
1030 | value, symsize, type, | |
1031 | binding, visibility, nonvis, | |
1032 | offset_base, only_if_ref); | |
1033 | #else | |
1034 | gold_unreachable(); | |
1035 | #endif | |
1036 | } | |
ead1e424 | 1037 | else |
a3ad94ed | 1038 | gold_unreachable(); |
ead1e424 ILT |
1039 | } |
1040 | ||
1041 | // Define a symbol in an Output_segment, sized version. | |
1042 | ||
1043 | template<int size> | |
14b31740 | 1044 | Sized_symbol<size>* |
ead1e424 | 1045 | Symbol_table::do_define_in_output_segment( |
14b31740 | 1046 | const Target* target, |
ead1e424 | 1047 | const char* name, |
14b31740 | 1048 | const char* version, |
ead1e424 ILT |
1049 | Output_segment* os, |
1050 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1051 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1052 | elfcpp::STT type, | |
1053 | elfcpp::STB binding, | |
1054 | elfcpp::STV visibility, | |
1055 | unsigned char nonvis, | |
1056 | Symbol::Segment_offset_base offset_base, | |
1057 | bool only_if_ref) | |
1058 | { | |
1059 | Sized_symbol<size>* sym; | |
86f2e683 | 1060 | Sized_symbol<size>* oldsym; |
ead1e424 | 1061 | |
9025d29d ILT |
1062 | if (parameters->is_big_endian()) |
1063 | { | |
1064 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
1065 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( | |
1066 | target, &name, &version, only_if_ref, &oldsym | |
1067 | SELECT_SIZE_ENDIAN(size, true)); | |
1068 | #else | |
1069 | gold_unreachable(); | |
1070 | #endif | |
1071 | } | |
ead1e424 | 1072 | else |
9025d29d ILT |
1073 | { |
1074 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
1075 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( | |
1076 | target, &name, &version, only_if_ref, &oldsym | |
1077 | SELECT_SIZE_ENDIAN(size, false)); | |
1078 | #else | |
1079 | gold_unreachable(); | |
1080 | #endif | |
1081 | } | |
ead1e424 ILT |
1082 | |
1083 | if (sym == NULL) | |
14b31740 | 1084 | return NULL; |
ead1e424 | 1085 | |
d4f5281b | 1086 | gold_assert(version == NULL || oldsym != NULL); |
ead1e424 ILT |
1087 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, |
1088 | offset_base); | |
14b31740 | 1089 | |
86f2e683 ILT |
1090 | if (oldsym != NULL |
1091 | && Symbol_table::should_override_with_special(oldsym)) | |
aeddab66 | 1092 | this->override_with_special(oldsym, sym); |
86f2e683 | 1093 | |
14b31740 | 1094 | return sym; |
ead1e424 ILT |
1095 | } |
1096 | ||
1097 | // Define a special symbol with a constant value. It is a multiple | |
1098 | // definition error if this symbol is already defined. | |
1099 | ||
14b31740 ILT |
1100 | Symbol* |
1101 | Symbol_table::define_as_constant(const Target* target, const char* name, | |
1102 | const char* version, uint64_t value, | |
1103 | uint64_t symsize, elfcpp::STT type, | |
1104 | elfcpp::STB binding, elfcpp::STV visibility, | |
1105 | unsigned char nonvis, bool only_if_ref) | |
ead1e424 | 1106 | { |
9025d29d | 1107 | if (parameters->get_size() == 32) |
86f2e683 ILT |
1108 | { |
1109 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
1110 | return this->do_define_as_constant<32>(target, name, version, value, | |
1111 | symsize, type, binding, | |
1112 | visibility, nonvis, only_if_ref); | |
1113 | #else | |
1114 | gold_unreachable(); | |
1115 | #endif | |
1116 | } | |
9025d29d | 1117 | else if (parameters->get_size() == 64) |
86f2e683 ILT |
1118 | { |
1119 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1120 | return this->do_define_as_constant<64>(target, name, version, value, | |
1121 | symsize, type, binding, | |
1122 | visibility, nonvis, only_if_ref); | |
1123 | #else | |
1124 | gold_unreachable(); | |
1125 | #endif | |
1126 | } | |
ead1e424 | 1127 | else |
a3ad94ed | 1128 | gold_unreachable(); |
ead1e424 ILT |
1129 | } |
1130 | ||
1131 | // Define a symbol as a constant, sized version. | |
1132 | ||
1133 | template<int size> | |
14b31740 | 1134 | Sized_symbol<size>* |
ead1e424 | 1135 | Symbol_table::do_define_as_constant( |
14b31740 | 1136 | const Target* target, |
ead1e424 | 1137 | const char* name, |
14b31740 | 1138 | const char* version, |
ead1e424 ILT |
1139 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1140 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1141 | elfcpp::STT type, | |
1142 | elfcpp::STB binding, | |
1143 | elfcpp::STV visibility, | |
1144 | unsigned char nonvis, | |
1145 | bool only_if_ref) | |
1146 | { | |
1147 | Sized_symbol<size>* sym; | |
86f2e683 | 1148 | Sized_symbol<size>* oldsym; |
ead1e424 | 1149 | |
9025d29d ILT |
1150 | if (parameters->is_big_endian()) |
1151 | { | |
1152 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
1153 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( | |
1154 | target, &name, &version, only_if_ref, &oldsym | |
1155 | SELECT_SIZE_ENDIAN(size, true)); | |
1156 | #else | |
1157 | gold_unreachable(); | |
1158 | #endif | |
1159 | } | |
ead1e424 | 1160 | else |
9025d29d ILT |
1161 | { |
1162 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
1163 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( | |
1164 | target, &name, &version, only_if_ref, &oldsym | |
1165 | SELECT_SIZE_ENDIAN(size, false)); | |
1166 | #else | |
1167 | gold_unreachable(); | |
1168 | #endif | |
1169 | } | |
ead1e424 ILT |
1170 | |
1171 | if (sym == NULL) | |
14b31740 | 1172 | return NULL; |
ead1e424 | 1173 | |
d4f5281b | 1174 | gold_assert(version == NULL || oldsym != NULL); |
ead1e424 | 1175 | sym->init(name, value, symsize, type, binding, visibility, nonvis); |
14b31740 | 1176 | |
86f2e683 ILT |
1177 | if (oldsym != NULL |
1178 | && Symbol_table::should_override_with_special(oldsym)) | |
aeddab66 | 1179 | this->override_with_special(oldsym, sym); |
86f2e683 | 1180 | |
14b31740 | 1181 | return sym; |
ead1e424 ILT |
1182 | } |
1183 | ||
1184 | // Define a set of symbols in output sections. | |
1185 | ||
1186 | void | |
14b31740 ILT |
1187 | Symbol_table::define_symbols(const Layout* layout, const Target* target, |
1188 | int count, const Define_symbol_in_section* p) | |
ead1e424 ILT |
1189 | { |
1190 | for (int i = 0; i < count; ++i, ++p) | |
1191 | { | |
1192 | Output_section* os = layout->find_output_section(p->output_section); | |
1193 | if (os != NULL) | |
14b31740 ILT |
1194 | this->define_in_output_data(target, p->name, NULL, os, p->value, |
1195 | p->size, p->type, p->binding, | |
1196 | p->visibility, p->nonvis, | |
1197 | p->offset_is_from_end, p->only_if_ref); | |
ead1e424 | 1198 | else |
14b31740 | 1199 | this->define_as_constant(target, p->name, NULL, 0, p->size, p->type, |
ead1e424 ILT |
1200 | p->binding, p->visibility, p->nonvis, |
1201 | p->only_if_ref); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | // Define a set of symbols in output segments. | |
1206 | ||
1207 | void | |
14b31740 ILT |
1208 | Symbol_table::define_symbols(const Layout* layout, const Target* target, |
1209 | int count, const Define_symbol_in_segment* p) | |
ead1e424 ILT |
1210 | { |
1211 | for (int i = 0; i < count; ++i, ++p) | |
1212 | { | |
1213 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
1214 | p->segment_flags_set, | |
1215 | p->segment_flags_clear); | |
1216 | if (os != NULL) | |
14b31740 ILT |
1217 | this->define_in_output_segment(target, p->name, NULL, os, p->value, |
1218 | p->size, p->type, p->binding, | |
1219 | p->visibility, p->nonvis, | |
1220 | p->offset_base, p->only_if_ref); | |
ead1e424 | 1221 | else |
14b31740 | 1222 | this->define_as_constant(target, p->name, NULL, 0, p->size, p->type, |
ead1e424 ILT |
1223 | p->binding, p->visibility, p->nonvis, |
1224 | p->only_if_ref); | |
1225 | } | |
1226 | } | |
1227 | ||
46fe1623 ILT |
1228 | // Define CSYM using a COPY reloc. POSD is the Output_data where the |
1229 | // symbol should be defined--typically a .dyn.bss section. VALUE is | |
1230 | // the offset within POSD. | |
1231 | ||
1232 | template<int size> | |
1233 | void | |
1234 | Symbol_table::define_with_copy_reloc(const Target* target, | |
1235 | Sized_symbol<size>* csym, | |
1236 | Output_data* posd, uint64_t value) | |
1237 | { | |
1238 | gold_assert(csym->is_from_dynobj()); | |
1239 | gold_assert(!csym->is_copied_from_dynobj()); | |
1240 | Object* object = csym->object(); | |
1241 | gold_assert(object->is_dynamic()); | |
1242 | Dynobj* dynobj = static_cast<Dynobj*>(object); | |
1243 | ||
1244 | // Our copied variable has to override any variable in a shared | |
1245 | // library. | |
1246 | elfcpp::STB binding = csym->binding(); | |
1247 | if (binding == elfcpp::STB_WEAK) | |
1248 | binding = elfcpp::STB_GLOBAL; | |
1249 | ||
1250 | this->define_in_output_data(target, csym->name(), csym->version(), | |
1251 | posd, value, csym->symsize(), | |
1252 | csym->type(), binding, | |
1253 | csym->visibility(), csym->nonvis(), | |
1254 | false, false); | |
1255 | ||
1256 | csym->set_is_copied_from_dynobj(); | |
1257 | csym->set_needs_dynsym_entry(); | |
1258 | ||
1259 | this->copied_symbol_dynobjs_[csym] = dynobj; | |
1260 | ||
1261 | // We have now defined all aliases, but we have not entered them all | |
1262 | // in the copied_symbol_dynobjs_ map. | |
1263 | if (csym->has_alias()) | |
1264 | { | |
1265 | Symbol* sym = csym; | |
1266 | while (true) | |
1267 | { | |
1268 | sym = this->weak_aliases_[sym]; | |
1269 | if (sym == csym) | |
1270 | break; | |
1271 | gold_assert(sym->output_data() == posd); | |
1272 | ||
1273 | sym->set_is_copied_from_dynobj(); | |
1274 | this->copied_symbol_dynobjs_[sym] = dynobj; | |
1275 | } | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | // SYM is defined using a COPY reloc. Return the dynamic object where | |
1280 | // the original definition was found. | |
1281 | ||
1282 | Dynobj* | |
1283 | Symbol_table::get_copy_source(const Symbol* sym) const | |
1284 | { | |
1285 | gold_assert(sym->is_copied_from_dynobj()); | |
1286 | Copied_symbol_dynobjs::const_iterator p = | |
1287 | this->copied_symbol_dynobjs_.find(sym); | |
1288 | gold_assert(p != this->copied_symbol_dynobjs_.end()); | |
1289 | return p->second; | |
1290 | } | |
1291 | ||
a3ad94ed ILT |
1292 | // Set the dynamic symbol indexes. INDEX is the index of the first |
1293 | // global dynamic symbol. Pointers to the symbols are stored into the | |
1294 | // vector SYMS. The names are added to DYNPOOL. This returns an | |
1295 | // updated dynamic symbol index. | |
1296 | ||
1297 | unsigned int | |
35cdfc9a | 1298 | Symbol_table::set_dynsym_indexes(const Target* target, |
14b31740 | 1299 | unsigned int index, |
a3ad94ed | 1300 | std::vector<Symbol*>* syms, |
14b31740 ILT |
1301 | Stringpool* dynpool, |
1302 | Versions* versions) | |
a3ad94ed ILT |
1303 | { |
1304 | for (Symbol_table_type::iterator p = this->table_.begin(); | |
1305 | p != this->table_.end(); | |
1306 | ++p) | |
1307 | { | |
1308 | Symbol* sym = p->second; | |
16649710 ILT |
1309 | |
1310 | // Note that SYM may already have a dynamic symbol index, since | |
1311 | // some symbols appear more than once in the symbol table, with | |
1312 | // and without a version. | |
1313 | ||
436ca963 | 1314 | if (!sym->should_add_dynsym_entry()) |
16649710 ILT |
1315 | sym->set_dynsym_index(-1U); |
1316 | else if (!sym->has_dynsym_index()) | |
a3ad94ed ILT |
1317 | { |
1318 | sym->set_dynsym_index(index); | |
1319 | ++index; | |
1320 | syms->push_back(sym); | |
cfd73a4e | 1321 | dynpool->add(sym->name(), false, NULL); |
14b31740 ILT |
1322 | |
1323 | // Record any version information. | |
1324 | if (sym->version() != NULL) | |
35cdfc9a | 1325 | versions->record_version(this, dynpool, sym); |
a3ad94ed ILT |
1326 | } |
1327 | } | |
1328 | ||
14b31740 ILT |
1329 | // Finish up the versions. In some cases this may add new dynamic |
1330 | // symbols. | |
1331 | index = versions->finalize(target, this, index, syms); | |
1332 | ||
a3ad94ed ILT |
1333 | return index; |
1334 | } | |
1335 | ||
c06b7b0b ILT |
1336 | // Set the final values for all the symbols. The index of the first |
1337 | // global symbol in the output file is INDEX. Record the file offset | |
75f65a3e | 1338 | // OFF. Add their names to POOL. Return the new file offset. |
54dc6425 | 1339 | |
75f65a3e | 1340 | off_t |
16649710 ILT |
1341 | Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff, |
1342 | size_t dyn_global_index, size_t dyncount, | |
1343 | Stringpool* pool) | |
54dc6425 | 1344 | { |
f6ce93d6 ILT |
1345 | off_t ret; |
1346 | ||
a3ad94ed | 1347 | gold_assert(index != 0); |
c06b7b0b ILT |
1348 | this->first_global_index_ = index; |
1349 | ||
16649710 ILT |
1350 | this->dynamic_offset_ = dynoff; |
1351 | this->first_dynamic_global_index_ = dyn_global_index; | |
1352 | this->dynamic_count_ = dyncount; | |
1353 | ||
9025d29d ILT |
1354 | if (parameters->get_size() == 32) |
1355 | { | |
1356 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE) | |
1357 | ret = this->sized_finalize<32>(index, off, pool); | |
1358 | #else | |
1359 | gold_unreachable(); | |
1360 | #endif | |
1361 | } | |
1362 | else if (parameters->get_size() == 64) | |
1363 | { | |
1364 | #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE) | |
1365 | ret = this->sized_finalize<64>(index, off, pool); | |
1366 | #else | |
1367 | gold_unreachable(); | |
1368 | #endif | |
1369 | } | |
61ba1cf9 | 1370 | else |
a3ad94ed | 1371 | gold_unreachable(); |
f6ce93d6 ILT |
1372 | |
1373 | // Now that we have the final symbol table, we can reliably note | |
1374 | // which symbols should get warnings. | |
1375 | this->warnings_.note_warnings(this); | |
1376 | ||
1377 | return ret; | |
75f65a3e ILT |
1378 | } |
1379 | ||
ead1e424 ILT |
1380 | // Set the final value for all the symbols. This is called after |
1381 | // Layout::finalize, so all the output sections have their final | |
1382 | // address. | |
75f65a3e ILT |
1383 | |
1384 | template<int size> | |
1385 | off_t | |
c06b7b0b | 1386 | Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool) |
75f65a3e | 1387 | { |
ead1e424 | 1388 | off = align_address(off, size >> 3); |
75f65a3e ILT |
1389 | this->offset_ = off; |
1390 | ||
c06b7b0b ILT |
1391 | size_t orig_index = index; |
1392 | ||
75f65a3e | 1393 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
c06b7b0b ILT |
1394 | for (Symbol_table_type::iterator p = this->table_.begin(); |
1395 | p != this->table_.end(); | |
1396 | ++p) | |
54dc6425 | 1397 | { |
75f65a3e | 1398 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
54dc6425 | 1399 | |
75f65a3e | 1400 | // FIXME: Here we need to decide which symbols should go into |
a3ad94ed ILT |
1401 | // the output file, based on --strip. |
1402 | ||
1403 | // The default version of a symbol may appear twice in the | |
1404 | // symbol table. We only need to finalize it once. | |
1405 | if (sym->has_symtab_index()) | |
1406 | continue; | |
75f65a3e | 1407 | |
008db82e ILT |
1408 | if (!sym->in_reg()) |
1409 | { | |
1410 | gold_assert(!sym->has_symtab_index()); | |
1411 | sym->set_symtab_index(-1U); | |
1412 | gold_assert(sym->dynsym_index() == -1U); | |
1413 | continue; | |
1414 | } | |
1415 | ||
ead1e424 | 1416 | typename Sized_symbol<size>::Value_type value; |
75f65a3e | 1417 | |
ead1e424 | 1418 | switch (sym->source()) |
75f65a3e | 1419 | { |
ead1e424 ILT |
1420 | case Symbol::FROM_OBJECT: |
1421 | { | |
16649710 | 1422 | unsigned int shndx = sym->shndx(); |
ead1e424 ILT |
1423 | |
1424 | // FIXME: We need some target specific support here. | |
16649710 ILT |
1425 | if (shndx >= elfcpp::SHN_LORESERVE |
1426 | && shndx != elfcpp::SHN_ABS) | |
ead1e424 | 1427 | { |
75f2446e ILT |
1428 | gold_error(_("%s: unsupported symbol section 0x%x"), |
1429 | sym->name(), shndx); | |
1430 | shndx = elfcpp::SHN_UNDEF; | |
ead1e424 ILT |
1431 | } |
1432 | ||
f6ce93d6 ILT |
1433 | Object* symobj = sym->object(); |
1434 | if (symobj->is_dynamic()) | |
1435 | { | |
1436 | value = 0; | |
16649710 | 1437 | shndx = elfcpp::SHN_UNDEF; |
f6ce93d6 | 1438 | } |
16649710 | 1439 | else if (shndx == elfcpp::SHN_UNDEF) |
ead1e424 | 1440 | value = 0; |
16649710 | 1441 | else if (shndx == elfcpp::SHN_ABS) |
ead1e424 ILT |
1442 | value = sym->value(); |
1443 | else | |
1444 | { | |
f6ce93d6 | 1445 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ead1e424 | 1446 | off_t secoff; |
16649710 | 1447 | Output_section* os = relobj->output_section(shndx, &secoff); |
ead1e424 ILT |
1448 | |
1449 | if (os == NULL) | |
1450 | { | |
c06b7b0b | 1451 | sym->set_symtab_index(-1U); |
16649710 | 1452 | gold_assert(sym->dynsym_index() == -1U); |
ead1e424 ILT |
1453 | continue; |
1454 | } | |
1455 | ||
1456 | value = sym->value() + os->address() + secoff; | |
1457 | } | |
1458 | } | |
1459 | break; | |
1460 | ||
1461 | case Symbol::IN_OUTPUT_DATA: | |
1462 | { | |
1463 | Output_data* od = sym->output_data(); | |
1464 | value = sym->value() + od->address(); | |
1465 | if (sym->offset_is_from_end()) | |
1466 | value += od->data_size(); | |
1467 | } | |
1468 | break; | |
1469 | ||
1470 | case Symbol::IN_OUTPUT_SEGMENT: | |
1471 | { | |
1472 | Output_segment* os = sym->output_segment(); | |
1473 | value = sym->value() + os->vaddr(); | |
1474 | switch (sym->offset_base()) | |
1475 | { | |
1476 | case Symbol::SEGMENT_START: | |
1477 | break; | |
1478 | case Symbol::SEGMENT_END: | |
1479 | value += os->memsz(); | |
1480 | break; | |
1481 | case Symbol::SEGMENT_BSS: | |
1482 | value += os->filesz(); | |
1483 | break; | |
1484 | default: | |
a3ad94ed | 1485 | gold_unreachable(); |
ead1e424 ILT |
1486 | } |
1487 | } | |
1488 | break; | |
1489 | ||
1490 | case Symbol::CONSTANT: | |
1491 | value = sym->value(); | |
1492 | break; | |
1493 | ||
1494 | default: | |
a3ad94ed | 1495 | gold_unreachable(); |
54dc6425 | 1496 | } |
ead1e424 ILT |
1497 | |
1498 | sym->set_value(value); | |
9e2dcb77 ILT |
1499 | |
1500 | if (parameters->strip_all()) | |
1501 | sym->set_symtab_index(-1U); | |
1502 | else | |
1503 | { | |
1504 | sym->set_symtab_index(index); | |
cfd73a4e | 1505 | pool->add(sym->name(), false, NULL); |
9e2dcb77 ILT |
1506 | ++index; |
1507 | off += sym_size; | |
1508 | } | |
54dc6425 | 1509 | } |
75f65a3e | 1510 | |
c06b7b0b | 1511 | this->output_count_ = index - orig_index; |
61ba1cf9 | 1512 | |
75f65a3e | 1513 | return off; |
54dc6425 ILT |
1514 | } |
1515 | ||
61ba1cf9 ILT |
1516 | // Write out the global symbols. |
1517 | ||
1518 | void | |
1519 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, | |
16649710 | 1520 | const Stringpool* dynpool, Output_file* of) const |
61ba1cf9 | 1521 | { |
9025d29d | 1522 | if (parameters->get_size() == 32) |
61ba1cf9 | 1523 | { |
9025d29d ILT |
1524 | if (parameters->is_big_endian()) |
1525 | { | |
1526 | #ifdef HAVE_TARGET_32_BIG | |
1527 | this->sized_write_globals<32, true>(target, sympool, dynpool, of); | |
1528 | #else | |
1529 | gold_unreachable(); | |
1530 | #endif | |
1531 | } | |
61ba1cf9 | 1532 | else |
9025d29d ILT |
1533 | { |
1534 | #ifdef HAVE_TARGET_32_LITTLE | |
1535 | this->sized_write_globals<32, false>(target, sympool, dynpool, of); | |
1536 | #else | |
1537 | gold_unreachable(); | |
1538 | #endif | |
1539 | } | |
61ba1cf9 | 1540 | } |
9025d29d | 1541 | else if (parameters->get_size() == 64) |
61ba1cf9 | 1542 | { |
9025d29d ILT |
1543 | if (parameters->is_big_endian()) |
1544 | { | |
1545 | #ifdef HAVE_TARGET_64_BIG | |
1546 | this->sized_write_globals<64, true>(target, sympool, dynpool, of); | |
1547 | #else | |
1548 | gold_unreachable(); | |
1549 | #endif | |
1550 | } | |
61ba1cf9 | 1551 | else |
9025d29d ILT |
1552 | { |
1553 | #ifdef HAVE_TARGET_64_LITTLE | |
1554 | this->sized_write_globals<64, false>(target, sympool, dynpool, of); | |
1555 | #else | |
1556 | gold_unreachable(); | |
1557 | #endif | |
1558 | } | |
61ba1cf9 ILT |
1559 | } |
1560 | else | |
a3ad94ed | 1561 | gold_unreachable(); |
61ba1cf9 ILT |
1562 | } |
1563 | ||
1564 | // Write out the global symbols. | |
1565 | ||
1566 | template<int size, bool big_endian> | |
1567 | void | |
ab5c9e90 | 1568 | Symbol_table::sized_write_globals(const Target* target, |
61ba1cf9 | 1569 | const Stringpool* sympool, |
16649710 | 1570 | const Stringpool* dynpool, |
61ba1cf9 ILT |
1571 | Output_file* of) const |
1572 | { | |
1573 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
c06b7b0b ILT |
1574 | unsigned int index = this->first_global_index_; |
1575 | const off_t oview_size = this->output_count_ * sym_size; | |
16649710 ILT |
1576 | unsigned char* const psyms = of->get_output_view(this->offset_, oview_size); |
1577 | ||
1578 | unsigned int dynamic_count = this->dynamic_count_; | |
1579 | off_t dynamic_size = dynamic_count * sym_size; | |
1580 | unsigned int first_dynamic_global_index = this->first_dynamic_global_index_; | |
1581 | unsigned char* dynamic_view; | |
1582 | if (this->dynamic_offset_ == 0) | |
1583 | dynamic_view = NULL; | |
1584 | else | |
1585 | dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); | |
c06b7b0b | 1586 | |
61ba1cf9 ILT |
1587 | unsigned char* ps = psyms; |
1588 | for (Symbol_table_type::const_iterator p = this->table_.begin(); | |
1589 | p != this->table_.end(); | |
1590 | ++p) | |
1591 | { | |
1592 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
1593 | ||
a3ad94ed | 1594 | unsigned int sym_index = sym->symtab_index(); |
16649710 ILT |
1595 | unsigned int dynsym_index; |
1596 | if (dynamic_view == NULL) | |
1597 | dynsym_index = -1U; | |
1598 | else | |
1599 | dynsym_index = sym->dynsym_index(); | |
1600 | ||
1601 | if (sym_index == -1U && dynsym_index == -1U) | |
a3ad94ed ILT |
1602 | { |
1603 | // This symbol is not included in the output file. | |
1604 | continue; | |
1605 | } | |
16649710 ILT |
1606 | |
1607 | if (sym_index == index) | |
1608 | ++index; | |
1609 | else if (sym_index != -1U) | |
a3ad94ed ILT |
1610 | { |
1611 | // We have already seen this symbol, because it has a | |
1612 | // default version. | |
1613 | gold_assert(sym_index < index); | |
16649710 ILT |
1614 | if (dynsym_index == -1U) |
1615 | continue; | |
1616 | sym_index = -1U; | |
a3ad94ed | 1617 | } |
c06b7b0b | 1618 | |
ead1e424 | 1619 | unsigned int shndx; |
ab5c9e90 | 1620 | typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value(); |
ead1e424 ILT |
1621 | switch (sym->source()) |
1622 | { | |
1623 | case Symbol::FROM_OBJECT: | |
1624 | { | |
16649710 | 1625 | unsigned int in_shndx = sym->shndx(); |
ead1e424 ILT |
1626 | |
1627 | // FIXME: We need some target specific support here. | |
16649710 ILT |
1628 | if (in_shndx >= elfcpp::SHN_LORESERVE |
1629 | && in_shndx != elfcpp::SHN_ABS) | |
ead1e424 | 1630 | { |
75f2446e ILT |
1631 | gold_error(_("%s: unsupported symbol section 0x%x"), |
1632 | sym->name(), in_shndx); | |
1633 | shndx = in_shndx; | |
f6ce93d6 | 1634 | } |
ead1e424 ILT |
1635 | else |
1636 | { | |
75f2446e ILT |
1637 | Object* symobj = sym->object(); |
1638 | if (symobj->is_dynamic()) | |
1639 | { | |
1640 | if (sym->needs_dynsym_value()) | |
1641 | value = target->dynsym_value(sym); | |
1642 | shndx = elfcpp::SHN_UNDEF; | |
1643 | } | |
1644 | else if (in_shndx == elfcpp::SHN_UNDEF | |
1645 | || in_shndx == elfcpp::SHN_ABS) | |
1646 | shndx = in_shndx; | |
1647 | else | |
1648 | { | |
1649 | Relobj* relobj = static_cast<Relobj*>(symobj); | |
1650 | off_t secoff; | |
1651 | Output_section* os = relobj->output_section(in_shndx, | |
1652 | &secoff); | |
1653 | gold_assert(os != NULL); | |
1654 | shndx = os->out_shndx(); | |
1655 | } | |
ead1e424 ILT |
1656 | } |
1657 | } | |
1658 | break; | |
1659 | ||
1660 | case Symbol::IN_OUTPUT_DATA: | |
1661 | shndx = sym->output_data()->out_shndx(); | |
1662 | break; | |
1663 | ||
1664 | case Symbol::IN_OUTPUT_SEGMENT: | |
1665 | shndx = elfcpp::SHN_ABS; | |
1666 | break; | |
1667 | ||
1668 | case Symbol::CONSTANT: | |
1669 | shndx = elfcpp::SHN_ABS; | |
1670 | break; | |
1671 | ||
1672 | default: | |
a3ad94ed | 1673 | gold_unreachable(); |
ead1e424 | 1674 | } |
61ba1cf9 | 1675 | |
16649710 ILT |
1676 | if (sym_index != -1U) |
1677 | { | |
6a469986 | 1678 | this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
ab5c9e90 | 1679 | sym, sym->value(), shndx, sympool, ps |
6a469986 | 1680 | SELECT_SIZE_ENDIAN(size, big_endian)); |
16649710 ILT |
1681 | ps += sym_size; |
1682 | } | |
61ba1cf9 | 1683 | |
16649710 ILT |
1684 | if (dynsym_index != -1U) |
1685 | { | |
1686 | dynsym_index -= first_dynamic_global_index; | |
1687 | gold_assert(dynsym_index < dynamic_count); | |
1688 | unsigned char* pd = dynamic_view + (dynsym_index * sym_size); | |
6a469986 | 1689 | this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
ab5c9e90 | 1690 | sym, value, shndx, dynpool, pd |
6a469986 | 1691 | SELECT_SIZE_ENDIAN(size, big_endian)); |
16649710 | 1692 | } |
61ba1cf9 ILT |
1693 | } |
1694 | ||
a3ad94ed | 1695 | gold_assert(ps - psyms == oview_size); |
c06b7b0b ILT |
1696 | |
1697 | of->write_output_view(this->offset_, oview_size, psyms); | |
16649710 ILT |
1698 | if (dynamic_view != NULL) |
1699 | of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); | |
1700 | } | |
1701 | ||
1702 | // Write out the symbol SYM, in section SHNDX, to P. POOL is the | |
1703 | // strtab holding the name. | |
1704 | ||
1705 | template<int size, bool big_endian> | |
1706 | void | |
ab5c9e90 ILT |
1707 | Symbol_table::sized_write_symbol( |
1708 | Sized_symbol<size>* sym, | |
1709 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1710 | unsigned int shndx, | |
1711 | const Stringpool* pool, | |
1712 | unsigned char* p | |
1713 | ACCEPT_SIZE_ENDIAN) const | |
16649710 ILT |
1714 | { |
1715 | elfcpp::Sym_write<size, big_endian> osym(p); | |
1716 | osym.put_st_name(pool->get_offset(sym->name())); | |
ab5c9e90 | 1717 | osym.put_st_value(value); |
16649710 ILT |
1718 | osym.put_st_size(sym->symsize()); |
1719 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
1720 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); | |
1721 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
1722 | } |
1723 | ||
a3ad94ed ILT |
1724 | // Write out a section symbol. Return the update offset. |
1725 | ||
1726 | void | |
9025d29d | 1727 | Symbol_table::write_section_symbol(const Output_section *os, |
a3ad94ed ILT |
1728 | Output_file* of, |
1729 | off_t offset) const | |
1730 | { | |
9025d29d | 1731 | if (parameters->get_size() == 32) |
a3ad94ed | 1732 | { |
9025d29d ILT |
1733 | if (parameters->is_big_endian()) |
1734 | { | |
1735 | #ifdef HAVE_TARGET_32_BIG | |
1736 | this->sized_write_section_symbol<32, true>(os, of, offset); | |
1737 | #else | |
1738 | gold_unreachable(); | |
1739 | #endif | |
1740 | } | |
a3ad94ed | 1741 | else |
9025d29d ILT |
1742 | { |
1743 | #ifdef HAVE_TARGET_32_LITTLE | |
1744 | this->sized_write_section_symbol<32, false>(os, of, offset); | |
1745 | #else | |
1746 | gold_unreachable(); | |
1747 | #endif | |
1748 | } | |
a3ad94ed | 1749 | } |
9025d29d | 1750 | else if (parameters->get_size() == 64) |
a3ad94ed | 1751 | { |
9025d29d ILT |
1752 | if (parameters->is_big_endian()) |
1753 | { | |
1754 | #ifdef HAVE_TARGET_64_BIG | |
1755 | this->sized_write_section_symbol<64, true>(os, of, offset); | |
1756 | #else | |
1757 | gold_unreachable(); | |
1758 | #endif | |
1759 | } | |
a3ad94ed | 1760 | else |
9025d29d ILT |
1761 | { |
1762 | #ifdef HAVE_TARGET_64_LITTLE | |
1763 | this->sized_write_section_symbol<64, false>(os, of, offset); | |
1764 | #else | |
1765 | gold_unreachable(); | |
1766 | #endif | |
1767 | } | |
a3ad94ed ILT |
1768 | } |
1769 | else | |
1770 | gold_unreachable(); | |
1771 | } | |
1772 | ||
1773 | // Write out a section symbol, specialized for size and endianness. | |
1774 | ||
1775 | template<int size, bool big_endian> | |
1776 | void | |
1777 | Symbol_table::sized_write_section_symbol(const Output_section* os, | |
1778 | Output_file* of, | |
1779 | off_t offset) const | |
1780 | { | |
1781 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
1782 | ||
1783 | unsigned char* pov = of->get_output_view(offset, sym_size); | |
1784 | ||
1785 | elfcpp::Sym_write<size, big_endian> osym(pov); | |
1786 | osym.put_st_name(0); | |
1787 | osym.put_st_value(os->address()); | |
1788 | osym.put_st_size(0); | |
1789 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, | |
1790 | elfcpp::STT_SECTION)); | |
1791 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); | |
1792 | osym.put_st_shndx(os->out_shndx()); | |
1793 | ||
1794 | of->write_output_view(offset, sym_size, pov); | |
1795 | } | |
1796 | ||
f6ce93d6 ILT |
1797 | // Warnings functions. |
1798 | ||
1799 | // Add a new warning. | |
1800 | ||
1801 | void | |
1802 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
1803 | unsigned int shndx) | |
1804 | { | |
1805 | name = symtab->canonicalize_name(name); | |
1806 | this->warnings_[name].set(obj, shndx); | |
1807 | } | |
1808 | ||
1809 | // Look through the warnings and mark the symbols for which we should | |
1810 | // warn. This is called during Layout::finalize when we know the | |
1811 | // sources for all the symbols. | |
1812 | ||
1813 | void | |
1814 | Warnings::note_warnings(Symbol_table* symtab) | |
1815 | { | |
1816 | for (Warning_table::iterator p = this->warnings_.begin(); | |
1817 | p != this->warnings_.end(); | |
1818 | ++p) | |
1819 | { | |
1820 | Symbol* sym = symtab->lookup(p->first, NULL); | |
1821 | if (sym != NULL | |
1822 | && sym->source() == Symbol::FROM_OBJECT | |
1823 | && sym->object() == p->second.object) | |
1824 | { | |
1825 | sym->set_has_warning(); | |
1826 | ||
1827 | // Read the section contents to get the warning text. It | |
1828 | // would be nicer if we only did this if we have to actually | |
1829 | // issue a warning. Unfortunately, warnings are issued as | |
1830 | // we relocate sections. That means that we can not lock | |
1831 | // the object then, as we might try to issue the same | |
1832 | // warning multiple times simultaneously. | |
645f8123 ILT |
1833 | { |
1834 | Task_locker_obj<Object> tl(*p->second.object); | |
1835 | const unsigned char* c; | |
1836 | off_t len; | |
9eb9fa57 ILT |
1837 | c = p->second.object->section_contents(p->second.shndx, &len, |
1838 | false); | |
645f8123 ILT |
1839 | p->second.set_text(reinterpret_cast<const char*>(c), len); |
1840 | } | |
f6ce93d6 ILT |
1841 | } |
1842 | } | |
1843 | } | |
1844 | ||
1845 | // Issue a warning. This is called when we see a relocation against a | |
1846 | // symbol for which has a warning. | |
1847 | ||
75f2446e | 1848 | template<int size, bool big_endian> |
f6ce93d6 | 1849 | void |
75f2446e ILT |
1850 | Warnings::issue_warning(const Symbol* sym, |
1851 | const Relocate_info<size, big_endian>* relinfo, | |
1852 | size_t relnum, off_t reloffset) const | |
f6ce93d6 | 1853 | { |
a3ad94ed | 1854 | gold_assert(sym->has_warning()); |
f6ce93d6 | 1855 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
a3ad94ed | 1856 | gold_assert(p != this->warnings_.end()); |
75f2446e ILT |
1857 | gold_warning_at_location(relinfo, relnum, reloffset, |
1858 | "%s", p->second.text.c_str()); | |
f6ce93d6 ILT |
1859 | } |
1860 | ||
14bfc3f5 ILT |
1861 | // Instantiate the templates we need. We could use the configure |
1862 | // script to restrict this to only the ones needed for implemented | |
1863 | // targets. | |
1864 | ||
193a53d9 | 1865 | #ifdef HAVE_TARGET_32_LITTLE |
14bfc3f5 ILT |
1866 | template |
1867 | void | |
193a53d9 ILT |
1868 | Symbol_table::add_from_relobj<32, false>( |
1869 | Sized_relobj<32, false>* relobj, | |
f6ce93d6 | 1870 | const unsigned char* syms, |
14bfc3f5 ILT |
1871 | size_t count, |
1872 | const char* sym_names, | |
1873 | size_t sym_name_size, | |
730cdc88 | 1874 | Sized_relobj<32, true>::Symbols* sympointers); |
193a53d9 | 1875 | #endif |
14bfc3f5 | 1876 | |
193a53d9 | 1877 | #ifdef HAVE_TARGET_32_BIG |
14bfc3f5 ILT |
1878 | template |
1879 | void | |
193a53d9 ILT |
1880 | Symbol_table::add_from_relobj<32, true>( |
1881 | Sized_relobj<32, true>* relobj, | |
f6ce93d6 | 1882 | const unsigned char* syms, |
14bfc3f5 ILT |
1883 | size_t count, |
1884 | const char* sym_names, | |
1885 | size_t sym_name_size, | |
730cdc88 | 1886 | Sized_relobj<32, false>::Symbols* sympointers); |
193a53d9 | 1887 | #endif |
14bfc3f5 | 1888 | |
193a53d9 | 1889 | #ifdef HAVE_TARGET_64_LITTLE |
14bfc3f5 ILT |
1890 | template |
1891 | void | |
193a53d9 ILT |
1892 | Symbol_table::add_from_relobj<64, false>( |
1893 | Sized_relobj<64, false>* relobj, | |
f6ce93d6 | 1894 | const unsigned char* syms, |
14bfc3f5 ILT |
1895 | size_t count, |
1896 | const char* sym_names, | |
1897 | size_t sym_name_size, | |
730cdc88 | 1898 | Sized_relobj<64, true>::Symbols* sympointers); |
193a53d9 | 1899 | #endif |
14bfc3f5 | 1900 | |
193a53d9 | 1901 | #ifdef HAVE_TARGET_64_BIG |
14bfc3f5 ILT |
1902 | template |
1903 | void | |
193a53d9 ILT |
1904 | Symbol_table::add_from_relobj<64, true>( |
1905 | Sized_relobj<64, true>* relobj, | |
f6ce93d6 | 1906 | const unsigned char* syms, |
14bfc3f5 ILT |
1907 | size_t count, |
1908 | const char* sym_names, | |
1909 | size_t sym_name_size, | |
730cdc88 | 1910 | Sized_relobj<64, false>::Symbols* sympointers); |
193a53d9 | 1911 | #endif |
14bfc3f5 | 1912 | |
193a53d9 | 1913 | #ifdef HAVE_TARGET_32_LITTLE |
dbe717ef ILT |
1914 | template |
1915 | void | |
193a53d9 ILT |
1916 | Symbol_table::add_from_dynobj<32, false>( |
1917 | Sized_dynobj<32, false>* dynobj, | |
dbe717ef ILT |
1918 | const unsigned char* syms, |
1919 | size_t count, | |
1920 | const char* sym_names, | |
1921 | size_t sym_name_size, | |
1922 | const unsigned char* versym, | |
1923 | size_t versym_size, | |
1924 | const std::vector<const char*>* version_map); | |
193a53d9 | 1925 | #endif |
dbe717ef | 1926 | |
193a53d9 | 1927 | #ifdef HAVE_TARGET_32_BIG |
dbe717ef ILT |
1928 | template |
1929 | void | |
193a53d9 ILT |
1930 | Symbol_table::add_from_dynobj<32, true>( |
1931 | Sized_dynobj<32, true>* dynobj, | |
dbe717ef ILT |
1932 | const unsigned char* syms, |
1933 | size_t count, | |
1934 | const char* sym_names, | |
1935 | size_t sym_name_size, | |
1936 | const unsigned char* versym, | |
1937 | size_t versym_size, | |
1938 | const std::vector<const char*>* version_map); | |
193a53d9 | 1939 | #endif |
dbe717ef | 1940 | |
193a53d9 | 1941 | #ifdef HAVE_TARGET_64_LITTLE |
dbe717ef ILT |
1942 | template |
1943 | void | |
193a53d9 ILT |
1944 | Symbol_table::add_from_dynobj<64, false>( |
1945 | Sized_dynobj<64, false>* dynobj, | |
dbe717ef ILT |
1946 | const unsigned char* syms, |
1947 | size_t count, | |
1948 | const char* sym_names, | |
1949 | size_t sym_name_size, | |
1950 | const unsigned char* versym, | |
1951 | size_t versym_size, | |
1952 | const std::vector<const char*>* version_map); | |
193a53d9 | 1953 | #endif |
dbe717ef | 1954 | |
193a53d9 | 1955 | #ifdef HAVE_TARGET_64_BIG |
dbe717ef ILT |
1956 | template |
1957 | void | |
193a53d9 ILT |
1958 | Symbol_table::add_from_dynobj<64, true>( |
1959 | Sized_dynobj<64, true>* dynobj, | |
dbe717ef ILT |
1960 | const unsigned char* syms, |
1961 | size_t count, | |
1962 | const char* sym_names, | |
1963 | size_t sym_name_size, | |
1964 | const unsigned char* versym, | |
1965 | size_t versym_size, | |
1966 | const std::vector<const char*>* version_map); | |
193a53d9 | 1967 | #endif |
dbe717ef | 1968 | |
46fe1623 ILT |
1969 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
1970 | template | |
1971 | void | |
1972 | Symbol_table::define_with_copy_reloc<32>(const Target* target, | |
1973 | Sized_symbol<32>* sym, | |
1974 | Output_data* posd, uint64_t value); | |
1975 | #endif | |
1976 | ||
1977 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1978 | template | |
1979 | void | |
1980 | Symbol_table::define_with_copy_reloc<64>(const Target* target, | |
1981 | Sized_symbol<64>* sym, | |
1982 | Output_data* posd, uint64_t value); | |
1983 | #endif | |
1984 | ||
75f2446e ILT |
1985 | #ifdef HAVE_TARGET_32_LITTLE |
1986 | template | |
1987 | void | |
1988 | Warnings::issue_warning<32, false>(const Symbol* sym, | |
1989 | const Relocate_info<32, false>* relinfo, | |
1990 | size_t relnum, off_t reloffset) const; | |
1991 | #endif | |
1992 | ||
1993 | #ifdef HAVE_TARGET_32_BIG | |
1994 | template | |
1995 | void | |
1996 | Warnings::issue_warning<32, true>(const Symbol* sym, | |
1997 | const Relocate_info<32, true>* relinfo, | |
1998 | size_t relnum, off_t reloffset) const; | |
1999 | #endif | |
2000 | ||
2001 | #ifdef HAVE_TARGET_64_LITTLE | |
2002 | template | |
2003 | void | |
2004 | Warnings::issue_warning<64, false>(const Symbol* sym, | |
2005 | const Relocate_info<64, false>* relinfo, | |
2006 | size_t relnum, off_t reloffset) const; | |
2007 | #endif | |
2008 | ||
2009 | #ifdef HAVE_TARGET_64_BIG | |
2010 | template | |
2011 | void | |
2012 | Warnings::issue_warning<64, true>(const Symbol* sym, | |
2013 | const Relocate_info<64, true>* relinfo, | |
2014 | size_t relnum, off_t reloffset) const; | |
2015 | #endif | |
2016 | ||
14bfc3f5 | 2017 | } // End namespace gold. |