Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // symtab.cc -- the gold symbol table |
2 | ||
3 | #include "gold.h" | |
4 | ||
14bfc3f5 ILT |
5 | #include <stdint.h> |
6 | #include <string> | |
7 | #include <utility> | |
8 | ||
9 | #include "object.h" | |
dbe717ef | 10 | #include "dynobj.h" |
75f65a3e | 11 | #include "output.h" |
61ba1cf9 | 12 | #include "target.h" |
645f8123 | 13 | #include "workqueue.h" |
14bfc3f5 ILT |
14 | #include "symtab.h" |
15 | ||
16 | namespace gold | |
17 | { | |
18 | ||
19 | // Class Symbol. | |
20 | ||
ead1e424 ILT |
21 | // Initialize fields in Symbol. This initializes everything except u_ |
22 | // and source_. | |
14bfc3f5 | 23 | |
14bfc3f5 | 24 | void |
ead1e424 ILT |
25 | Symbol::init_fields(const char* name, const char* version, |
26 | elfcpp::STT type, elfcpp::STB binding, | |
27 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
28 | { |
29 | this->name_ = name; | |
30 | this->version_ = version; | |
c06b7b0b ILT |
31 | this->symtab_index_ = 0; |
32 | this->dynsym_index_ = 0; | |
ead1e424 ILT |
33 | this->got_offset_ = 0; |
34 | this->type_ = type; | |
35 | this->binding_ = binding; | |
36 | this->visibility_ = visibility; | |
37 | this->nonvis_ = nonvis; | |
38 | this->is_target_special_ = false; | |
1564db8d ILT |
39 | this->is_def_ = false; |
40 | this->is_forwarder_ = false; | |
c06b7b0b | 41 | this->needs_dynsym_entry_ = false; |
ead1e424 ILT |
42 | this->in_dyn_ = false; |
43 | this->has_got_offset_ = false; | |
f6ce93d6 | 44 | this->has_warning_ = false; |
ead1e424 ILT |
45 | } |
46 | ||
47 | // Initialize the fields in the base class Symbol for SYM in OBJECT. | |
48 | ||
49 | template<int size, bool big_endian> | |
50 | void | |
51 | Symbol::init_base(const char* name, const char* version, Object* object, | |
52 | const elfcpp::Sym<size, big_endian>& sym) | |
53 | { | |
54 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
55 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
56 | this->u_.from_object.object = object; | |
57 | // FIXME: Handle SHN_XINDEX. | |
58 | this->u_.from_object.shnum = sym.get_st_shndx(); | |
59 | this->source_ = FROM_OBJECT; | |
1564db8d | 60 | this->in_dyn_ = object->is_dynamic(); |
14bfc3f5 ILT |
61 | } |
62 | ||
ead1e424 ILT |
63 | // Initialize the fields in the base class Symbol for a symbol defined |
64 | // in an Output_data. | |
65 | ||
66 | void | |
67 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, | |
68 | elfcpp::STB binding, elfcpp::STV visibility, | |
69 | unsigned char nonvis, bool offset_is_from_end) | |
70 | { | |
71 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
72 | this->u_.in_output_data.output_data = od; | |
73 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
74 | this->source_ = IN_OUTPUT_DATA; | |
75 | } | |
76 | ||
77 | // Initialize the fields in the base class Symbol for a symbol defined | |
78 | // in an Output_segment. | |
79 | ||
80 | void | |
81 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
82 | elfcpp::STB binding, elfcpp::STV visibility, | |
83 | unsigned char nonvis, Segment_offset_base offset_base) | |
84 | { | |
85 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
86 | this->u_.in_output_segment.output_segment = os; | |
87 | this->u_.in_output_segment.offset_base = offset_base; | |
88 | this->source_ = IN_OUTPUT_SEGMENT; | |
89 | } | |
90 | ||
91 | // Initialize the fields in the base class Symbol for a symbol defined | |
92 | // as a constant. | |
93 | ||
94 | void | |
95 | Symbol::init_base(const char* name, elfcpp::STT type, | |
96 | elfcpp::STB binding, elfcpp::STV visibility, | |
97 | unsigned char nonvis) | |
98 | { | |
99 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
100 | this->source_ = CONSTANT; | |
101 | } | |
102 | ||
103 | // Initialize the fields in Sized_symbol for SYM in OBJECT. | |
14bfc3f5 ILT |
104 | |
105 | template<int size> | |
106 | template<bool big_endian> | |
107 | void | |
108 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, | |
109 | const elfcpp::Sym<size, big_endian>& sym) | |
110 | { | |
111 | this->init_base(name, version, object, sym); | |
112 | this->value_ = sym.get_st_value(); | |
ead1e424 ILT |
113 | this->symsize_ = sym.get_st_size(); |
114 | } | |
115 | ||
116 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
117 | // Output_data. | |
118 | ||
119 | template<int size> | |
120 | void | |
121 | Sized_symbol<size>::init(const char* name, Output_data* od, | |
122 | Value_type value, Size_type symsize, | |
123 | elfcpp::STT type, elfcpp::STB binding, | |
124 | elfcpp::STV visibility, unsigned char nonvis, | |
125 | bool offset_is_from_end) | |
126 | { | |
127 | this->init_base(name, od, type, binding, visibility, nonvis, | |
128 | offset_is_from_end); | |
129 | this->value_ = value; | |
130 | this->symsize_ = symsize; | |
131 | } | |
132 | ||
133 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
134 | // Output_segment. | |
135 | ||
136 | template<int size> | |
137 | void | |
138 | Sized_symbol<size>::init(const char* name, Output_segment* os, | |
139 | Value_type value, Size_type symsize, | |
140 | elfcpp::STT type, elfcpp::STB binding, | |
141 | elfcpp::STV visibility, unsigned char nonvis, | |
142 | Segment_offset_base offset_base) | |
143 | { | |
144 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); | |
145 | this->value_ = value; | |
146 | this->symsize_ = symsize; | |
147 | } | |
148 | ||
149 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
150 | // constant. | |
151 | ||
152 | template<int size> | |
153 | void | |
154 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, | |
155 | elfcpp::STT type, elfcpp::STB binding, | |
156 | elfcpp::STV visibility, unsigned char nonvis) | |
157 | { | |
158 | this->init_base(name, type, binding, visibility, nonvis); | |
159 | this->value_ = value; | |
160 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
161 | } |
162 | ||
163 | // Class Symbol_table. | |
164 | ||
165 | Symbol_table::Symbol_table() | |
ead1e424 | 166 | : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(), |
f6ce93d6 | 167 | forwarders_(), commons_(), warnings_() |
14bfc3f5 ILT |
168 | { |
169 | } | |
170 | ||
171 | Symbol_table::~Symbol_table() | |
172 | { | |
173 | } | |
174 | ||
175 | // The hash function. The key is always canonicalized, so we use a | |
176 | // simple combination of the pointers. | |
177 | ||
178 | size_t | |
179 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const | |
180 | { | |
f0641a0b | 181 | return key.first ^ key.second; |
14bfc3f5 ILT |
182 | } |
183 | ||
184 | // The symbol table key equality function. This is only called with | |
185 | // canonicalized name and version strings, so we can use pointer | |
186 | // comparison. | |
187 | ||
188 | bool | |
189 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, | |
190 | const Symbol_table_key& k2) const | |
191 | { | |
192 | return k1.first == k2.first && k1.second == k2.second; | |
193 | } | |
194 | ||
195 | // Make TO a symbol which forwards to FROM. | |
196 | ||
197 | void | |
198 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
199 | { | |
a3ad94ed ILT |
200 | gold_assert(from != to); |
201 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); | |
14bfc3f5 ILT |
202 | this->forwarders_[from] = to; |
203 | from->set_forwarder(); | |
204 | } | |
205 | ||
61ba1cf9 ILT |
206 | // Resolve the forwards from FROM, returning the real symbol. |
207 | ||
14bfc3f5 | 208 | Symbol* |
c06b7b0b | 209 | Symbol_table::resolve_forwards(const Symbol* from) const |
14bfc3f5 | 210 | { |
a3ad94ed | 211 | gold_assert(from->is_forwarder()); |
c06b7b0b | 212 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
14bfc3f5 | 213 | this->forwarders_.find(from); |
a3ad94ed | 214 | gold_assert(p != this->forwarders_.end()); |
14bfc3f5 ILT |
215 | return p->second; |
216 | } | |
217 | ||
61ba1cf9 ILT |
218 | // Look up a symbol by name. |
219 | ||
220 | Symbol* | |
221 | Symbol_table::lookup(const char* name, const char* version) const | |
222 | { | |
f0641a0b ILT |
223 | Stringpool::Key name_key; |
224 | name = this->namepool_.find(name, &name_key); | |
61ba1cf9 ILT |
225 | if (name == NULL) |
226 | return NULL; | |
f0641a0b ILT |
227 | |
228 | Stringpool::Key version_key = 0; | |
61ba1cf9 ILT |
229 | if (version != NULL) |
230 | { | |
f0641a0b | 231 | version = this->namepool_.find(version, &version_key); |
61ba1cf9 ILT |
232 | if (version == NULL) |
233 | return NULL; | |
234 | } | |
235 | ||
f0641a0b | 236 | Symbol_table_key key(name_key, version_key); |
61ba1cf9 ILT |
237 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
238 | if (p == this->table_.end()) | |
239 | return NULL; | |
240 | return p->second; | |
241 | } | |
242 | ||
14bfc3f5 ILT |
243 | // Resolve a Symbol with another Symbol. This is only used in the |
244 | // unusual case where there are references to both an unversioned | |
245 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
246 | // version is the default version. Because this is unusual, we do |
247 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 248 | |
1564db8d | 249 | template<int size, bool big_endian> |
14bfc3f5 | 250 | void |
5482377d ILT |
251 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from |
252 | ACCEPT_SIZE_ENDIAN) | |
14bfc3f5 | 253 | { |
1564db8d ILT |
254 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
255 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
256 | // We don't bother to set the st_name field. | |
257 | esym.put_st_value(from->value()); | |
258 | esym.put_st_size(from->symsize()); | |
259 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 260 | esym.put_st_other(from->visibility(), from->nonvis()); |
1564db8d ILT |
261 | esym.put_st_shndx(from->shnum()); |
262 | Symbol_table::resolve(to, esym.sym(), from->object()); | |
14bfc3f5 ILT |
263 | } |
264 | ||
265 | // Add one symbol from OBJECT to the symbol table. NAME is symbol | |
266 | // name and VERSION is the version; both are canonicalized. DEF is | |
267 | // whether this is the default version. | |
268 | ||
269 | // If DEF is true, then this is the definition of a default version of | |
270 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
271 | // of NAME/VERSION should always return the same symbol. This is | |
272 | // obvious for references, but in particular we want to do this for | |
273 | // definitions: overriding NAME/NULL should also override | |
274 | // NAME/VERSION. If we don't do that, it would be very hard to | |
275 | // override functions in a shared library which uses versioning. | |
276 | ||
277 | // We implement this by simply making both entries in the hash table | |
278 | // point to the same Symbol structure. That is easy enough if this is | |
279 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
280 | // that we have seen both already, in which case they will both have | |
281 | // independent entries in the symbol table. We can't simply change | |
282 | // the symbol table entry, because we have pointers to the entries | |
283 | // attached to the object files. So we mark the entry attached to the | |
284 | // object file as a forwarder, and record it in the forwarders_ map. | |
285 | // Note that entries in the hash table will never be marked as | |
286 | // forwarders. | |
287 | ||
288 | template<int size, bool big_endian> | |
289 | Symbol* | |
f6ce93d6 | 290 | Symbol_table::add_from_object(Object* object, |
14bfc3f5 | 291 | const char *name, |
f0641a0b ILT |
292 | Stringpool::Key name_key, |
293 | const char *version, | |
294 | Stringpool::Key version_key, | |
295 | bool def, | |
14bfc3f5 ILT |
296 | const elfcpp::Sym<size, big_endian>& sym) |
297 | { | |
298 | Symbol* const snull = NULL; | |
299 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
f0641a0b ILT |
300 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
301 | snull)); | |
14bfc3f5 ILT |
302 | |
303 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
304 | std::make_pair(this->table_.end(), false); | |
305 | if (def) | |
306 | { | |
f0641a0b ILT |
307 | const Stringpool::Key vnull_key = 0; |
308 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
309 | vnull_key), | |
14bfc3f5 ILT |
310 | snull)); |
311 | } | |
312 | ||
313 | // ins.first: an iterator, which is a pointer to a pair. | |
314 | // ins.first->first: the key (a pair of name and version). | |
315 | // ins.first->second: the value (Symbol*). | |
316 | // ins.second: true if new entry was inserted, false if not. | |
317 | ||
1564db8d | 318 | Sized_symbol<size>* ret; |
ead1e424 ILT |
319 | bool was_undefined; |
320 | bool was_common; | |
14bfc3f5 ILT |
321 | if (!ins.second) |
322 | { | |
323 | // We already have an entry for NAME/VERSION. | |
593f47df ILT |
324 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second |
325 | SELECT_SIZE(size)); | |
a3ad94ed | 326 | gold_assert(ret != NULL); |
ead1e424 ILT |
327 | |
328 | was_undefined = ret->is_undefined(); | |
329 | was_common = ret->is_common(); | |
330 | ||
14bfc3f5 ILT |
331 | Symbol_table::resolve(ret, sym, object); |
332 | ||
333 | if (def) | |
334 | { | |
335 | if (insdef.second) | |
336 | { | |
337 | // This is the first time we have seen NAME/NULL. Make | |
338 | // NAME/NULL point to NAME/VERSION. | |
339 | insdef.first->second = ret; | |
340 | } | |
dbe717ef | 341 | else if (insdef.first->second != ret) |
14bfc3f5 ILT |
342 | { |
343 | // This is the unfortunate case where we already have | |
344 | // entries for both NAME/VERSION and NAME/NULL. | |
274e99f9 | 345 | const Sized_symbol<size>* sym2; |
593f47df | 346 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d ILT |
347 | insdef.first->second |
348 | SELECT_SIZE(size)); | |
593f47df | 349 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
5482377d | 350 | ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian)); |
14bfc3f5 ILT |
351 | this->make_forwarder(insdef.first->second, ret); |
352 | insdef.first->second = ret; | |
353 | } | |
354 | } | |
355 | } | |
356 | else | |
357 | { | |
358 | // This is the first time we have seen NAME/VERSION. | |
a3ad94ed | 359 | gold_assert(ins.first->second == NULL); |
ead1e424 ILT |
360 | |
361 | was_undefined = false; | |
362 | was_common = false; | |
363 | ||
14bfc3f5 ILT |
364 | if (def && !insdef.second) |
365 | { | |
366 | // We already have an entry for NAME/NULL. Make | |
367 | // NAME/VERSION point to it. | |
593f47df ILT |
368 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
369 | insdef.first->second | |
370 | SELECT_SIZE(size)); | |
14bfc3f5 ILT |
371 | Symbol_table::resolve(ret, sym, object); |
372 | ins.first->second = ret; | |
373 | } | |
374 | else | |
375 | { | |
f6ce93d6 ILT |
376 | Sized_target<size, big_endian>* target = |
377 | object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
378 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
1564db8d ILT |
379 | if (!target->has_make_symbol()) |
380 | ret = new Sized_symbol<size>(); | |
381 | else | |
14bfc3f5 | 382 | { |
1564db8d ILT |
383 | ret = target->make_symbol(); |
384 | if (ret == NULL) | |
14bfc3f5 ILT |
385 | { |
386 | // This means that we don't want a symbol table | |
387 | // entry after all. | |
388 | if (!def) | |
389 | this->table_.erase(ins.first); | |
390 | else | |
391 | { | |
392 | this->table_.erase(insdef.first); | |
393 | // Inserting insdef invalidated ins. | |
f0641a0b ILT |
394 | this->table_.erase(std::make_pair(name_key, |
395 | version_key)); | |
14bfc3f5 ILT |
396 | } |
397 | return NULL; | |
398 | } | |
399 | } | |
14bfc3f5 | 400 | |
1564db8d ILT |
401 | ret->init(name, version, object, sym); |
402 | ||
14bfc3f5 ILT |
403 | ins.first->second = ret; |
404 | if (def) | |
405 | { | |
406 | // This is the first time we have seen NAME/NULL. Point | |
407 | // it at the new entry for NAME/VERSION. | |
a3ad94ed | 408 | gold_assert(insdef.second); |
14bfc3f5 ILT |
409 | insdef.first->second = ret; |
410 | } | |
411 | } | |
412 | } | |
413 | ||
ead1e424 ILT |
414 | // Record every time we see a new undefined symbol, to speed up |
415 | // archive groups. | |
416 | if (!was_undefined && ret->is_undefined()) | |
417 | ++this->saw_undefined_; | |
418 | ||
419 | // Keep track of common symbols, to speed up common symbol | |
420 | // allocation. | |
421 | if (!was_common && ret->is_common()) | |
422 | this->commons_.push_back(ret); | |
423 | ||
14bfc3f5 ILT |
424 | return ret; |
425 | } | |
426 | ||
f6ce93d6 | 427 | // Add all the symbols in a relocatable object to the hash table. |
14bfc3f5 ILT |
428 | |
429 | template<int size, bool big_endian> | |
430 | void | |
dbe717ef ILT |
431 | Symbol_table::add_from_relobj( |
432 | Sized_relobj<size, big_endian>* relobj, | |
f6ce93d6 | 433 | const unsigned char* syms, |
14bfc3f5 ILT |
434 | size_t count, |
435 | const char* sym_names, | |
436 | size_t sym_name_size, | |
437 | Symbol** sympointers) | |
438 | { | |
439 | // We take the size from the first object we see. | |
440 | if (this->get_size() == 0) | |
441 | this->set_size(size); | |
442 | ||
dbe717ef | 443 | if (size != this->get_size() || size != relobj->target()->get_size()) |
14bfc3f5 ILT |
444 | { |
445 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
dbe717ef | 446 | program_name, relobj->name().c_str()); |
14bfc3f5 ILT |
447 | gold_exit(false); |
448 | } | |
449 | ||
a783673b ILT |
450 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
451 | ||
f6ce93d6 | 452 | const unsigned char* p = syms; |
a783673b | 453 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
454 | { |
455 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 456 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 457 | |
a783673b | 458 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
459 | if (st_name >= sym_name_size) |
460 | { | |
54dc6425 ILT |
461 | fprintf(stderr, |
462 | _("%s: %s: bad global symbol name offset %u at %lu\n"), | |
dbe717ef | 463 | program_name, relobj->name().c_str(), st_name, |
14bfc3f5 ILT |
464 | static_cast<unsigned long>(i)); |
465 | gold_exit(false); | |
466 | } | |
467 | ||
dbe717ef ILT |
468 | const char* name = sym_names + st_name; |
469 | ||
a783673b ILT |
470 | // A symbol defined in a section which we are not including must |
471 | // be treated as an undefined symbol. | |
472 | unsigned char symbuf[sym_size]; | |
473 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
474 | unsigned int st_shndx = psym->get_st_shndx(); | |
475 | if (st_shndx != elfcpp::SHN_UNDEF | |
476 | && st_shndx < elfcpp::SHN_LORESERVE | |
dbe717ef | 477 | && !relobj->is_section_included(st_shndx)) |
a783673b ILT |
478 | { |
479 | memcpy(symbuf, p, sym_size); | |
480 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
481 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
482 | psym = &sym2; | |
483 | } | |
484 | ||
14bfc3f5 ILT |
485 | // In an object file, an '@' in the name separates the symbol |
486 | // name from the version name. If there are two '@' characters, | |
487 | // this is the default version. | |
488 | const char* ver = strchr(name, '@'); | |
489 | ||
490 | Symbol* res; | |
491 | if (ver == NULL) | |
492 | { | |
f0641a0b ILT |
493 | Stringpool::Key name_key; |
494 | name = this->namepool_.add(name, &name_key); | |
dbe717ef | 495 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
f0641a0b | 496 | false, *psym); |
14bfc3f5 ILT |
497 | } |
498 | else | |
499 | { | |
f0641a0b ILT |
500 | Stringpool::Key name_key; |
501 | name = this->namepool_.add(name, ver - name, &name_key); | |
502 | ||
14bfc3f5 ILT |
503 | bool def = false; |
504 | ++ver; | |
505 | if (*ver == '@') | |
506 | { | |
507 | def = true; | |
508 | ++ver; | |
509 | } | |
f0641a0b ILT |
510 | |
511 | Stringpool::Key ver_key; | |
512 | ver = this->namepool_.add(ver, &ver_key); | |
513 | ||
dbe717ef | 514 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
f0641a0b | 515 | def, *psym); |
14bfc3f5 ILT |
516 | } |
517 | ||
518 | *sympointers++ = res; | |
14bfc3f5 ILT |
519 | } |
520 | } | |
521 | ||
dbe717ef ILT |
522 | // Add all the symbols in a dynamic object to the hash table. |
523 | ||
524 | template<int size, bool big_endian> | |
525 | void | |
526 | Symbol_table::add_from_dynobj( | |
527 | Sized_dynobj<size, big_endian>* dynobj, | |
528 | const unsigned char* syms, | |
529 | size_t count, | |
530 | const char* sym_names, | |
531 | size_t sym_name_size, | |
532 | const unsigned char* versym, | |
533 | size_t versym_size, | |
534 | const std::vector<const char*>* version_map) | |
535 | { | |
536 | // We take the size from the first object we see. | |
537 | if (this->get_size() == 0) | |
538 | this->set_size(size); | |
539 | ||
540 | if (size != this->get_size() || size != dynobj->target()->get_size()) | |
541 | { | |
542 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
543 | program_name, dynobj->name().c_str()); | |
544 | gold_exit(false); | |
545 | } | |
546 | ||
547 | if (versym != NULL && versym_size / 2 < count) | |
548 | { | |
549 | fprintf(stderr, _("%s: %s: too few symbol versions\n"), | |
550 | program_name, dynobj->name().c_str()); | |
551 | gold_exit(false); | |
552 | } | |
553 | ||
554 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
555 | ||
556 | const unsigned char* p = syms; | |
557 | const unsigned char* vs = versym; | |
558 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) | |
559 | { | |
560 | elfcpp::Sym<size, big_endian> sym(p); | |
561 | ||
562 | // Ignore symbols with local binding. | |
563 | if (sym.get_st_bind() == elfcpp::STB_LOCAL) | |
564 | continue; | |
565 | ||
566 | unsigned int st_name = sym.get_st_name(); | |
567 | if (st_name >= sym_name_size) | |
568 | { | |
569 | fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"), | |
570 | program_name, dynobj->name().c_str(), st_name, | |
571 | static_cast<unsigned long>(i)); | |
572 | gold_exit(false); | |
573 | } | |
574 | ||
575 | const char* name = sym_names + st_name; | |
576 | ||
577 | if (versym == NULL) | |
578 | { | |
579 | Stringpool::Key name_key; | |
580 | name = this->namepool_.add(name, &name_key); | |
581 | this->add_from_object(dynobj, name, name_key, NULL, 0, | |
582 | false, sym); | |
583 | continue; | |
584 | } | |
585 | ||
586 | // Read the version information. | |
587 | ||
588 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); | |
589 | ||
590 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; | |
591 | v &= elfcpp::VERSYM_VERSION; | |
592 | ||
593 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)) | |
594 | { | |
595 | // This symbol should not be visible outside the object. | |
596 | continue; | |
597 | } | |
598 | ||
599 | // At this point we are definitely going to add this symbol. | |
600 | Stringpool::Key name_key; | |
601 | name = this->namepool_.add(name, &name_key); | |
602 | ||
603 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) | |
604 | { | |
605 | // This symbol does not have a version. | |
606 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); | |
607 | continue; | |
608 | } | |
609 | ||
610 | if (v >= version_map->size()) | |
611 | { | |
612 | fprintf(stderr, | |
613 | _("%s: %s: versym for symbol %zu out of range: %u\n"), | |
614 | program_name, dynobj->name().c_str(), i, v); | |
615 | gold_exit(false); | |
616 | } | |
617 | ||
618 | const char* version = (*version_map)[v]; | |
619 | if (version == NULL) | |
620 | { | |
621 | fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"), | |
622 | program_name, dynobj->name().c_str(), i, v); | |
623 | gold_exit(false); | |
624 | } | |
625 | ||
626 | Stringpool::Key version_key; | |
627 | version = this->namepool_.add(version, &version_key); | |
628 | ||
629 | // If this is an absolute symbol, and the version name and | |
630 | // symbol name are the same, then this is the version definition | |
631 | // symbol. These symbols exist to support using -u to pull in | |
632 | // particular versions. We do not want to record a version for | |
633 | // them. | |
634 | if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key) | |
635 | { | |
636 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); | |
637 | continue; | |
638 | } | |
639 | ||
640 | const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF; | |
641 | ||
642 | this->add_from_object(dynobj, name, name_key, version, version_key, | |
643 | def, sym); | |
644 | } | |
645 | } | |
646 | ||
ead1e424 ILT |
647 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
648 | // true, then only create the symbol if there is a reference to it. | |
649 | ||
650 | template<int size, bool big_endian> | |
651 | Sized_symbol<size>* | |
652 | Symbol_table::define_special_symbol(Target* target, const char* name, | |
593f47df ILT |
653 | bool only_if_ref |
654 | ACCEPT_SIZE_ENDIAN) | |
ead1e424 | 655 | { |
a3ad94ed | 656 | gold_assert(this->size_ == size); |
ead1e424 ILT |
657 | |
658 | Symbol* oldsym; | |
659 | Sized_symbol<size>* sym; | |
660 | ||
661 | if (only_if_ref) | |
662 | { | |
663 | oldsym = this->lookup(name, NULL); | |
f6ce93d6 | 664 | if (oldsym == NULL || !oldsym->is_undefined()) |
ead1e424 ILT |
665 | return NULL; |
666 | sym = NULL; | |
667 | ||
668 | // Canonicalize NAME. | |
669 | name = oldsym->name(); | |
670 | } | |
671 | else | |
672 | { | |
673 | // Canonicalize NAME. | |
f0641a0b ILT |
674 | Stringpool::Key name_key; |
675 | name = this->namepool_.add(name, &name_key); | |
ead1e424 ILT |
676 | |
677 | Symbol* const snull = NULL; | |
f0641a0b | 678 | const Stringpool::Key ver_key = 0; |
ead1e424 | 679 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
f0641a0b | 680 | this->table_.insert(std::make_pair(std::make_pair(name_key, ver_key), |
ead1e424 ILT |
681 | snull)); |
682 | ||
683 | if (!ins.second) | |
684 | { | |
685 | // We already have a symbol table entry for NAME. | |
686 | oldsym = ins.first->second; | |
a3ad94ed | 687 | gold_assert(oldsym != NULL); |
ead1e424 ILT |
688 | sym = NULL; |
689 | } | |
690 | else | |
691 | { | |
692 | // We haven't seen this symbol before. | |
a3ad94ed | 693 | gold_assert(ins.first->second == NULL); |
ead1e424 ILT |
694 | |
695 | if (!target->has_make_symbol()) | |
696 | sym = new Sized_symbol<size>(); | |
697 | else | |
698 | { | |
a3ad94ed ILT |
699 | gold_assert(target->get_size() == size); |
700 | gold_assert(target->is_big_endian() ? big_endian : !big_endian); | |
ead1e424 ILT |
701 | typedef Sized_target<size, big_endian> My_target; |
702 | My_target* sized_target = static_cast<My_target*>(target); | |
703 | sym = sized_target->make_symbol(); | |
704 | if (sym == NULL) | |
705 | return NULL; | |
706 | } | |
707 | ||
708 | ins.first->second = sym; | |
709 | oldsym = NULL; | |
710 | } | |
711 | } | |
712 | ||
713 | if (oldsym != NULL) | |
714 | { | |
a3ad94ed | 715 | gold_assert(sym == NULL); |
ead1e424 | 716 | |
593f47df ILT |
717 | sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym |
718 | SELECT_SIZE(size)); | |
a3ad94ed | 719 | gold_assert(sym->source() == Symbol::FROM_OBJECT); |
ead1e424 ILT |
720 | const int old_shnum = sym->shnum(); |
721 | if (old_shnum != elfcpp::SHN_UNDEF | |
722 | && old_shnum != elfcpp::SHN_COMMON | |
723 | && !sym->object()->is_dynamic()) | |
724 | { | |
725 | fprintf(stderr, "%s: linker defined: multiple definition of %s\n", | |
726 | program_name, name); | |
727 | // FIXME: Report old location. Record that we have seen an | |
728 | // error. | |
729 | return NULL; | |
730 | } | |
731 | ||
732 | // Our new definition is going to override the old reference. | |
733 | } | |
734 | ||
735 | return sym; | |
736 | } | |
737 | ||
738 | // Define a symbol based on an Output_data. | |
739 | ||
740 | void | |
741 | Symbol_table::define_in_output_data(Target* target, const char* name, | |
742 | Output_data* od, | |
743 | uint64_t value, uint64_t symsize, | |
744 | elfcpp::STT type, elfcpp::STB binding, | |
745 | elfcpp::STV visibility, | |
746 | unsigned char nonvis, | |
747 | bool offset_is_from_end, | |
748 | bool only_if_ref) | |
749 | { | |
a3ad94ed | 750 | gold_assert(target->get_size() == this->size_); |
ead1e424 ILT |
751 | if (this->size_ == 32) |
752 | this->do_define_in_output_data<32>(target, name, od, value, symsize, | |
753 | type, binding, visibility, nonvis, | |
754 | offset_is_from_end, only_if_ref); | |
755 | else if (this->size_ == 64) | |
756 | this->do_define_in_output_data<64>(target, name, od, value, symsize, | |
757 | type, binding, visibility, nonvis, | |
758 | offset_is_from_end, only_if_ref); | |
759 | else | |
a3ad94ed | 760 | gold_unreachable(); |
ead1e424 ILT |
761 | } |
762 | ||
763 | // Define a symbol in an Output_data, sized version. | |
764 | ||
765 | template<int size> | |
766 | void | |
767 | Symbol_table::do_define_in_output_data( | |
768 | Target* target, | |
769 | const char* name, | |
770 | Output_data* od, | |
771 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
772 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
773 | elfcpp::STT type, | |
774 | elfcpp::STB binding, | |
775 | elfcpp::STV visibility, | |
776 | unsigned char nonvis, | |
777 | bool offset_is_from_end, | |
778 | bool only_if_ref) | |
779 | { | |
780 | Sized_symbol<size>* sym; | |
781 | ||
782 | if (target->is_big_endian()) | |
593f47df ILT |
783 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
784 | target, name, only_if_ref | |
785 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 786 | else |
593f47df ILT |
787 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
788 | target, name, only_if_ref | |
789 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
790 | |
791 | if (sym == NULL) | |
792 | return; | |
793 | ||
794 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, | |
795 | offset_is_from_end); | |
796 | } | |
797 | ||
798 | // Define a symbol based on an Output_segment. | |
799 | ||
800 | void | |
801 | Symbol_table::define_in_output_segment(Target* target, const char* name, | |
802 | Output_segment* os, | |
803 | uint64_t value, uint64_t symsize, | |
804 | elfcpp::STT type, elfcpp::STB binding, | |
805 | elfcpp::STV visibility, | |
806 | unsigned char nonvis, | |
807 | Symbol::Segment_offset_base offset_base, | |
808 | bool only_if_ref) | |
809 | { | |
a3ad94ed | 810 | gold_assert(target->get_size() == this->size_); |
ead1e424 ILT |
811 | if (this->size_ == 32) |
812 | this->do_define_in_output_segment<32>(target, name, os, value, symsize, | |
813 | type, binding, visibility, nonvis, | |
814 | offset_base, only_if_ref); | |
815 | else if (this->size_ == 64) | |
816 | this->do_define_in_output_segment<64>(target, name, os, value, symsize, | |
817 | type, binding, visibility, nonvis, | |
818 | offset_base, only_if_ref); | |
819 | else | |
a3ad94ed | 820 | gold_unreachable(); |
ead1e424 ILT |
821 | } |
822 | ||
823 | // Define a symbol in an Output_segment, sized version. | |
824 | ||
825 | template<int size> | |
826 | void | |
827 | Symbol_table::do_define_in_output_segment( | |
828 | Target* target, | |
829 | const char* name, | |
830 | Output_segment* os, | |
831 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
832 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
833 | elfcpp::STT type, | |
834 | elfcpp::STB binding, | |
835 | elfcpp::STV visibility, | |
836 | unsigned char nonvis, | |
837 | Symbol::Segment_offset_base offset_base, | |
838 | bool only_if_ref) | |
839 | { | |
840 | Sized_symbol<size>* sym; | |
841 | ||
842 | if (target->is_big_endian()) | |
593f47df ILT |
843 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
844 | target, name, only_if_ref | |
845 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 846 | else |
593f47df ILT |
847 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
848 | target, name, only_if_ref | |
849 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
850 | |
851 | if (sym == NULL) | |
852 | return; | |
853 | ||
854 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, | |
855 | offset_base); | |
856 | } | |
857 | ||
858 | // Define a special symbol with a constant value. It is a multiple | |
859 | // definition error if this symbol is already defined. | |
860 | ||
861 | void | |
862 | Symbol_table::define_as_constant(Target* target, const char* name, | |
863 | uint64_t value, uint64_t symsize, | |
864 | elfcpp::STT type, elfcpp::STB binding, | |
865 | elfcpp::STV visibility, unsigned char nonvis, | |
866 | bool only_if_ref) | |
867 | { | |
a3ad94ed | 868 | gold_assert(target->get_size() == this->size_); |
ead1e424 ILT |
869 | if (this->size_ == 32) |
870 | this->do_define_as_constant<32>(target, name, value, symsize, | |
871 | type, binding, visibility, nonvis, | |
872 | only_if_ref); | |
873 | else if (this->size_ == 64) | |
874 | this->do_define_as_constant<64>(target, name, value, symsize, | |
875 | type, binding, visibility, nonvis, | |
876 | only_if_ref); | |
877 | else | |
a3ad94ed | 878 | gold_unreachable(); |
ead1e424 ILT |
879 | } |
880 | ||
881 | // Define a symbol as a constant, sized version. | |
882 | ||
883 | template<int size> | |
884 | void | |
885 | Symbol_table::do_define_as_constant( | |
886 | Target* target, | |
887 | const char* name, | |
888 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
889 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
890 | elfcpp::STT type, | |
891 | elfcpp::STB binding, | |
892 | elfcpp::STV visibility, | |
893 | unsigned char nonvis, | |
894 | bool only_if_ref) | |
895 | { | |
896 | Sized_symbol<size>* sym; | |
897 | ||
898 | if (target->is_big_endian()) | |
593f47df ILT |
899 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
900 | target, name, only_if_ref | |
901 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 902 | else |
593f47df ILT |
903 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
904 | target, name, only_if_ref | |
905 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
906 | |
907 | if (sym == NULL) | |
908 | return; | |
909 | ||
910 | sym->init(name, value, symsize, type, binding, visibility, nonvis); | |
911 | } | |
912 | ||
913 | // Define a set of symbols in output sections. | |
914 | ||
915 | void | |
916 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
917 | const Define_symbol_in_section* p) | |
918 | { | |
919 | for (int i = 0; i < count; ++i, ++p) | |
920 | { | |
921 | Output_section* os = layout->find_output_section(p->output_section); | |
922 | if (os != NULL) | |
923 | this->define_in_output_data(target, p->name, os, p->value, p->size, | |
924 | p->type, p->binding, p->visibility, | |
925 | p->nonvis, p->offset_is_from_end, | |
926 | p->only_if_ref); | |
927 | else | |
928 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
929 | p->binding, p->visibility, p->nonvis, | |
930 | p->only_if_ref); | |
931 | } | |
932 | } | |
933 | ||
934 | // Define a set of symbols in output segments. | |
935 | ||
936 | void | |
937 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
938 | const Define_symbol_in_segment* p) | |
939 | { | |
940 | for (int i = 0; i < count; ++i, ++p) | |
941 | { | |
942 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
943 | p->segment_flags_set, | |
944 | p->segment_flags_clear); | |
945 | if (os != NULL) | |
946 | this->define_in_output_segment(target, p->name, os, p->value, p->size, | |
947 | p->type, p->binding, p->visibility, | |
948 | p->nonvis, p->offset_base, | |
949 | p->only_if_ref); | |
950 | else | |
951 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
952 | p->binding, p->visibility, p->nonvis, | |
953 | p->only_if_ref); | |
954 | } | |
955 | } | |
956 | ||
a3ad94ed ILT |
957 | // Set the dynamic symbol indexes. INDEX is the index of the first |
958 | // global dynamic symbol. Pointers to the symbols are stored into the | |
959 | // vector SYMS. The names are added to DYNPOOL. This returns an | |
960 | // updated dynamic symbol index. | |
961 | ||
962 | unsigned int | |
963 | Symbol_table::set_dynsym_indexes(unsigned int index, | |
964 | std::vector<Symbol*>* syms, | |
965 | Stringpool* dynpool) | |
966 | { | |
967 | for (Symbol_table_type::iterator p = this->table_.begin(); | |
968 | p != this->table_.end(); | |
969 | ++p) | |
970 | { | |
971 | Symbol* sym = p->second; | |
972 | if (sym->needs_dynsym_entry()) | |
973 | { | |
974 | sym->set_dynsym_index(index); | |
975 | ++index; | |
976 | syms->push_back(sym); | |
977 | dynpool->add(sym->name(), NULL); | |
978 | } | |
979 | } | |
980 | ||
981 | return index; | |
982 | } | |
983 | ||
c06b7b0b ILT |
984 | // Set the final values for all the symbols. The index of the first |
985 | // global symbol in the output file is INDEX. Record the file offset | |
75f65a3e | 986 | // OFF. Add their names to POOL. Return the new file offset. |
54dc6425 | 987 | |
75f65a3e | 988 | off_t |
c06b7b0b | 989 | Symbol_table::finalize(unsigned int index, off_t off, Stringpool* pool) |
54dc6425 | 990 | { |
f6ce93d6 ILT |
991 | off_t ret; |
992 | ||
a3ad94ed | 993 | gold_assert(index != 0); |
c06b7b0b ILT |
994 | this->first_global_index_ = index; |
995 | ||
75f65a3e | 996 | if (this->size_ == 32) |
c06b7b0b | 997 | ret = this->sized_finalize<32>(index, off, pool); |
61ba1cf9 | 998 | else if (this->size_ == 64) |
c06b7b0b | 999 | ret = this->sized_finalize<64>(index, off, pool); |
61ba1cf9 | 1000 | else |
a3ad94ed | 1001 | gold_unreachable(); |
f6ce93d6 ILT |
1002 | |
1003 | // Now that we have the final symbol table, we can reliably note | |
1004 | // which symbols should get warnings. | |
1005 | this->warnings_.note_warnings(this); | |
1006 | ||
1007 | return ret; | |
75f65a3e ILT |
1008 | } |
1009 | ||
ead1e424 ILT |
1010 | // Set the final value for all the symbols. This is called after |
1011 | // Layout::finalize, so all the output sections have their final | |
1012 | // address. | |
75f65a3e ILT |
1013 | |
1014 | template<int size> | |
1015 | off_t | |
c06b7b0b | 1016 | Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool) |
75f65a3e | 1017 | { |
ead1e424 | 1018 | off = align_address(off, size >> 3); |
75f65a3e ILT |
1019 | this->offset_ = off; |
1020 | ||
c06b7b0b ILT |
1021 | size_t orig_index = index; |
1022 | ||
75f65a3e | 1023 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
c06b7b0b ILT |
1024 | for (Symbol_table_type::iterator p = this->table_.begin(); |
1025 | p != this->table_.end(); | |
1026 | ++p) | |
54dc6425 | 1027 | { |
75f65a3e | 1028 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
54dc6425 | 1029 | |
75f65a3e | 1030 | // FIXME: Here we need to decide which symbols should go into |
a3ad94ed ILT |
1031 | // the output file, based on --strip. |
1032 | ||
1033 | // The default version of a symbol may appear twice in the | |
1034 | // symbol table. We only need to finalize it once. | |
1035 | if (sym->has_symtab_index()) | |
1036 | continue; | |
75f65a3e | 1037 | |
ead1e424 | 1038 | typename Sized_symbol<size>::Value_type value; |
75f65a3e | 1039 | |
ead1e424 | 1040 | switch (sym->source()) |
75f65a3e | 1041 | { |
ead1e424 ILT |
1042 | case Symbol::FROM_OBJECT: |
1043 | { | |
1044 | unsigned int shnum = sym->shnum(); | |
1045 | ||
1046 | // FIXME: We need some target specific support here. | |
1047 | if (shnum >= elfcpp::SHN_LORESERVE | |
1048 | && shnum != elfcpp::SHN_ABS) | |
1049 | { | |
1050 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
1051 | program_name, sym->name(), shnum); | |
1052 | gold_exit(false); | |
1053 | } | |
1054 | ||
f6ce93d6 ILT |
1055 | Object* symobj = sym->object(); |
1056 | if (symobj->is_dynamic()) | |
1057 | { | |
1058 | value = 0; | |
1059 | shnum = elfcpp::SHN_UNDEF; | |
1060 | } | |
1061 | else if (shnum == elfcpp::SHN_UNDEF) | |
ead1e424 ILT |
1062 | value = 0; |
1063 | else if (shnum == elfcpp::SHN_ABS) | |
1064 | value = sym->value(); | |
1065 | else | |
1066 | { | |
f6ce93d6 | 1067 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ead1e424 | 1068 | off_t secoff; |
f6ce93d6 | 1069 | Output_section* os = relobj->output_section(shnum, &secoff); |
ead1e424 ILT |
1070 | |
1071 | if (os == NULL) | |
1072 | { | |
c06b7b0b | 1073 | sym->set_symtab_index(-1U); |
ead1e424 ILT |
1074 | continue; |
1075 | } | |
1076 | ||
1077 | value = sym->value() + os->address() + secoff; | |
1078 | } | |
1079 | } | |
1080 | break; | |
1081 | ||
1082 | case Symbol::IN_OUTPUT_DATA: | |
1083 | { | |
1084 | Output_data* od = sym->output_data(); | |
1085 | value = sym->value() + od->address(); | |
1086 | if (sym->offset_is_from_end()) | |
1087 | value += od->data_size(); | |
1088 | } | |
1089 | break; | |
1090 | ||
1091 | case Symbol::IN_OUTPUT_SEGMENT: | |
1092 | { | |
1093 | Output_segment* os = sym->output_segment(); | |
1094 | value = sym->value() + os->vaddr(); | |
1095 | switch (sym->offset_base()) | |
1096 | { | |
1097 | case Symbol::SEGMENT_START: | |
1098 | break; | |
1099 | case Symbol::SEGMENT_END: | |
1100 | value += os->memsz(); | |
1101 | break; | |
1102 | case Symbol::SEGMENT_BSS: | |
1103 | value += os->filesz(); | |
1104 | break; | |
1105 | default: | |
a3ad94ed | 1106 | gold_unreachable(); |
ead1e424 ILT |
1107 | } |
1108 | } | |
1109 | break; | |
1110 | ||
1111 | case Symbol::CONSTANT: | |
1112 | value = sym->value(); | |
1113 | break; | |
1114 | ||
1115 | default: | |
a3ad94ed | 1116 | gold_unreachable(); |
54dc6425 | 1117 | } |
ead1e424 ILT |
1118 | |
1119 | sym->set_value(value); | |
c06b7b0b | 1120 | sym->set_symtab_index(index); |
f0641a0b | 1121 | pool->add(sym->name(), NULL); |
c06b7b0b | 1122 | ++index; |
ead1e424 | 1123 | off += sym_size; |
54dc6425 | 1124 | } |
75f65a3e | 1125 | |
c06b7b0b | 1126 | this->output_count_ = index - orig_index; |
61ba1cf9 | 1127 | |
75f65a3e | 1128 | return off; |
54dc6425 ILT |
1129 | } |
1130 | ||
61ba1cf9 ILT |
1131 | // Write out the global symbols. |
1132 | ||
1133 | void | |
1134 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, | |
1135 | Output_file* of) const | |
1136 | { | |
1137 | if (this->size_ == 32) | |
1138 | { | |
1139 | if (target->is_big_endian()) | |
1140 | this->sized_write_globals<32, true>(target, sympool, of); | |
1141 | else | |
1142 | this->sized_write_globals<32, false>(target, sympool, of); | |
1143 | } | |
1144 | else if (this->size_ == 64) | |
1145 | { | |
1146 | if (target->is_big_endian()) | |
1147 | this->sized_write_globals<64, true>(target, sympool, of); | |
1148 | else | |
1149 | this->sized_write_globals<64, false>(target, sympool, of); | |
1150 | } | |
1151 | else | |
a3ad94ed | 1152 | gold_unreachable(); |
61ba1cf9 ILT |
1153 | } |
1154 | ||
1155 | // Write out the global symbols. | |
1156 | ||
1157 | template<int size, bool big_endian> | |
1158 | void | |
1159 | Symbol_table::sized_write_globals(const Target*, | |
1160 | const Stringpool* sympool, | |
1161 | Output_file* of) const | |
1162 | { | |
1163 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
c06b7b0b ILT |
1164 | unsigned int index = this->first_global_index_; |
1165 | const off_t oview_size = this->output_count_ * sym_size; | |
1166 | unsigned char* psyms = of->get_output_view(this->offset_, oview_size); | |
1167 | ||
61ba1cf9 ILT |
1168 | unsigned char* ps = psyms; |
1169 | for (Symbol_table_type::const_iterator p = this->table_.begin(); | |
1170 | p != this->table_.end(); | |
1171 | ++p) | |
1172 | { | |
1173 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
1174 | ||
a3ad94ed ILT |
1175 | unsigned int sym_index = sym->symtab_index(); |
1176 | if (sym_index == -1U) | |
1177 | { | |
1178 | // This symbol is not included in the output file. | |
1179 | continue; | |
1180 | } | |
1181 | if (sym_index != index) | |
1182 | { | |
1183 | // We have already seen this symbol, because it has a | |
1184 | // default version. | |
1185 | gold_assert(sym_index < index); | |
1186 | continue; | |
1187 | } | |
1188 | ++index; | |
c06b7b0b | 1189 | |
ead1e424 ILT |
1190 | unsigned int shndx; |
1191 | switch (sym->source()) | |
1192 | { | |
1193 | case Symbol::FROM_OBJECT: | |
1194 | { | |
1195 | unsigned int shnum = sym->shnum(); | |
1196 | ||
1197 | // FIXME: We need some target specific support here. | |
1198 | if (shnum >= elfcpp::SHN_LORESERVE | |
1199 | && shnum != elfcpp::SHN_ABS) | |
1200 | { | |
1201 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
1202 | program_name, sym->name(), sym->shnum()); | |
1203 | gold_exit(false); | |
1204 | } | |
1205 | ||
f6ce93d6 ILT |
1206 | Object* symobj = sym->object(); |
1207 | if (symobj->is_dynamic()) | |
1208 | { | |
1209 | // FIXME. | |
1210 | shndx = elfcpp::SHN_UNDEF; | |
1211 | } | |
1212 | else if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS) | |
ead1e424 ILT |
1213 | shndx = shnum; |
1214 | else | |
1215 | { | |
f6ce93d6 | 1216 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ead1e424 | 1217 | off_t secoff; |
f6ce93d6 | 1218 | Output_section* os = relobj->output_section(shnum, &secoff); |
a3ad94ed | 1219 | gold_assert(os != NULL); |
ead1e424 ILT |
1220 | shndx = os->out_shndx(); |
1221 | } | |
1222 | } | |
1223 | break; | |
1224 | ||
1225 | case Symbol::IN_OUTPUT_DATA: | |
1226 | shndx = sym->output_data()->out_shndx(); | |
1227 | break; | |
1228 | ||
1229 | case Symbol::IN_OUTPUT_SEGMENT: | |
1230 | shndx = elfcpp::SHN_ABS; | |
1231 | break; | |
1232 | ||
1233 | case Symbol::CONSTANT: | |
1234 | shndx = elfcpp::SHN_ABS; | |
1235 | break; | |
1236 | ||
1237 | default: | |
a3ad94ed | 1238 | gold_unreachable(); |
ead1e424 | 1239 | } |
61ba1cf9 ILT |
1240 | |
1241 | elfcpp::Sym_write<size, big_endian> osym(ps); | |
1242 | osym.put_st_name(sympool->get_offset(sym->name())); | |
1243 | osym.put_st_value(sym->value()); | |
1244 | osym.put_st_size(sym->symsize()); | |
1245 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
ead1e424 ILT |
1246 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), |
1247 | sym->nonvis())); | |
1248 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
1249 | |
1250 | ps += sym_size; | |
1251 | } | |
1252 | ||
a3ad94ed | 1253 | gold_assert(ps - psyms == oview_size); |
c06b7b0b ILT |
1254 | |
1255 | of->write_output_view(this->offset_, oview_size, psyms); | |
61ba1cf9 ILT |
1256 | } |
1257 | ||
a3ad94ed ILT |
1258 | // Write out a section symbol. Return the update offset. |
1259 | ||
1260 | void | |
1261 | Symbol_table::write_section_symbol(const Target* target, | |
1262 | const Output_section *os, | |
1263 | Output_file* of, | |
1264 | off_t offset) const | |
1265 | { | |
1266 | if (this->size_ == 32) | |
1267 | { | |
1268 | if (target->is_big_endian()) | |
1269 | this->sized_write_section_symbol<32, true>(os, of, offset); | |
1270 | else | |
1271 | this->sized_write_section_symbol<32, false>(os, of, offset); | |
1272 | } | |
1273 | else if (this->size_ == 64) | |
1274 | { | |
1275 | if (target->is_big_endian()) | |
1276 | this->sized_write_section_symbol<64, true>(os, of, offset); | |
1277 | else | |
1278 | this->sized_write_section_symbol<64, false>(os, of, offset); | |
1279 | } | |
1280 | else | |
1281 | gold_unreachable(); | |
1282 | } | |
1283 | ||
1284 | // Write out a section symbol, specialized for size and endianness. | |
1285 | ||
1286 | template<int size, bool big_endian> | |
1287 | void | |
1288 | Symbol_table::sized_write_section_symbol(const Output_section* os, | |
1289 | Output_file* of, | |
1290 | off_t offset) const | |
1291 | { | |
1292 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
1293 | ||
1294 | unsigned char* pov = of->get_output_view(offset, sym_size); | |
1295 | ||
1296 | elfcpp::Sym_write<size, big_endian> osym(pov); | |
1297 | osym.put_st_name(0); | |
1298 | osym.put_st_value(os->address()); | |
1299 | osym.put_st_size(0); | |
1300 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, | |
1301 | elfcpp::STT_SECTION)); | |
1302 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); | |
1303 | osym.put_st_shndx(os->out_shndx()); | |
1304 | ||
1305 | of->write_output_view(offset, sym_size, pov); | |
1306 | } | |
1307 | ||
f6ce93d6 ILT |
1308 | // Warnings functions. |
1309 | ||
1310 | // Add a new warning. | |
1311 | ||
1312 | void | |
1313 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
1314 | unsigned int shndx) | |
1315 | { | |
1316 | name = symtab->canonicalize_name(name); | |
1317 | this->warnings_[name].set(obj, shndx); | |
1318 | } | |
1319 | ||
1320 | // Look through the warnings and mark the symbols for which we should | |
1321 | // warn. This is called during Layout::finalize when we know the | |
1322 | // sources for all the symbols. | |
1323 | ||
1324 | void | |
1325 | Warnings::note_warnings(Symbol_table* symtab) | |
1326 | { | |
1327 | for (Warning_table::iterator p = this->warnings_.begin(); | |
1328 | p != this->warnings_.end(); | |
1329 | ++p) | |
1330 | { | |
1331 | Symbol* sym = symtab->lookup(p->first, NULL); | |
1332 | if (sym != NULL | |
1333 | && sym->source() == Symbol::FROM_OBJECT | |
1334 | && sym->object() == p->second.object) | |
1335 | { | |
1336 | sym->set_has_warning(); | |
1337 | ||
1338 | // Read the section contents to get the warning text. It | |
1339 | // would be nicer if we only did this if we have to actually | |
1340 | // issue a warning. Unfortunately, warnings are issued as | |
1341 | // we relocate sections. That means that we can not lock | |
1342 | // the object then, as we might try to issue the same | |
1343 | // warning multiple times simultaneously. | |
645f8123 ILT |
1344 | { |
1345 | Task_locker_obj<Object> tl(*p->second.object); | |
1346 | const unsigned char* c; | |
1347 | off_t len; | |
1348 | c = p->second.object->section_contents(p->second.shndx, &len); | |
1349 | p->second.set_text(reinterpret_cast<const char*>(c), len); | |
1350 | } | |
f6ce93d6 ILT |
1351 | } |
1352 | } | |
1353 | } | |
1354 | ||
1355 | // Issue a warning. This is called when we see a relocation against a | |
1356 | // symbol for which has a warning. | |
1357 | ||
1358 | void | |
c06b7b0b | 1359 | Warnings::issue_warning(const Symbol* sym, const std::string& location) const |
f6ce93d6 | 1360 | { |
a3ad94ed | 1361 | gold_assert(sym->has_warning()); |
f6ce93d6 | 1362 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
a3ad94ed | 1363 | gold_assert(p != this->warnings_.end()); |
f6ce93d6 ILT |
1364 | fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(), |
1365 | p->second.text.c_str()); | |
1366 | } | |
1367 | ||
14bfc3f5 ILT |
1368 | // Instantiate the templates we need. We could use the configure |
1369 | // script to restrict this to only the ones needed for implemented | |
1370 | // targets. | |
1371 | ||
1372 | template | |
1373 | void | |
dbe717ef ILT |
1374 | Symbol_table::add_from_relobj<32, true>( |
1375 | Sized_relobj<32, true>* relobj, | |
f6ce93d6 | 1376 | const unsigned char* syms, |
14bfc3f5 ILT |
1377 | size_t count, |
1378 | const char* sym_names, | |
1379 | size_t sym_name_size, | |
1380 | Symbol** sympointers); | |
1381 | ||
1382 | template | |
1383 | void | |
dbe717ef ILT |
1384 | Symbol_table::add_from_relobj<32, false>( |
1385 | Sized_relobj<32, false>* relobj, | |
f6ce93d6 | 1386 | const unsigned char* syms, |
14bfc3f5 ILT |
1387 | size_t count, |
1388 | const char* sym_names, | |
1389 | size_t sym_name_size, | |
1390 | Symbol** sympointers); | |
1391 | ||
1392 | template | |
1393 | void | |
dbe717ef ILT |
1394 | Symbol_table::add_from_relobj<64, true>( |
1395 | Sized_relobj<64, true>* relobj, | |
f6ce93d6 | 1396 | const unsigned char* syms, |
14bfc3f5 ILT |
1397 | size_t count, |
1398 | const char* sym_names, | |
1399 | size_t sym_name_size, | |
1400 | Symbol** sympointers); | |
1401 | ||
1402 | template | |
1403 | void | |
dbe717ef ILT |
1404 | Symbol_table::add_from_relobj<64, false>( |
1405 | Sized_relobj<64, false>* relobj, | |
f6ce93d6 | 1406 | const unsigned char* syms, |
14bfc3f5 ILT |
1407 | size_t count, |
1408 | const char* sym_names, | |
1409 | size_t sym_name_size, | |
1410 | Symbol** sympointers); | |
1411 | ||
dbe717ef ILT |
1412 | template |
1413 | void | |
1414 | Symbol_table::add_from_dynobj<32, true>( | |
1415 | Sized_dynobj<32, true>* dynobj, | |
1416 | const unsigned char* syms, | |
1417 | size_t count, | |
1418 | const char* sym_names, | |
1419 | size_t sym_name_size, | |
1420 | const unsigned char* versym, | |
1421 | size_t versym_size, | |
1422 | const std::vector<const char*>* version_map); | |
1423 | ||
1424 | template | |
1425 | void | |
1426 | Symbol_table::add_from_dynobj<32, false>( | |
1427 | Sized_dynobj<32, false>* dynobj, | |
1428 | const unsigned char* syms, | |
1429 | size_t count, | |
1430 | const char* sym_names, | |
1431 | size_t sym_name_size, | |
1432 | const unsigned char* versym, | |
1433 | size_t versym_size, | |
1434 | const std::vector<const char*>* version_map); | |
1435 | ||
1436 | template | |
1437 | void | |
1438 | Symbol_table::add_from_dynobj<64, true>( | |
1439 | Sized_dynobj<64, true>* dynobj, | |
1440 | const unsigned char* syms, | |
1441 | size_t count, | |
1442 | const char* sym_names, | |
1443 | size_t sym_name_size, | |
1444 | const unsigned char* versym, | |
1445 | size_t versym_size, | |
1446 | const std::vector<const char*>* version_map); | |
1447 | ||
1448 | template | |
1449 | void | |
1450 | Symbol_table::add_from_dynobj<64, false>( | |
1451 | Sized_dynobj<64, false>* dynobj, | |
1452 | const unsigned char* syms, | |
1453 | size_t count, | |
1454 | const char* sym_names, | |
1455 | size_t sym_name_size, | |
1456 | const unsigned char* versym, | |
1457 | size_t versym_size, | |
1458 | const std::vector<const char*>* version_map); | |
1459 | ||
14bfc3f5 | 1460 | } // End namespace gold. |