Avoid multiple definition errors from linkonce sections.
[deliverable/binutils-gdb.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 #include "gold.h"
4
5 #include <cassert>
6 #include <stdint.h>
7 #include <string>
8 #include <utility>
9
10 #include "object.h"
11 #include "output.h"
12 #include "target.h"
13 #include "symtab.h"
14
15 namespace gold
16 {
17
18 // Class Symbol.
19
20 // Initialize the fields in the base class Symbol.
21
22 template<int size, bool big_endian>
23 void
24 Symbol::init_base(const char* name, const char* version, Object* object,
25 const elfcpp::Sym<size, big_endian>& sym)
26 {
27 this->name_ = name;
28 this->version_ = version;
29 this->object_ = object;
30 this->shnum_ = sym.get_st_shndx(); // FIXME: Handle SHN_XINDEX.
31 this->type_ = sym.get_st_type();
32 this->binding_ = sym.get_st_bind();
33 this->visibility_ = sym.get_st_visibility();
34 this->other_ = sym.get_st_nonvis();
35 this->is_special_ = false;
36 this->is_def_ = false;
37 this->is_forwarder_ = false;
38 this->in_dyn_ = object->is_dynamic();
39 }
40
41 // Initialize the fields in Sized_symbol.
42
43 template<int size>
44 template<bool big_endian>
45 void
46 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
47 const elfcpp::Sym<size, big_endian>& sym)
48 {
49 this->init_base(name, version, object, sym);
50 this->value_ = sym.get_st_value();
51 this->size_ = sym.get_st_size();
52 }
53
54 // Class Symbol_table.
55
56 Symbol_table::Symbol_table()
57 : size_(0), offset_(0), table_(), namepool_(), forwarders_()
58 {
59 }
60
61 Symbol_table::~Symbol_table()
62 {
63 }
64
65 // The hash function. The key is always canonicalized, so we use a
66 // simple combination of the pointers.
67
68 size_t
69 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
70 {
71 return (reinterpret_cast<size_t>(key.first)
72 ^ reinterpret_cast<size_t>(key.second));
73 }
74
75 // The symbol table key equality function. This is only called with
76 // canonicalized name and version strings, so we can use pointer
77 // comparison.
78
79 bool
80 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
81 const Symbol_table_key& k2) const
82 {
83 return k1.first == k2.first && k1.second == k2.second;
84 }
85
86 // Make TO a symbol which forwards to FROM.
87
88 void
89 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
90 {
91 assert(!from->is_forwarder() && !to->is_forwarder());
92 this->forwarders_[from] = to;
93 from->set_forwarder();
94 }
95
96 // Resolve the forwards from FROM, returning the real symbol.
97
98 Symbol*
99 Symbol_table::resolve_forwards(Symbol* from) const
100 {
101 assert(from->is_forwarder());
102 Unordered_map<Symbol*, Symbol*>::const_iterator p =
103 this->forwarders_.find(from);
104 assert(p != this->forwarders_.end());
105 return p->second;
106 }
107
108 // Look up a symbol by name.
109
110 Symbol*
111 Symbol_table::lookup(const char* name, const char* version) const
112 {
113 name = this->namepool_.find(name);
114 if (name == NULL)
115 return NULL;
116 if (version != NULL)
117 {
118 version = this->namepool_.find(version);
119 if (version == NULL)
120 return NULL;
121 }
122
123 Symbol_table_key key(name, version);
124 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
125 if (p == this->table_.end())
126 return NULL;
127 return p->second;
128 }
129
130 // Resolve a Symbol with another Symbol. This is only used in the
131 // unusual case where there are references to both an unversioned
132 // symbol and a symbol with a version, and we then discover that that
133 // version is the default version. Because this is unusual, we do
134 // this the slow way, by converting back to an ELF symbol.
135
136 template<int size, bool big_endian>
137 void
138 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
139 ACCEPT_SIZE_ENDIAN)
140 {
141 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
142 elfcpp::Sym_write<size, big_endian> esym(buf);
143 // We don't bother to set the st_name field.
144 esym.put_st_value(from->value());
145 esym.put_st_size(from->symsize());
146 esym.put_st_info(from->binding(), from->type());
147 esym.put_st_other(from->visibility(), from->other());
148 esym.put_st_shndx(from->shnum());
149 Symbol_table::resolve(to, esym.sym(), from->object());
150 }
151
152 // Add one symbol from OBJECT to the symbol table. NAME is symbol
153 // name and VERSION is the version; both are canonicalized. DEF is
154 // whether this is the default version.
155
156 // If DEF is true, then this is the definition of a default version of
157 // a symbol. That means that any lookup of NAME/NULL and any lookup
158 // of NAME/VERSION should always return the same symbol. This is
159 // obvious for references, but in particular we want to do this for
160 // definitions: overriding NAME/NULL should also override
161 // NAME/VERSION. If we don't do that, it would be very hard to
162 // override functions in a shared library which uses versioning.
163
164 // We implement this by simply making both entries in the hash table
165 // point to the same Symbol structure. That is easy enough if this is
166 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
167 // that we have seen both already, in which case they will both have
168 // independent entries in the symbol table. We can't simply change
169 // the symbol table entry, because we have pointers to the entries
170 // attached to the object files. So we mark the entry attached to the
171 // object file as a forwarder, and record it in the forwarders_ map.
172 // Note that entries in the hash table will never be marked as
173 // forwarders.
174
175 template<int size, bool big_endian>
176 Symbol*
177 Symbol_table::add_from_object(Sized_object<size, big_endian>* object,
178 const char *name,
179 const char *version, bool def,
180 const elfcpp::Sym<size, big_endian>& sym)
181 {
182 Symbol* const snull = NULL;
183 std::pair<typename Symbol_table_type::iterator, bool> ins =
184 this->table_.insert(std::make_pair(std::make_pair(name, version), snull));
185
186 std::pair<typename Symbol_table_type::iterator, bool> insdef =
187 std::make_pair(this->table_.end(), false);
188 if (def)
189 {
190 const char* const vnull = NULL;
191 insdef = this->table_.insert(std::make_pair(std::make_pair(name, vnull),
192 snull));
193 }
194
195 // ins.first: an iterator, which is a pointer to a pair.
196 // ins.first->first: the key (a pair of name and version).
197 // ins.first->second: the value (Symbol*).
198 // ins.second: true if new entry was inserted, false if not.
199
200 Sized_symbol<size>* ret;
201 if (!ins.second)
202 {
203 // We already have an entry for NAME/VERSION.
204 ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second
205 SELECT_SIZE(size));
206 assert(ret != NULL);
207 Symbol_table::resolve(ret, sym, object);
208
209 if (def)
210 {
211 if (insdef.second)
212 {
213 // This is the first time we have seen NAME/NULL. Make
214 // NAME/NULL point to NAME/VERSION.
215 insdef.first->second = ret;
216 }
217 else
218 {
219 // This is the unfortunate case where we already have
220 // entries for both NAME/VERSION and NAME/NULL.
221 const Sized_symbol<size>* sym2;
222 sym2 = this->get_sized_symbol SELECT_SIZE_NAME (
223 insdef.first->second
224 SELECT_SIZE(size));
225 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME (
226 ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
227 this->make_forwarder(insdef.first->second, ret);
228 insdef.first->second = ret;
229 }
230 }
231 }
232 else
233 {
234 // This is the first time we have seen NAME/VERSION.
235 assert(ins.first->second == NULL);
236 if (def && !insdef.second)
237 {
238 // We already have an entry for NAME/NULL. Make
239 // NAME/VERSION point to it.
240 ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second
241 SELECT_SIZE(size));
242 Symbol_table::resolve(ret, sym, object);
243 ins.first->second = ret;
244 }
245 else
246 {
247 Sized_target<size, big_endian>* target = object->sized_target();
248 if (!target->has_make_symbol())
249 ret = new Sized_symbol<size>();
250 else
251 {
252 ret = target->make_symbol();
253 if (ret == NULL)
254 {
255 // This means that we don't want a symbol table
256 // entry after all.
257 if (!def)
258 this->table_.erase(ins.first);
259 else
260 {
261 this->table_.erase(insdef.first);
262 // Inserting insdef invalidated ins.
263 this->table_.erase(std::make_pair(name, version));
264 }
265 return NULL;
266 }
267 }
268
269 ret->init(name, version, object, sym);
270
271 ins.first->second = ret;
272 if (def)
273 {
274 // This is the first time we have seen NAME/NULL. Point
275 // it at the new entry for NAME/VERSION.
276 assert(insdef.second);
277 insdef.first->second = ret;
278 }
279 }
280 }
281
282 return ret;
283 }
284
285 // Add all the symbols in an object to the hash table.
286
287 template<int size, bool big_endian>
288 void
289 Symbol_table::add_from_object(
290 Sized_object<size, big_endian>* object,
291 const elfcpp::Sym<size, big_endian>* syms,
292 size_t count,
293 const char* sym_names,
294 size_t sym_name_size,
295 Symbol** sympointers)
296 {
297 // We take the size from the first object we see.
298 if (this->get_size() == 0)
299 this->set_size(size);
300
301 if (size != this->get_size() || size != object->target()->get_size())
302 {
303 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
304 program_name, object->name().c_str());
305 gold_exit(false);
306 }
307
308 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
309
310 const unsigned char* p = reinterpret_cast<const unsigned char*>(syms);
311 for (size_t i = 0; i < count; ++i, p += sym_size)
312 {
313 elfcpp::Sym<size, big_endian> sym(p);
314 elfcpp::Sym<size, big_endian>* psym = &sym;
315
316 unsigned int st_name = psym->get_st_name();
317 if (st_name >= sym_name_size)
318 {
319 fprintf(stderr,
320 _("%s: %s: bad global symbol name offset %u at %lu\n"),
321 program_name, object->name().c_str(), st_name,
322 static_cast<unsigned long>(i));
323 gold_exit(false);
324 }
325
326 // A symbol defined in a section which we are not including must
327 // be treated as an undefined symbol.
328 unsigned char symbuf[sym_size];
329 elfcpp::Sym<size, big_endian> sym2(symbuf);
330 unsigned int st_shndx = psym->get_st_shndx();
331 if (st_shndx != elfcpp::SHN_UNDEF
332 && st_shndx < elfcpp::SHN_LORESERVE
333 && !object->is_section_included(st_shndx))
334 {
335 memcpy(symbuf, p, sym_size);
336 elfcpp::Sym_write<size, big_endian> sw(symbuf);
337 sw.put_st_shndx(elfcpp::SHN_UNDEF);
338 psym = &sym2;
339 }
340
341 const char* name = sym_names + st_name;
342
343 // In an object file, an '@' in the name separates the symbol
344 // name from the version name. If there are two '@' characters,
345 // this is the default version.
346 const char* ver = strchr(name, '@');
347
348 Symbol* res;
349 if (ver == NULL)
350 {
351 name = this->namepool_.add(name);
352 res = this->add_from_object(object, name, NULL, false, *psym);
353 }
354 else
355 {
356 name = this->namepool_.add(name, ver - name);
357 bool def = false;
358 ++ver;
359 if (*ver == '@')
360 {
361 def = true;
362 ++ver;
363 }
364 ver = this->namepool_.add(ver);
365 res = this->add_from_object(object, name, ver, def, *psym);
366 }
367
368 *sympointers++ = res;
369 }
370 }
371
372 // Set the final values for all the symbols. Record the file offset
373 // OFF. Add their names to POOL. Return the new file offset.
374
375 off_t
376 Symbol_table::finalize(off_t off, Stringpool* pool)
377 {
378 if (this->size_ == 32)
379 return this->sized_finalize<32>(off, pool);
380 else if (this->size_ == 64)
381 return this->sized_finalize<64>(off, pool);
382 else
383 abort();
384 }
385
386 // Set the final value for all the symbols.
387
388 template<int size>
389 off_t
390 Symbol_table::sized_finalize(off_t off, Stringpool* pool)
391 {
392 off = (off + (size >> 3) - 1) & ~ ((size >> 3) - 1);
393 this->offset_ = off;
394
395 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
396 Symbol_table_type::iterator p = this->table_.begin();
397 size_t count = 0;
398 while (p != this->table_.end())
399 {
400 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
401
402 // FIXME: Here we need to decide which symbols should go into
403 // the output file.
404
405 // FIXME: This is wrong.
406 if (sym->shnum() >= elfcpp::SHN_LORESERVE)
407 {
408 ++p;
409 continue;
410 }
411
412 off_t secoff;
413 Output_section* os = sym->object()->output_section(sym->shnum(),
414 &secoff);
415
416 if (os == NULL)
417 {
418 // We should be able to erase this symbol from the symbol
419 // table, but at least with gcc 4.0.2
420 // std::unordered_map::erase doesn't appear to return the
421 // new iterator.
422 // p = this->table_.erase(p);
423 ++p;
424 }
425 else
426 {
427 sym->set_value(sym->value() + os->address() + secoff);
428 pool->add(sym->name());
429 ++p;
430 ++count;
431 off += sym_size;
432 }
433 }
434
435 this->output_count_ = count;
436
437 return off;
438 }
439
440 // Write out the global symbols.
441
442 void
443 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
444 Output_file* of) const
445 {
446 if (this->size_ == 32)
447 {
448 if (target->is_big_endian())
449 this->sized_write_globals<32, true>(target, sympool, of);
450 else
451 this->sized_write_globals<32, false>(target, sympool, of);
452 }
453 else if (this->size_ == 64)
454 {
455 if (target->is_big_endian())
456 this->sized_write_globals<64, true>(target, sympool, of);
457 else
458 this->sized_write_globals<64, false>(target, sympool, of);
459 }
460 else
461 abort();
462 }
463
464 // Write out the global symbols.
465
466 template<int size, bool big_endian>
467 void
468 Symbol_table::sized_write_globals(const Target*,
469 const Stringpool* sympool,
470 Output_file* of) const
471 {
472 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
473 unsigned char* psyms = of->get_output_view(this->offset_,
474 this->output_count_ * sym_size);
475 unsigned char* ps = psyms;
476 for (Symbol_table_type::const_iterator p = this->table_.begin();
477 p != this->table_.end();
478 ++p)
479 {
480 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
481
482 // FIXME: This repeats sized_finalize().
483
484 // FIXME: This is wrong.
485 if (sym->shnum() >= elfcpp::SHN_LORESERVE)
486 continue;
487
488 off_t secoff;
489 Output_section* os = sym->object()->output_section(sym->shnum(),
490 &secoff);
491 if (os == NULL)
492 continue;
493
494 elfcpp::Sym_write<size, big_endian> osym(ps);
495 osym.put_st_name(sympool->get_offset(sym->name()));
496 osym.put_st_value(sym->value());
497 osym.put_st_size(sym->symsize());
498 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
499 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->other()));
500 osym.put_st_shndx(os->shndx());
501
502 ps += sym_size;
503 }
504
505 of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
506 }
507
508 // Instantiate the templates we need. We could use the configure
509 // script to restrict this to only the ones needed for implemented
510 // targets.
511
512 template
513 void
514 Symbol_table::add_from_object<32, true>(
515 Sized_object<32, true>* object,
516 const elfcpp::Sym<32, true>* syms,
517 size_t count,
518 const char* sym_names,
519 size_t sym_name_size,
520 Symbol** sympointers);
521
522 template
523 void
524 Symbol_table::add_from_object<32, false>(
525 Sized_object<32, false>* object,
526 const elfcpp::Sym<32, false>* syms,
527 size_t count,
528 const char* sym_names,
529 size_t sym_name_size,
530 Symbol** sympointers);
531
532 template
533 void
534 Symbol_table::add_from_object<64, true>(
535 Sized_object<64, true>* object,
536 const elfcpp::Sym<64, true>* syms,
537 size_t count,
538 const char* sym_names,
539 size_t sym_name_size,
540 Symbol** sympointers);
541
542 template
543 void
544 Symbol_table::add_from_object<64, false>(
545 Sized_object<64, false>* object,
546 const elfcpp::Sym<64, false>* syms,
547 size_t count,
548 const char* sym_names,
549 size_t sym_name_size,
550 Symbol** sympointers);
551
552 } // End namespace gold.
This page took 0.042759 seconds and 5 git commands to generate.