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