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