X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=gold%2Fehframe.h;h=f2830068a08131e9951d5554e22476aee5b3be89;hb=5a3ca6e319583a49310067a4b47d7b0dd080c2cd;hp=12c8c160df8ae85dc2e4f7f91c7fdaff5839722e;hpb=27bc2bce094e3a3ef536c5b8c2a38470bd7f3217;p=binutils-gdb.git diff --git a/gold/ehframe.h b/gold/ehframe.h index 12c8c160df8..f2830068a08 100644 --- a/gold/ehframe.h +++ b/gold/ehframe.h @@ -1,6 +1,6 @@ // ehframe.h -- handle exception frame sections for gold -*- C++ -*- -// Copyright 2006, 2007 Free Software Foundation, Inc. +// Copyright (C) 2006-2022 Free Software Foundation, Inc. // Written by Ian Lance Taylor . // This file is part of gold. @@ -23,6 +23,10 @@ #ifndef GOLD_EHFRAME_H #define GOLD_EHFRAME_H +#include +#include +#include + #include "output.h" #include "merge.h" @@ -41,10 +45,6 @@ class Eh_frame; // time and when a shared object is loaded, and the time required to // deregister the exception handlers when a shared object is unloaded. -// FIXME: gcc supports using storing a sorted lookup table for the -// FDEs in the PT_GNU_EH_FRAME segment, but we do not yet generate -// that. - class Eh_frame_hdr : public Output_section_data { public: @@ -57,12 +57,13 @@ class Eh_frame_hdr : public Output_section_data // Record an FDE. void - record_fde(off_t fde_offset, unsigned char fde_encoding) + record_fde(section_offset_type fde_offset, unsigned char fde_encoding) { if (!this->any_unrecognized_eh_frame_sections_) this->fde_offsets_.push_back(std::make_pair(fde_offset, fde_encoding)); } + protected: // Set the final data size. void set_final_data_size(); @@ -71,6 +72,11 @@ class Eh_frame_hdr : public Output_section_data void do_write(Output_file*); + // Write to a map file. + void + do_print_to_mapfile(Mapfile* mapfile) const + { mapfile->print_output_data(this, _("** eh_frame_hdr")); } + private: // Write the data to the file with the right endianness. template @@ -79,7 +85,7 @@ class Eh_frame_hdr : public Output_section_data // The data we record for one FDE: the offset of the FDE within the // .eh_frame section, and the FDE encoding. - typedef std::pair Fde_offset; + typedef std::pair Fde_offset; // The list of information we record for an FDE. typedef std::vector Fde_offsets; @@ -131,8 +137,9 @@ class Eh_frame_hdr : public Output_section_data // Return the PC to which an FDE refers. template typename elfcpp::Elf_types::Elf_Addr - get_fde_pc(const unsigned char* eh_frame_contents, - off_t fde_offset, unsigned char fde_encoding); + get_fde_pc(typename elfcpp::Elf_types::Elf_Addr eh_frame_address, + const unsigned char* eh_frame_contents, + section_offset_type fde_offset, unsigned char fde_encoding); // Convert Fde_offsets to Fde_addresses. template @@ -157,11 +164,24 @@ class Eh_frame_hdr : public Output_section_data class Fde { public: - Fde(Relobj* object, unsigned int shndx, off_t input_offset, + Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset, const unsigned char* contents, size_t length) - : object_(object), shndx_(shndx), input_offset_(input_offset), + : object_(object), contents_(reinterpret_cast(contents), length) - { } + { + this->u_.from_object.shndx = shndx; + this->u_.from_object.input_offset = input_offset; + } + + // Create an FDE associated with a PLT. + Fde(Output_data* plt, const unsigned char* contents, size_t length, + bool post_map) + : object_(NULL), + contents_(reinterpret_cast(contents), length) + { + this->u_.from_linker.plt = plt; + this->u_.from_linker.post_map = post_map; + } // Return the length of this FDE. Add 4 for the length and 4 for // the offset to the CIE. @@ -169,40 +189,90 @@ class Fde length() const { return this->contents_.length() + 8; } - // Add a mapping for this FDE to MERGE_MAP. + // Add a mapping for this FDE to MERGE_MAP, so that relocations + // against the FDE are applied to right part of the output file. void - add_mapping(off_t output_offset, Merge_map* merge_map) const + add_mapping(section_offset_type output_offset, + Output_section_data* output_data) const { - merge_map->add_mapping(this->object_, this->shndx_, - this->input_offset_, this->length(), - output_offset); + if (this->object_ != NULL) + this->object_->add_merge_mapping(output_data, this->u_.from_object.shndx, + this->u_.from_object.input_offset, this->length(), + output_offset); } + // Return whether this FDE was added after merge mapping. + bool + post_map() const + { return this->object_ == NULL && this->u_.from_linker.post_map; } + + // Return whether this FDE was added for the PLT after merge mapping. + bool + post_map(const Output_data* plt) const + { return this->post_map() && this->u_.from_linker.plt == plt; } + // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the - // encoding, from the CIE. Record the FDE in EH_FRAME_HDR. Return - // the new offset. + // encoding, from the CIE. Round up the bytes to ADDRALIGN if + // necessary. ADDRESS is the virtual address of OVIEW. Record the + // FDE in EH_FRAME_HDR. Return the new offset. template - off_t - write(unsigned char* oview, off_t offset, off_t cie_offset, - unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr); + section_offset_type + write(unsigned char* oview, section_offset_type output_section_offset, + section_offset_type offset, uint64_t address, unsigned int addralign, + section_offset_type cie_offset, unsigned char fde_encoding, + Eh_frame_hdr* eh_frame_hdr); private: - // The object in which this FDE was seen. + // The object in which this FDE was seen. This will be NULL for a + // linker generated FDE. Relobj* object_; - // Input section index for this FDE. - unsigned int shndx_; - // Offset within the input section for this FDE. - off_t input_offset_; + union + { + // These fields are used if the FDE is from an input object (the + // object_ field is not NULL). + struct + { + // Input section index for this FDE. + unsigned int shndx; + // Offset within the input section for this FDE. + section_offset_type input_offset; + } from_object; + // This field is used if the FDE is generated by the linker (the + // object_ field is NULL). + struct + { + // The only linker generated FDEs are for PLT sections, and this + // points to the PLT section. + Output_data* plt; + // Set if the FDE was added after merge mapping. + bool post_map; + } from_linker; + } u_; // FDE data. std::string contents_; }; +// A FDE plus some info from a CIE to allow later writing of the FDE. + +struct Post_fde +{ + Post_fde(Fde* f, section_offset_type cie_off, unsigned char encoding) + : fde(f), cie_offset(cie_off), fde_encoding(encoding) + { } + + Fde* fde; + section_offset_type cie_offset; + unsigned char fde_encoding; +}; + +typedef std::vector Post_fdes; + // This class holds a CIE. class Cie { public: - Cie(Relobj* object, unsigned int shndx, off_t input_offset, + Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset, unsigned char fde_encoding, const char* personality_name, const unsigned char* contents, size_t length) : object_(object), @@ -233,6 +303,16 @@ class Cie add_fde(Fde* fde) { this->fdes_.push_back(fde); } + // Remove the last FDE associated with this CIE. + void + remove_fde() + { this->fdes_.pop_back(); } + + // Access the last FDE associated with this CIE. + const Fde* + last_fde() const + { return this->fdes_.back(); } + // Return the number of FDEs. unsigned int fde_count() const @@ -242,14 +322,26 @@ class Cie // followed by all its FDEs. ADDRALIGN is the required address // alignment, typically 4 or 8. This updates MERGE_MAP with the // mapping. It returns the new output offset. - off_t - set_output_offset(off_t output_offset, unsigned int addralign, Merge_map*); - - // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the - // exception frame header for FDE recording. Return the new offset. + section_offset_type + set_output_offset(section_offset_type output_offset, unsigned int addralign, + Output_section_data*); + + // Write the CIE to OVIEW starting at OFFSET. Round up the bytes to + // ADDRALIGN. ADDRESS is the virtual address of OVIEW. + // EH_FRAME_HDR is the exception frame header for FDE recording. + // POST_FDES stashes FDEs created after mappings were done, for later + // writing. Return the new offset. template - off_t - write(unsigned char* oview, off_t offset, Eh_frame_hdr* eh_frame_hdr); + section_offset_type + write(unsigned char* oview, section_offset_type output_section_offset, + section_offset_type offset, uint64_t address, + unsigned int addralign, Eh_frame_hdr* eh_frame_hdr, + Post_fdes* post_fdes); + + // Return the FDE encoding. + unsigned char + fde_encoding() const + { return this->fde_encoding_; } friend bool operator<(const Cie&, const Cie&); friend bool operator==(const Cie&, const Cie&); @@ -258,12 +350,15 @@ class Cie // The class is not assignable. Cie& operator=(const Cie&); - // The object in which this CIE was first seen. + // The object in which this CIE was first seen. This will be NULL + // for a linker generated CIE. Relobj* object_; - // Input section index for this CIE. + // Input section index for this CIE. This will be 0 for a linker + // generated CIE. unsigned int shndx_; - // Offset within the input section for this CIE. - off_t input_offset_; + // Offset within the input section for this CIE. This will be 0 for + // a linker generated CIE. + section_offset_type input_offset_; // The encoding of the FDE. This is a DW_EH_PE code. unsigned char fde_encoding_; // The name of the personality routine. This will be the name of a @@ -284,6 +379,14 @@ extern bool operator==(const Cie&, const Cie&); class Eh_frame : public Output_section_data { public: + enum Eh_frame_section_disposition + { + EH_EMPTY_SECTION, + EH_UNRECOGNIZED_SECTION, + EH_OPTIMIZABLE_SECTION, + EH_END_MARKER_SECTION + }; + Eh_frame(); // Record the associated Eh_frame_hdr, if any. @@ -299,32 +402,53 @@ class Eh_frame : public Output_section_data // returns whether the section was incorporated into the .eh_frame // data. template - bool - add_ehframe_input_section(Sized_relobj* object, + Eh_frame_section_disposition + add_ehframe_input_section(Sized_relobj_file* object, const unsigned char* symbols, - off_t symbols_size, + section_size_type symbols_size, const unsigned char* symbol_names, - off_t symbol_names_size, + section_size_type symbol_names_size, unsigned int shndx, unsigned int reloc_shndx, unsigned int reloc_type); + // Add a CIE and an FDE for a PLT section, to permit unwinding + // through a PLT. The FDE data should start with 8 bytes of zero, + // which will be replaced by a 4 byte PC relative reference to the + // address of PLT and a 4 byte size of PLT. + void + add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data, + size_t cie_length, const unsigned char* fde_data, + size_t fde_length); + + // Remove all post-map unwind information for a PLT. + void + remove_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data, + size_t cie_length); + // Return the number of FDEs. unsigned int fde_count() const; + protected: // Set the final data size. void set_final_data_size(); // Return the output address for an input address. bool - do_output_offset(const Relobj*, unsigned int shndx, off_t offset, - off_t* poutput) const; + do_output_offset(const Relobj*, unsigned int shndx, + section_offset_type offset, + section_offset_type* poutput) const; // Write the data to the file. void do_write(Output_file*); + // Write to a map file. + void + do_print_to_mapfile(Mapfile* mapfile) const + { mapfile->print_output_data(this, _("** eh_frame")); } + private: // The comparison routine for the CIE map. struct Cie_less @@ -334,11 +458,11 @@ class Eh_frame : public Output_section_data { return *cie1 < *cie2; } }; - // A mapping from unique CIEs to their offset in the output file. - typedef std::map Cie_offsets; + // A set of unique CIEs. + typedef std::set Cie_offsets; - // A list of unmergeable CIEs with their offsets. - typedef std::vector > Unmergeable_cie_offsets; + // A list of unmergeable CIEs. + typedef std::vector Unmergeable_cie_offsets; // A mapping from offsets to CIEs. This is used while reading an // input section. @@ -355,30 +479,30 @@ class Eh_frame : public Output_section_data // The implementation of add_ehframe_input_section. template bool - do_add_ehframe_input_section(Sized_relobj* object, + do_add_ehframe_input_section(Sized_relobj_file* object, const unsigned char* symbols, - off_t symbols_size, + section_size_type symbols_size, const unsigned char* symbol_names, - off_t symbol_names_size, + section_size_type symbol_names_size, unsigned int shndx, unsigned int reloc_shndx, unsigned int reloc_type, const unsigned char* pcontents, - off_t contents_len, + section_size_type contents_len, New_cies*); // Read a CIE. template bool - read_cie(Sized_relobj* object, + read_cie(Sized_relobj_file* object, unsigned int shndx, const unsigned char* symbols, - off_t symbols_size, + section_size_type symbols_size, const unsigned char* symbol_names, - off_t symbol_names_size, + section_size_type symbol_names_size, const unsigned char* pcontents, const unsigned char* pcie, - const unsigned char *pcieend, + const unsigned char* pcieend, Track_relocs* relocs, Offsets_to_cie* cies, New_cies* new_cies); @@ -386,14 +510,14 @@ class Eh_frame : public Output_section_data // Read an FDE. template bool - read_fde(Sized_relobj* object, + read_fde(Sized_relobj_file* object, unsigned int shndx, const unsigned char* symbols, - off_t symbols_size, + section_size_type symbols_size, const unsigned char* pcontents, unsigned int offset, const unsigned char* pfde, - const unsigned char *pfdeend, + const unsigned char* pfdeend, Track_relocs* relocs, Offsets_to_cie* cies); @@ -410,8 +534,11 @@ class Eh_frame : public Output_section_data // A mapping from unmergeable CIEs to their offset in the output // file. Unmergeable_cie_offsets unmergeable_cie_offsets_; - // A mapping from input sections to the output section. - Merge_map merge_map_; + // Whether we have created the mappings to the output section. + bool mappings_are_done_; + // The final data size. This is only set if mappings_are_done_ is + // true. + section_size_type final_data_size_; }; } // End namespace gold.