1  // dynobj.h -- dynamic object support for gold   -*- C++ -*-
       2  
       3  // Copyright (C) 2006-2023 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  
      23  #ifndef GOLD_DYNOBJ_H
      24  #define GOLD_DYNOBJ_H
      25  
      26  #include <vector>
      27  
      28  #include "stringpool.h"
      29  #include "object.h"
      30  
      31  namespace gold
      32  {
      33  
      34  class Version_script_info;
      35  
      36  // A dynamic object (ET_DYN).  This is an abstract base class itself.
      37  // The implementations is the template class Sized_dynobj.
      38  
      39  class Dynobj : public Object
      40  {
      41   public:
      42    // We keep a list of all the DT_NEEDED entries we find.
      43    typedef std::vector<std::string> Needed;
      44  
      45    Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
      46  
      47    // Return the name to use in a DT_NEEDED entry for this object.
      48    const char*
      49    soname() const
      50    { return this->soname_.c_str(); }
      51  
      52    // Return the list of DT_NEEDED strings.
      53    const Needed&
      54    needed() const
      55    { return this->needed_; }
      56  
      57    // Return whether this dynamic object has any DT_NEEDED entries
      58    // which were not seen during the link.
      59    bool
      60    has_unknown_needed_entries() const
      61    {
      62      gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
      63      return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
      64    }
      65  
      66    // Set whether this dynamic object has any DT_NEEDED entries which
      67    // were not seen during the link.
      68    void
      69    set_has_unknown_needed_entries(bool set)
      70    {
      71      gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
      72      this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
      73    }
      74  
      75    // Return the word size of the object file.
      76    int
      77    elfsize() const
      78    { gold_unreachable(); }
      79  
      80    // Return TRUE if this is a big-endian object file.
      81    bool
      82    is_big_endian() const
      83    { gold_unreachable(); }
      84  
      85    // Compute the ELF hash code for a string.
      86    static uint32_t
      87    elf_hash(const char*);
      88  
      89    // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
      90    // DYNSYMS is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the
      91    // number of local dynamic symbols, which is the index of the first
      92    // dynamic gobal symbol.
      93    static void
      94    create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
      95  			unsigned int local_dynsym_count,
      96  			unsigned char** pphash,
      97  			unsigned int* phashlen);
      98  
      99    // Create a GNU hash table, setting *PPHASH and *PHASHLEN.  DYNSYMS
     100    // is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the number
     101    // of local dynamic symbols, which is the index of the first dynamic
     102    // gobal symbol.
     103    static void
     104    create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
     105  			unsigned int local_dynsym_count,
     106  			unsigned char** pphash, unsigned int* phashlen);
     107  
     108   protected:
     109    // Return a pointer to this object.
     110    virtual Dynobj*
     111    do_dynobj()
     112    { return this; }
     113  
     114    // Set the DT_SONAME string.
     115    void
     116    set_soname_string(const char* s)
     117    { this->soname_.assign(s); }
     118  
     119    // Add an entry to the list of DT_NEEDED strings.
     120    void
     121    add_needed(const char* s)
     122    { this->needed_.push_back(std::string(s)); }
     123  
     124   private:
     125    // Compute the GNU hash code for a string.
     126    static uint32_t
     127    gnu_hash(const char*);
     128  
     129    // Compute the number of hash buckets to use.
     130    static unsigned int
     131    compute_bucket_count(const std::vector<uint32_t>& hashcodes,
     132  		       bool for_gnu_hash_table);
     133  
     134    // Sized version of create_elf_hash_table.
     135    template<int size, bool big_endian>
     136    static void
     137    sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
     138  			      const std::vector<uint32_t>& chain,
     139  			      unsigned char* phash,
     140  			      unsigned int hashlen);
     141  
     142    // Sized version of create_gnu_hash_table.
     143    template<int size, bool big_endian>
     144    static void
     145    sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
     146  			      const std::vector<uint32_t>& dynsym_hashvals,
     147  			      unsigned int unhashed_dynsym_count,
     148  			      unsigned char** pphash,
     149  			      unsigned int* phashlen);
     150  
     151    // Values for the has_unknown_needed_entries_ field.
     152    enum Unknown_needed
     153    {
     154      UNKNOWN_NEEDED_UNSET,
     155      UNKNOWN_NEEDED_TRUE,
     156      UNKNOWN_NEEDED_FALSE
     157    };
     158  
     159    // The DT_SONAME name, if any.
     160    std::string soname_;
     161    // The list of DT_NEEDED entries.
     162    Needed needed_;
     163    // Whether this dynamic object has any DT_NEEDED entries not seen
     164    // during the link.
     165    Unknown_needed unknown_needed_;
     166  };
     167  
     168  // A dynamic object, size and endian specific version.
     169  
     170  template<int size, bool big_endian>
     171  class Sized_dynobj : public Dynobj
     172  {
     173   public:
     174    typedef typename Sized_relobj_file<size, big_endian>::Symbols Symbols;
     175  
     176    Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
     177  	       const typename elfcpp::Ehdr<size, big_endian>&);
     178  
     179    // Set up the object file based on TARGET.
     180    void
     181    setup();
     182  
     183    // Read the symbols.
     184    void
     185    do_read_symbols(Read_symbols_data*);
     186  
     187    // Lay out the input sections.
     188    void
     189    do_layout(Symbol_table*, Layout*, Read_symbols_data*);
     190  
     191    // Add the symbols to the symbol table.
     192    void
     193    do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
     194  
     195    Archive::Should_include
     196    do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
     197                             std::string* why);
     198  
     199    // Iterate over global symbols, calling a visitor class V for each.
     200    void
     201    do_for_all_global_symbols(Read_symbols_data* sd,
     202  			    Library_base::Symbol_visitor_base* v);
     203  
     204    // Iterate over local symbols, calling a visitor class V for each GOT offset
     205    // associated with a local symbol.
     206    void
     207    do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
     208  
     209    // Get the size of a section.
     210    uint64_t
     211    do_section_size(unsigned int shndx)
     212    { return this->elf_file_.section_size(shndx); }
     213  
     214    // Get the name of a section.
     215    std::string
     216    do_section_name(unsigned int shndx) const
     217    { return this->elf_file_.section_name(shndx); }
     218  
     219    // Return a view of the contents of a section.  Set *PLEN to the
     220    // size.
     221    const unsigned char*
     222    do_section_contents(unsigned int shndx, section_size_type* plen,
     223  		      bool cache)
     224    {
     225      Location loc(this->elf_file_.section_contents(shndx));
     226      *plen = convert_to_section_size_type(loc.data_size);
     227      if (*plen == 0)
     228        {
     229  	static const unsigned char empty[1] = { '\0' };
     230  	return empty;
     231        }
     232      return this->get_view(loc.file_offset, *plen, true, cache);
     233    }
     234  
     235    // Return section flags.
     236    uint64_t
     237    do_section_flags(unsigned int shndx)
     238    { return this->elf_file_.section_flags(shndx); }
     239  
     240    // Not used for dynobj.
     241    uint64_t
     242    do_section_entsize(unsigned int )
     243    { gold_unreachable(); }
     244  
     245    // Return section address.
     246    uint64_t
     247    do_section_address(unsigned int shndx)
     248    { return this->elf_file_.section_addr(shndx); }
     249  
     250    // Return section type.
     251    unsigned int
     252    do_section_type(unsigned int shndx)
     253    { return this->elf_file_.section_type(shndx); }
     254  
     255    // Return the section link field.
     256    unsigned int
     257    do_section_link(unsigned int shndx)
     258    { return this->elf_file_.section_link(shndx); }
     259  
     260    // Return the section link field.
     261    unsigned int
     262    do_section_info(unsigned int shndx)
     263    { return this->elf_file_.section_info(shndx); }
     264  
     265    // Return the section alignment.
     266    uint64_t
     267    do_section_addralign(unsigned int shndx)
     268    { return this->elf_file_.section_addralign(shndx); }
     269  
     270    // Return the Xindex structure to use.
     271    Xindex*
     272    do_initialize_xindex();
     273  
     274    // Get symbol counts.
     275    void
     276    do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
     277  
     278    // Get the global symbols.
     279    const Symbols*
     280    do_get_global_symbols() const
     281    { return this->symbols_; }
     282  
     283   protected:
     284    // Read the symbols.  This is common code for all target-specific
     285    // overrides of do_read_symbols().
     286    void
     287    base_read_symbols(Read_symbols_data*);
     288  
     289   private:
     290    // For convenience.
     291    typedef Sized_dynobj<size, big_endian> This;
     292    static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
     293    static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
     294    static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
     295    typedef elfcpp::Shdr<size, big_endian> Shdr;
     296    typedef elfcpp::Dyn<size, big_endian> Dyn;
     297  
     298    // Adjust a section index if necessary.
     299    unsigned int
     300    adjust_shndx(unsigned int shndx)
     301    {
     302      if (shndx >= elfcpp::SHN_LORESERVE)
     303        shndx += this->elf_file_.large_shndx_offset();
     304      return shndx;
     305    }
     306  
     307    // Find the dynamic symbol table and the version sections, given the
     308    // section headers.
     309    void
     310    find_dynsym_sections(const unsigned char* pshdrs,
     311  		       unsigned int* pversym_shndx,
     312  		       unsigned int* pverdef_shndx,
     313  		       unsigned int* pverneed_shndx,
     314  		       unsigned int* pdynamic_shndx);
     315  
     316    // Read the dynamic symbol section SHNDX.
     317    void
     318    read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
     319  		      elfcpp::SHT type, unsigned int link,
     320  		      File_view** view, section_size_type* view_size,
     321  		      unsigned int* view_info);
     322  
     323    // Read the dynamic tags.
     324    void
     325    read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
     326  	       unsigned int strtab_shndx, const unsigned char* strtabu,
     327  	       off_t strtab_size);
     328  
     329    // Mapping from version number to version name.
     330    typedef std::vector<const char*> Version_map;
     331  
     332    // Create the version map.
     333    void
     334    make_version_map(Read_symbols_data* sd, Version_map*) const;
     335  
     336    // Add version definitions to the version map.
     337    void
     338    make_verdef_map(Read_symbols_data* sd, Version_map*) const;
     339  
     340    // Add version references to the version map.
     341    void
     342    make_verneed_map(Read_symbols_data* sd, Version_map*) const;
     343  
     344    // Add an entry to the version map.
     345    void
     346    set_version_map(Version_map*, unsigned int ndx, const char* name) const;
     347  
     348    // General access to the ELF file.
     349    elfcpp::Elf_file<size, big_endian, Object> elf_file_;
     350    // The section index of the dynamic symbol table.
     351    unsigned int dynsym_shndx_;
     352    // The entries in the symbol table for the symbols.  We only keep
     353    // this if we need it to print symbol information.
     354    Symbols* symbols_;
     355    // Number of defined symbols.
     356    size_t defined_count_;
     357  };
     358  
     359  // A base class for Verdef and Verneed_version which just handles the
     360  // version index which will be stored in the SHT_GNU_versym section.
     361  
     362  class Version_base
     363  {
     364   public:
     365    Version_base()
     366      : index_(-1U)
     367    { }
     368  
     369    virtual
     370    ~Version_base()
     371    { }
     372  
     373    // Return the version index.
     374    unsigned int
     375    index() const
     376    {
     377      gold_assert(this->index_ != -1U);
     378      return this->index_;
     379    }
     380  
     381    // Set the version index.
     382    void
     383    set_index(unsigned int index)
     384    {
     385      gold_assert(this->index_ == -1U);
     386      this->index_ = index;
     387    }
     388  
     389    // Clear the weak flag in a version definition.
     390    virtual void
     391    clear_weak() = 0;
     392  
     393   private:
     394    Version_base(const Version_base&);
     395    Version_base& operator=(const Version_base&);
     396  
     397    // The index of the version definition or reference.
     398    unsigned int index_;
     399  };
     400  
     401  // This class handles a version being defined in the file we are
     402  // generating.
     403  
     404  class Verdef : public Version_base
     405  {
     406   public:
     407    Verdef(const char* name, const std::vector<std::string>& deps,
     408           bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
     409      : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
     410        is_info_(is_info), is_symbol_created_(is_symbol_created)
     411    { }
     412  
     413    // Return the version name.
     414    const char*
     415    name() const
     416    { return this->name_; }
     417  
     418    // Return the number of dependencies.
     419    unsigned int
     420    count_dependencies() const
     421    { return this->deps_.size(); }
     422  
     423    // Add a dependency to this version.  The NAME should be
     424    // canonicalized in the dynamic Stringpool.
     425    void
     426    add_dependency(const char* name)
     427    { this->deps_.push_back(name); }
     428  
     429    // Return whether this definition is weak.
     430    bool
     431    is_weak() const
     432    { return this->is_weak_; }
     433  
     434    // Clear the weak flag.
     435    void
     436    clear_weak()
     437    { this->is_weak_ = false; }
     438  
     439    // Return whether this definition is informational.
     440    bool
     441    is_info() const
     442    { return this->is_info_; }
     443  
     444    // Return whether a version symbol has been created for this
     445    // definition.
     446    bool
     447    is_symbol_created() const
     448    { return this->is_symbol_created_; }
     449  
     450    // Write contents to buffer.
     451    template<int size, bool big_endian>
     452    unsigned char*
     453    write(const Stringpool*, bool is_last, unsigned char*) const;
     454  
     455   private:
     456    Verdef(const Verdef&);
     457    Verdef& operator=(const Verdef&);
     458  
     459    // The type of the list of version dependencies.  Each dependency
     460    // should be canonicalized in the dynamic Stringpool.
     461    typedef std::vector<std::string> Deps;
     462  
     463    // The name of this version.  This should be canonicalized in the
     464    // dynamic Stringpool.
     465    const char* name_;
     466    // A list of other versions which this version depends upon.
     467    Deps deps_;
     468    // Whether this is the base version.
     469    bool is_base_;
     470    // Whether this version is weak.
     471    bool is_weak_;
     472    // Whether this version is informational.
     473    bool is_info_;
     474    // Whether a version symbol has been created.
     475    bool is_symbol_created_;
     476  };
     477  
     478  // A referened version.  This will be associated with a filename by
     479  // Verneed.
     480  
     481  class Verneed_version : public Version_base
     482  {
     483   public:
     484    Verneed_version(const char* version)
     485      : version_(version)
     486    { }
     487  
     488    // Return the version name.
     489    const char*
     490    version() const
     491    { return this->version_; }
     492  
     493    // Clear the weak flag.  This is invalid for a reference.
     494    void
     495    clear_weak()
     496    { gold_unreachable(); }
     497  
     498   private:
     499    Verneed_version(const Verneed_version&);
     500    Verneed_version& operator=(const Verneed_version&);
     501  
     502    const char* version_;
     503  };
     504  
     505  // Version references in a single dynamic object.
     506  
     507  class Verneed
     508  {
     509   public:
     510    Verneed(const char* filename)
     511      : filename_(filename), need_versions_()
     512    { }
     513  
     514    ~Verneed();
     515  
     516    // Return the file name.
     517    const char*
     518    filename() const
     519    { return this->filename_; }
     520  
     521    // Return the number of versions.
     522    unsigned int
     523    count_versions() const
     524    { return this->need_versions_.size(); }
     525  
     526    // Add a version name.  The name should be canonicalized in the
     527    // dynamic Stringpool.  If the name is already present, this does
     528    // nothing.
     529    Verneed_version*
     530    add_name(const char* name);
     531  
     532    // Set the version indexes, starting at INDEX.  Return the updated
     533    // INDEX.
     534    unsigned int
     535    finalize(unsigned int index);
     536  
     537    // Write contents to buffer.
     538    template<int size, bool big_endian>
     539    unsigned char*
     540    write(const Stringpool*, bool is_last, unsigned char*) const;
     541  
     542   private:
     543    Verneed(const Verneed&);
     544    Verneed& operator=(const Verneed&);
     545  
     546    // The type of the list of version names.  Each name should be
     547    // canonicalized in the dynamic Stringpool.
     548    typedef std::vector<Verneed_version*> Need_versions;
     549  
     550    // The filename of the dynamic object.  This should be
     551    // canonicalized in the dynamic Stringpool.
     552    const char* filename_;
     553    // The list of version names.
     554    Need_versions need_versions_;
     555  };
     556  
     557  // This class handles version definitions and references which go into
     558  // the output file.
     559  
     560  class Versions
     561  {
     562   public:
     563    Versions(const Version_script_info&, Stringpool*);
     564  
     565    ~Versions();
     566  
     567    // SYM is going into the dynamic symbol table and has a version.
     568    // Record the appropriate version information.
     569    void
     570    record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
     571  
     572    // Set the version indexes.  DYNSYM_INDEX is the index we should use
     573    // for the next dynamic symbol.  We add new dynamic symbols to SYMS
     574    // and return an updated DYNSYM_INDEX.
     575    unsigned int
     576    finalize(Symbol_table* symtab, unsigned int dynsym_index,
     577  	   std::vector<Symbol*>* syms);
     578  
     579    // Return whether there are any version definitions.
     580    bool
     581    any_defs() const
     582    { return !this->defs_.empty(); }
     583  
     584    // Return whether there are any version references.
     585    bool
     586    any_needs() const
     587    { return !this->needs_.empty(); }
     588  
     589    // Build an allocated buffer holding the contents of the symbol
     590    // version section (.gnu.version).
     591    template<int size, bool big_endian>
     592    void
     593    symbol_section_contents(const Symbol_table*, const Stringpool*,
     594  			  unsigned int local_symcount,
     595  			  const std::vector<Symbol*>& syms,
     596  			  unsigned char**, unsigned int*) const;
     597  
     598    // Build an allocated buffer holding the contents of the version
     599    // definition section (.gnu.version_d).
     600    template<int size, bool big_endian>
     601    void
     602    def_section_contents(const Stringpool*, unsigned char**,
     603  		       unsigned int* psize, unsigned int* pentries) const;
     604  
     605    // Build an allocated buffer holding the contents of the version
     606    // reference section (.gnu.version_r).
     607    template<int size, bool big_endian>
     608    void
     609    need_section_contents(const Stringpool*, unsigned char**,
     610  			unsigned int* psize, unsigned int* pentries) const;
     611  
     612    const Version_script_info&
     613    version_script() const
     614    { return this->version_script_; }
     615  
     616   private:
     617    Versions(const Versions&);
     618    Versions& operator=(const Versions&);
     619  
     620    // The type of the list of version definitions.
     621    typedef std::vector<Verdef*> Defs;
     622  
     623    // The type of the list of version references.
     624    typedef std::vector<Verneed*> Needs;
     625  
     626    // Handle a symbol SYM defined with version VERSION.
     627    void
     628    add_def(Stringpool*, const Symbol* sym, const char* version,
     629  	  Stringpool::Key);
     630  
     631    // Add a reference to version NAME in file FILENAME.
     632    void
     633    add_need(Stringpool*, const char* filename, const char* name,
     634  	   Stringpool::Key);
     635  
     636    // Get the dynamic object to use for SYM.
     637    Dynobj*
     638    get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
     639  
     640    // Return the version index to use for SYM.
     641    unsigned int
     642    version_index(const Symbol_table*, const Stringpool*,
     643  		const Symbol* sym) const;
     644  
     645    // Define the base version of a shared library.
     646    void
     647    define_base_version(Stringpool* dynpool);
     648  
     649    // We keep a hash table mapping canonicalized name/version pairs to
     650    // a version base.
     651    typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
     652  
     653    struct Version_table_hash
     654    {
     655      size_t
     656      operator()(const Key& k) const
     657      { return k.first + k.second; }
     658    };
     659  
     660    struct Version_table_eq
     661    {
     662      bool
     663      operator()(const Key& k1, const Key& k2) const
     664      { return k1.first == k2.first && k1.second == k2.second; }
     665    };
     666  
     667    typedef Unordered_map<Key, Version_base*, Version_table_hash,
     668  			Version_table_eq> Version_table;
     669  
     670    // The version definitions.
     671    Defs defs_;
     672    // The version references.
     673    Needs needs_;
     674    // The mapping from a canonicalized version/filename pair to a
     675    // version index.  The filename may be NULL.
     676    Version_table version_table_;
     677    // Whether the version indexes have been set.
     678    bool is_finalized_;
     679    // Contents of --version-script, if passed, or NULL.
     680    const Version_script_info& version_script_;
     681    // Whether we need to insert a base version.  This is only used for
     682    // shared libraries and is cleared when the base version is defined.
     683    bool needs_base_version_;
     684  };
     685  
     686  } // End namespace gold.
     687  
     688  #endif // !defined(GOLD_DYNOBJ_H)