X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=gold%2Fsymtab.cc;h=2251ea76c7d1f3dc64d1aad428033d712f1064d5;hb=5482377ddc6d4104399a7e2e49518a6e893dca65;hp=8cf7789b82fd7feb19548865e0d5059dcfb062c0;hpb=1564db8db6620560889dd5baeae801623c00a595;p=binutils-gdb.git diff --git a/gold/symtab.cc b/gold/symtab.cc index 8cf7789b82f..2251ea76c7d 100644 --- a/gold/symtab.cc +++ b/gold/symtab.cc @@ -8,6 +8,8 @@ #include #include "object.h" +#include "output.h" +#include "target.h" #include "symtab.h" namespace gold @@ -52,7 +54,7 @@ Sized_symbol::init(const char* name, const char* version, Object* object, // Class Symbol_table. Symbol_table::Symbol_table() - : size_(0), table_(), namepool_(), forwarders_() + : size_(0), offset_(0), table_(), namepool_(), forwarders_() { } @@ -91,6 +93,8 @@ Symbol_table::make_forwarder(Symbol* from, Symbol* to) from->set_forwarder(); } +// Resolve the forwards from FROM, returning the real symbol. + Symbol* Symbol_table::resolve_forwards(Symbol* from) const { @@ -101,6 +105,28 @@ Symbol_table::resolve_forwards(Symbol* from) const return p->second; } +// Look up a symbol by name. + +Symbol* +Symbol_table::lookup(const char* name, const char* version) const +{ + name = this->namepool_.find(name); + if (name == NULL) + return NULL; + if (version != NULL) + { + version = this->namepool_.find(version); + if (version == NULL) + return NULL; + } + + Symbol_table_key key(name, version); + Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); + if (p == this->table_.end()) + return NULL; + return p->second; +} + // Resolve a Symbol with another Symbol. This is only used in the // unusual case where there are references to both an unversioned // symbol and a symbol with a version, and we then discover that that @@ -109,7 +135,8 @@ Symbol_table::resolve_forwards(Symbol* from) const template void -Symbol_table::resolve(Sized_symbol* to, const Sized_symbol* from) +Symbol_table::resolve(Sized_symbol* to, const Sized_symbol* from + ACCEPT_SIZE_ENDIAN) { unsigned char buf[elfcpp::Elf_sizes::sym_size]; elfcpp::Sym_write esym(buf); @@ -174,7 +201,8 @@ Symbol_table::add_from_object(Sized_object* object, if (!ins.second) { // We already have an entry for NAME/VERSION. - ret = this->get_sized_symbol(ins.first->second); + ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second + SELECT_SIZE(size)); assert(ret != NULL); Symbol_table::resolve(ret, sym, object); @@ -190,9 +218,12 @@ Symbol_table::add_from_object(Sized_object* object, { // This is the unfortunate case where we already have // entries for both NAME/VERSION and NAME/NULL. - const Sized_symbol* sym2 = - this->get_sized_symbol(insdef.first->second); - Symbol_table::resolve(ret, sym2); + const Sized_symbol* sym2; + sym2 = this->get_sized_symbol SELECT_SIZE_NAME ( + insdef.first->second + SELECT_SIZE(size)); + Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME ( + ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian)); this->make_forwarder(insdef.first->second, ret); insdef.first->second = ret; } @@ -206,7 +237,8 @@ Symbol_table::add_from_object(Sized_object* object, { // We already have an entry for NAME/NULL. Make // NAME/VERSION point to it. - ret = this->get_sized_symbol(insdef.first->second); + ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second + SELECT_SIZE(size)); Symbol_table::resolve(ret, sym, object); ins.first->second = ret; } @@ -281,7 +313,8 @@ Symbol_table::add_from_object( unsigned int st_name = sym.get_st_name(); if (st_name >= sym_name_size) { - fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"), + fprintf(stderr, + _("%s: %s: bad global symbol name offset %u at %lu\n"), program_name, object->name().c_str(), st_name, static_cast(i)); gold_exit(false); @@ -320,6 +353,143 @@ Symbol_table::add_from_object( } } +// Set the final values for all the symbols. Record the file offset +// OFF. Add their names to POOL. Return the new file offset. + +off_t +Symbol_table::finalize(off_t off, Stringpool* pool) +{ + if (this->size_ == 32) + return this->sized_finalize<32>(off, pool); + else if (this->size_ == 64) + return this->sized_finalize<64>(off, pool); + else + abort(); +} + +// Set the final value for all the symbols. + +template +off_t +Symbol_table::sized_finalize(off_t off, Stringpool* pool) +{ + off = (off + (size >> 3) - 1) & ~ ((size >> 3) - 1); + this->offset_ = off; + + const int sym_size = elfcpp::Elf_sizes::sym_size; + Symbol_table_type::iterator p = this->table_.begin(); + size_t count = 0; + while (p != this->table_.end()) + { + Sized_symbol* sym = static_cast*>(p->second); + + // FIXME: Here we need to decide which symbols should go into + // the output file. + + // FIXME: This is wrong. + if (sym->shnum() >= elfcpp::SHN_LORESERVE) + { + ++p; + continue; + } + + const Object::Map_to_output* mo = + sym->object()->section_output_info(sym->shnum()); + + if (mo->output_section == NULL) + { + // We should be able to erase this symbol from the symbol + // table, but at least with gcc 4.0.2 + // std::unordered_map::erase doesn't appear to return the + // new iterator. + // p = this->table_.erase(p); + ++p; + } + else + { + sym->set_value(sym->value() + + mo->output_section->address() + + mo->offset); + pool->add(sym->name()); + ++p; + ++count; + off += sym_size; + } + } + + this->output_count_ = count; + + return off; +} + +// Write out the global symbols. + +void +Symbol_table::write_globals(const Target* target, const Stringpool* sympool, + Output_file* of) const +{ + if (this->size_ == 32) + { + if (target->is_big_endian()) + this->sized_write_globals<32, true>(target, sympool, of); + else + this->sized_write_globals<32, false>(target, sympool, of); + } + else if (this->size_ == 64) + { + if (target->is_big_endian()) + this->sized_write_globals<64, true>(target, sympool, of); + else + this->sized_write_globals<64, false>(target, sympool, of); + } + else + abort(); +} + +// Write out the global symbols. + +template +void +Symbol_table::sized_write_globals(const Target*, + const Stringpool* sympool, + Output_file* of) const +{ + const int sym_size = elfcpp::Elf_sizes::sym_size; + unsigned char* psyms = of->get_output_view(this->offset_, + this->output_count_ * sym_size); + unsigned char* ps = psyms; + for (Symbol_table_type::const_iterator p = this->table_.begin(); + p != this->table_.end(); + ++p) + { + Sized_symbol* sym = static_cast*>(p->second); + + // FIXME: This repeats sized_finalize(). + + // FIXME: This is wrong. + if (sym->shnum() >= elfcpp::SHN_LORESERVE) + continue; + + const Object::Map_to_output* mo = + sym->object()->section_output_info(sym->shnum()); + + if (mo->output_section == NULL) + continue; + + elfcpp::Sym_write osym(ps); + osym.put_st_name(sympool->get_offset(sym->name())); + osym.put_st_value(sym->value()); + osym.put_st_size(sym->symsize()); + osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); + osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->other())); + osym.put_st_shndx(mo->output_section->shndx()); + + ps += sym_size; + } + + of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms); +} + // Instantiate the templates we need. We could use the configure // script to restrict this to only the ones needed for implemented // targets.