(root)/
binutils-2.41/
gold/
icf.h
       1  // icf.h --  Identical Code Folding
       2  
       3  // Copyright (C) 2009-2023 Free Software Foundation, Inc.
       4  // Written by Sriraman Tallam <tmsriram@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_ICF_H
      24  #define GOLD_ICF_H
      25  
      26  #include <vector>
      27  
      28  #include "elfcpp.h"
      29  #include "symtab.h"
      30  #include "object.h"
      31  
      32  namespace gold
      33  {
      34  
      35  class Object;
      36  class Input_objects;
      37  class Symbol_table;
      38  
      39  class Icf
      40  {
      41   public:
      42    typedef std::vector<Section_id> Sections_reachable_info;
      43    typedef std::vector<Symbol*> Symbol_info;
      44    typedef std::vector<std::pair<long long, long long> > Addend_info;
      45    typedef std::vector<uint64_t> Offset_info;
      46    typedef std::vector<unsigned int> Reloc_addend_size_info;
      47    typedef Unordered_map<Section_id,
      48                          unsigned int,
      49                          Section_id_hash> Uniq_secn_id_map;
      50    typedef Unordered_set<Section_id, Section_id_hash> Secn_fptr_taken_set;
      51  
      52    typedef struct
      53    {
      54      // This stores the section corresponding to the reloc.
      55      Sections_reachable_info section_info;
      56      // This stores the symbol corresponding to the reloc.
      57      Symbol_info symbol_info;
      58      // This stores the symbol value and the addend for a reloc.
      59      Addend_info addend_info;
      60      Offset_info offset_info;
      61      Reloc_addend_size_info reloc_addend_size_info;
      62    } Reloc_info;
      63  
      64    typedef Unordered_map<Section_id, Reloc_info,
      65                          Section_id_hash> Reloc_info_list;
      66  
      67    // A region of some other section that should be considered part of
      68    // a section for ICF purposes. This is used to avoid folding sections
      69    // that have identical text and relocations but different .eh_frame
      70    // information.
      71    typedef struct
      72    {
      73  	Section_id section;
      74  	section_offset_type offset;
      75  	section_size_type length;
      76    } Extra_identity_info;
      77  
      78    typedef std::multimap<Section_id, Extra_identity_info> Extra_identity_list;
      79  
      80    Icf()
      81    : id_section_(), section_id_(), kept_section_id_(),
      82      fptr_section_id_(),
      83      icf_ready_(false),
      84      reloc_info_list_()
      85    { }
      86  
      87    // Returns the kept folded identical section corresponding to
      88    // dup_obj and dup_shndx.
      89    Section_id
      90    get_folded_section(Relobj* dup_obj, unsigned int dup_shndx);
      91  
      92    // Forms groups of identical sections where the first member
      93    // of each group is the kept section during folding.
      94    void
      95    find_identical_sections(const Input_objects* input_objects,
      96                            Symbol_table* symtab);
      97  
      98    // This is set when ICF has been run and the groups of
      99    // identical sections have been formed.
     100    void
     101    icf_ready()
     102    { this->icf_ready_ = true; }
     103  
     104    // Returns true if ICF has been run.
     105    bool
     106    is_icf_ready()
     107    { return this->icf_ready_; }
     108  
     109    // Unfolds the section denoted by OBJ and SHNDX if folded.
     110    void
     111    unfold_section(Relobj* obj, unsigned int shndx);
     112  
     113    // Returns the kept section corresponding to the
     114    // given section.
     115    bool
     116    is_section_folded(Relobj* obj, unsigned int shndx);
     117  
     118    // Given an object and a section index, this returns true if the
     119    // pointer of the function defined in this section is taken.
     120    bool
     121    section_has_function_pointers(Relobj* obj, unsigned int shndx)
     122    {
     123      return (this->fptr_section_id_.find(Section_id(obj, shndx))
     124              != this->fptr_section_id_.end());
     125    }
     126  
     127    // Records that a pointer of the function defined in this section
     128    // is taken.
     129    void
     130    set_section_has_function_pointers(Relobj* obj, unsigned int shndx)
     131    {
     132      this->fptr_section_id_.insert(Section_id(obj, shndx));
     133    }
     134  
     135    // Checks if the section_name should be searched for relocs
     136    // corresponding to taken function pointers.  Ignores eh_frame
     137    // and vtable sections.
     138    inline bool
     139    check_section_for_function_pointers(const std::string& section_name,
     140                                        Target* target)
     141    {
     142      return (parameters->options().icf_safe_folding()
     143  	    && target->can_check_for_function_pointers()
     144  	    && target->section_may_have_icf_unsafe_pointers(
     145  	        section_name.c_str()));
     146    }
     147  
     148    // Returns a map of a section to info (Reloc_info) about its relocations.
     149    Reloc_info_list&
     150    reloc_info_list()
     151    { return this->reloc_info_list_; }
     152  
     153    // Returns a map from section to region of a different section that should
     154    // be considered part of the key section for ICF purposes.
     155    Extra_identity_list&
     156    extra_identity_list()
     157    { return this->extra_identity_list_; }
     158  
     159    // Returns a mapping of each section to a unique integer.
     160    Uniq_secn_id_map&
     161    section_to_int_map()
     162    { return this->section_id_; }
     163  
     164   private:
     165  
     166    bool
     167    add_ehframe_links(Relobj* object, unsigned int ehframe_shndx,
     168  		    Reloc_info& ehframe_relocs);
     169  
     170    // Maps integers to sections.
     171    std::vector<Section_id> id_section_;
     172    // Does the reverse.
     173    Uniq_secn_id_map section_id_;
     174    // Given a section id, this maps it to the id of the kept
     175    // section.  If the id's are the same then this section is
     176    // not folded.
     177    std::vector<unsigned int> kept_section_id_;
     178    // Given a section id, this says if the pointer to this
     179    // function is taken in which case it is dangerous to fold
     180    // this function.
     181    Secn_fptr_taken_set fptr_section_id_;
     182    // Flag to indicate if ICF has been run.
     183    bool icf_ready_;
     184    // This list is populated by gc_process_relocs in gc.h.
     185    Reloc_info_list reloc_info_list_;
     186    // Regions of other sections that should be considered part of
     187    // each section for ICF purposes.
     188    Extra_identity_list extra_identity_list_;
     189  };
     190  
     191  // This function returns true if this section corresponds to a function that
     192  // should be considered by icf as a possible candidate for folding.  Some
     193  // earlier gcc versions, like 4.0.3, put constructors and destructors in
     194  // .gnu.linkonce.t sections and hence should be included too.
     195  // The mechanism used to safely fold functions referenced by .eh_frame
     196  // requires folding .gcc_except_table sections as well; see "Notes regarding
     197  // C++ exception handling" at the top of icf.cc for an explanation why.
     198  inline bool
     199  is_section_foldable_candidate(const std::string& section_name)
     200  {
     201    const char* section_name_cstr = section_name.c_str();
     202    return (is_prefix_of(".text", section_name_cstr)
     203            || is_prefix_of(".gcc_except_table", section_name_cstr)
     204            || is_prefix_of(".gnu.linkonce.t", section_name_cstr));
     205  }
     206  
     207  } // End of namespace gold.
     208  
     209  #endif