binutils-gdb/gold/icf.h

179 lines
5.6 KiB
C
Raw Normal View History

2009-08-06 04:51:56 +08:00
// icf.h -- Identical Code Folding
// Copyright 2009, 2010 Free Software Foundation, Inc.
2009-08-06 04:51:56 +08:00
// Written by Sriraman Tallam <tmsriram@google.com>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#ifndef GOLD_ICF_H
#define GOLD_ICF_H
#include <vector>
#include "elfcpp.h"
#include "symtab.h"
2010-01-20 Doug Kwan <dougkwan@google.com> * arm.cc (Target_arm::Arm_input_section_map): Change key type from Input_section_specifier to Section_id. (Target_arm::new_arm_input_section: Adjust code for change of key type. (Target_arm::find_arm_input_section): Ditto. * gc.h (object.h): Include for Section_id nand Section_id_hash. (Section_id): Remove. (Garbage_collection::Section_id_hash): Remove. * icf.h (object.h): Include for Section_id nand Section_id_hash. (Section_id): Remove. (Icf::Section_id_hash): Remove. * object.h (Section_id, Const_section_id, Section_id_hash, Const_section_id_hash): New type definitions. * output.cc (Output_section::add_relaxed_input_section): Change to use Const_section_id instead of Input_section_specifier as key type. (Output_section::add_merge_input_section): Ditto. (Output_section::build_relaxation_map): Change to use Section_id instead of Input_section_specifier as key type. (Output_section::convert_input_sections_in_list_to_relaxed_sections): Ditto. (Output_section::convert_input_sections_to_relaxed_sections): Change to use Const_section_id instead of Input_section_specifier as key type. (Output_section::find_merge_section): Ditto. (Output_section::find_relaxed_input_section): Ditto. * output.h (Input_section_specifier): Remove class. (Output_section::Output_section_data_by_input_section_map): Change key type to Const_section_id. (Output_section::Output_relaxed_input_section_by_input_section_map): Ditto. (Output_section::Relaxation_map): Change key type to Section_id.
2010-01-21 01:29:52 +08:00
#include "object.h"
2009-08-06 04:51:56 +08:00
namespace gold
{
class Object;
class Input_objects;
class Symbol_table;
class Icf
{
public:
typedef std::vector<Section_id> Sections_reachable_info;
2009-08-06 04:51:56 +08:00
typedef std::vector<Symbol*> Symbol_info;
typedef std::vector<std::pair<long long, long long> > Addend_info;
typedef std::vector<uint64_t> Offset_info;
2009-08-06 04:51:56 +08:00
typedef Unordered_map<Section_id,
unsigned int,
Section_id_hash> Uniq_secn_id_map;
2010-02-12 Sriraman Tallam <tmsriram@google.com> * arm.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * sparc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * powerpc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * i386.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * x86_64.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. (Scan::possible_function_pointer_reloc): New function. (Target_x86_64::can_check_for_function_pointers): New function. * gc.h (gc_process_relocs): Scan relocation types to determine if function pointers were taken for targets that support it. * icf.cc (Icf::find_identical_sections): Include functions for folding in safe ICF whose pointer is not taken. * icf.h (Secn_fptr_taken_set): New typedef. (fptr_section_id_): New member. (section_has_function_pointers): New function. (set_section_has_function_pointers): New function. (check_section_for_function_pointers): New function. * options.h: Fix comment for safe ICF option. * target.h (can_check_for_function_pointers): New function. * testsuite/Makefile.am: Add icf_safe_so_test test case. Modify icf_safe_test for X86-64. * testsuite/Makefile.in: Regenerate. * testsuite/icf_safe_so_test.cc: New file. * testsuite/icf_safe_so_test.sh: New file. * testsuite/icf_safe_test.cc (kept_func_3): New function. (main): Change to take pointer to function kept_func_3. * testsuite/icf_safe_test.sh (arch_specific_safe_fold): Check if safe folding is done correctly for X86-64.
2010-02-13 10:04:21 +08:00
typedef Unordered_set<Section_id, Section_id_hash> Secn_fptr_taken_set;
2009-08-06 04:51:56 +08:00
typedef struct
{
// This stores the section corresponding to the reloc.
Sections_reachable_info section_info;
// This stores the symbol corresponding to the reloc.
Symbol_info symbol_info;
// This stores the symbol value and the addend for a reloc.
Addend_info addend_info;
Offset_info offset_info;
} Reloc_info;
typedef Unordered_map<Section_id, Reloc_info,
Section_id_hash> Reloc_info_list;
2009-08-06 04:51:56 +08:00
Icf()
: id_section_(), section_id_(), kept_section_id_(),
2010-02-12 Sriraman Tallam <tmsriram@google.com> * arm.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * sparc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * powerpc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * i386.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * x86_64.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. (Scan::possible_function_pointer_reloc): New function. (Target_x86_64::can_check_for_function_pointers): New function. * gc.h (gc_process_relocs): Scan relocation types to determine if function pointers were taken for targets that support it. * icf.cc (Icf::find_identical_sections): Include functions for folding in safe ICF whose pointer is not taken. * icf.h (Secn_fptr_taken_set): New typedef. (fptr_section_id_): New member. (section_has_function_pointers): New function. (set_section_has_function_pointers): New function. (check_section_for_function_pointers): New function. * options.h: Fix comment for safe ICF option. * target.h (can_check_for_function_pointers): New function. * testsuite/Makefile.am: Add icf_safe_so_test test case. Modify icf_safe_test for X86-64. * testsuite/Makefile.in: Regenerate. * testsuite/icf_safe_so_test.cc: New file. * testsuite/icf_safe_so_test.sh: New file. * testsuite/icf_safe_test.cc (kept_func_3): New function. (main): Change to take pointer to function kept_func_3. * testsuite/icf_safe_test.sh (arch_specific_safe_fold): Check if safe folding is done correctly for X86-64.
2010-02-13 10:04:21 +08:00
fptr_section_id_(),
2009-08-06 04:51:56 +08:00
num_tracked_relocs(NULL), icf_ready_(false),
reloc_info_list_()
2009-08-06 04:51:56 +08:00
{ }
// Returns the kept folded identical section corresponding to
// dup_obj and dup_shndx.
Section_id
get_folded_section(Object* dup_obj, unsigned int dup_shndx);
// Forms groups of identical sections where the first member
// of each group is the kept section during folding.
void
find_identical_sections(const Input_objects* input_objects,
Symbol_table* symtab);
// This is set when ICF has been run and the groups of
// identical sections have been formed.
void
icf_ready()
{ this->icf_ready_ = true; }
// Returns true if ICF has been run.
bool
is_icf_ready()
{ return this->icf_ready_; }
// Unfolds the section denoted by OBJ and SHNDX if folded.
void
unfold_section(Object* obj, unsigned int shndx);
2009-08-06 04:51:56 +08:00
// Returns the kept section corresponding to the
// given section.
bool
is_section_folded(Object* obj, unsigned int shndx);
2010-02-12 Sriraman Tallam <tmsriram@google.com> * arm.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * sparc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * powerpc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * i386.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * x86_64.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. (Scan::possible_function_pointer_reloc): New function. (Target_x86_64::can_check_for_function_pointers): New function. * gc.h (gc_process_relocs): Scan relocation types to determine if function pointers were taken for targets that support it. * icf.cc (Icf::find_identical_sections): Include functions for folding in safe ICF whose pointer is not taken. * icf.h (Secn_fptr_taken_set): New typedef. (fptr_section_id_): New member. (section_has_function_pointers): New function. (set_section_has_function_pointers): New function. (check_section_for_function_pointers): New function. * options.h: Fix comment for safe ICF option. * target.h (can_check_for_function_pointers): New function. * testsuite/Makefile.am: Add icf_safe_so_test test case. Modify icf_safe_test for X86-64. * testsuite/Makefile.in: Regenerate. * testsuite/icf_safe_so_test.cc: New file. * testsuite/icf_safe_so_test.sh: New file. * testsuite/icf_safe_test.cc (kept_func_3): New function. (main): Change to take pointer to function kept_func_3. * testsuite/icf_safe_test.sh (arch_specific_safe_fold): Check if safe folding is done correctly for X86-64.
2010-02-13 10:04:21 +08:00
// Given an object and a section index, this returns true if the
// pointer of the function defined in this section is taken.
bool
section_has_function_pointers(Object *obj, unsigned int shndx)
{
return (this->fptr_section_id_.find(Section_id(obj, shndx))
!= this->fptr_section_id_.end());
}
// Records that a pointer of the function defined in this section
// is taken.
void
set_section_has_function_pointers(Object *obj, unsigned int shndx)
{
this->fptr_section_id_.insert(Section_id(obj, shndx));
}
// Checks if the section_name should be searched for relocs
// corresponding to taken function pointers. Ignores eh_frame
// and vtable sections.
inline bool
check_section_for_function_pointers(std::string section_name,
Target* target)
{
return (parameters->options().icf_safe_folding()
&& target->can_check_for_function_pointers()
&& !is_prefix_of(".rodata._ZTV", section_name.c_str())
&& !is_prefix_of(".data.rel.ro._ZTV", section_name.c_str())
2010-02-12 Sriraman Tallam <tmsriram@google.com> * arm.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * sparc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * powerpc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * i386.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * x86_64.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. (Scan::possible_function_pointer_reloc): New function. (Target_x86_64::can_check_for_function_pointers): New function. * gc.h (gc_process_relocs): Scan relocation types to determine if function pointers were taken for targets that support it. * icf.cc (Icf::find_identical_sections): Include functions for folding in safe ICF whose pointer is not taken. * icf.h (Secn_fptr_taken_set): New typedef. (fptr_section_id_): New member. (section_has_function_pointers): New function. (set_section_has_function_pointers): New function. (check_section_for_function_pointers): New function. * options.h: Fix comment for safe ICF option. * target.h (can_check_for_function_pointers): New function. * testsuite/Makefile.am: Add icf_safe_so_test test case. Modify icf_safe_test for X86-64. * testsuite/Makefile.in: Regenerate. * testsuite/icf_safe_so_test.cc: New file. * testsuite/icf_safe_so_test.sh: New file. * testsuite/icf_safe_test.cc (kept_func_3): New function. (main): Change to take pointer to function kept_func_3. * testsuite/icf_safe_test.sh (arch_specific_safe_fold): Check if safe folding is done correctly for X86-64.
2010-02-13 10:04:21 +08:00
&& !is_prefix_of(".eh_frame", section_name.c_str()));
}
// Returns a map of a section to info (Reloc_info) about its relocations.
Reloc_info_list&
reloc_info_list()
{ return this->reloc_info_list_; }
2009-08-06 04:51:56 +08:00
// Returns a mapping of each section to a unique integer.
Uniq_secn_id_map&
section_to_int_map()
{ return this->section_id_; }
private:
// Maps integers to sections.
std::vector<Section_id> id_section_;
// Does the reverse.
Uniq_secn_id_map section_id_;
// Given a section id, this maps it to the id of the kept
// section. If the id's are the same then this section is
// not folded.
std::vector<unsigned int> kept_section_id_;
2010-02-12 Sriraman Tallam <tmsriram@google.com> * arm.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * sparc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * powerpc.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * i386.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. * x86_64.cc (Scan::local_reloc_may_be_function_pointer): New function. (Scan::global_reloc_may_be_function_pointer): New function. (Scan::possible_function_pointer_reloc): New function. (Target_x86_64::can_check_for_function_pointers): New function. * gc.h (gc_process_relocs): Scan relocation types to determine if function pointers were taken for targets that support it. * icf.cc (Icf::find_identical_sections): Include functions for folding in safe ICF whose pointer is not taken. * icf.h (Secn_fptr_taken_set): New typedef. (fptr_section_id_): New member. (section_has_function_pointers): New function. (set_section_has_function_pointers): New function. (check_section_for_function_pointers): New function. * options.h: Fix comment for safe ICF option. * target.h (can_check_for_function_pointers): New function. * testsuite/Makefile.am: Add icf_safe_so_test test case. Modify icf_safe_test for X86-64. * testsuite/Makefile.in: Regenerate. * testsuite/icf_safe_so_test.cc: New file. * testsuite/icf_safe_so_test.sh: New file. * testsuite/icf_safe_test.cc (kept_func_3): New function. (main): Change to take pointer to function kept_func_3. * testsuite/icf_safe_test.sh (arch_specific_safe_fold): Check if safe folding is done correctly for X86-64.
2010-02-13 10:04:21 +08:00
// Given a section id, this says if the pointer to this
// function is taken in which case it is dangerous to fold
// this function.
Secn_fptr_taken_set fptr_section_id_;
2009-08-06 04:51:56 +08:00
unsigned int* num_tracked_relocs;
// Flag to indicate if ICF has been run.
bool icf_ready_;
// This list is populated by gc_process_relocs in gc.h.
Reloc_info_list reloc_info_list_;
2009-08-06 04:51:56 +08:00
};
// This function returns true if this section corresponds to a function that
// should be considered by icf as a possible candidate for folding. Some
// earlier gcc versions, like 4.0.3, put constructors and destructors in
// .gnu.linkonce.t sections and hence should be included too.
inline bool
is_section_foldable_candidate(const char* section_name)
{
return (is_prefix_of(".text", section_name)
|| is_prefix_of(".gnu.linkonce.t", section_name));
}
2009-08-06 04:51:56 +08:00
} // End of namespace gold.
#endif