1 // ehframe.h -- handle exception frame sections for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_EHFRAME_H
24 #define GOLD_EHFRAME_H
36 template<int size
, bool big_endian
>
41 // This class manages the .eh_frame_hdr section, which holds the data
42 // for the PT_GNU_EH_FRAME segment. gcc's unwind support code uses
43 // the PT_GNU_EH_FRAME segment to find the list of FDEs. This saves
44 // the time required to register the exception handlers at startup
45 // time and when a shared object is loaded, and the time required to
46 // deregister the exception handlers when a shared object is unloaded.
48 // FIXME: gcc supports using storing a sorted lookup table for the
49 // FDEs in the PT_GNU_EH_FRAME segment, but we do not yet generate
52 class Eh_frame_hdr
: public Output_section_data
55 Eh_frame_hdr(Output_section
* eh_frame_section
, const Eh_frame
*);
57 // Record that we found an unrecognized .eh_frame section.
59 found_unrecognized_eh_frame_section()
60 { this->any_unrecognized_eh_frame_sections_
= true; }
64 record_fde(section_offset_type fde_offset
, unsigned char fde_encoding
)
66 if (!this->any_unrecognized_eh_frame_sections_
)
67 this->fde_offsets_
.push_back(std::make_pair(fde_offset
, fde_encoding
));
70 // Set the final data size.
72 set_final_data_size();
74 // Write the data to the file.
76 do_write(Output_file
*);
79 // Write the data to the file with the right endianness.
80 template<int size
, bool big_endian
>
82 do_sized_write(Output_file
*);
84 // The data we record for one FDE: the offset of the FDE within the
85 // .eh_frame section, and the FDE encoding.
86 typedef std::pair
<section_offset_type
, unsigned char> Fde_offset
;
88 // The list of information we record for an FDE.
89 typedef std::vector
<Fde_offset
> Fde_offsets
;
91 // When writing out the header, we convert the FDE offsets into FDE
92 // addresses. This is a list of pairs of the offset from the header
93 // to the FDE PC and to the FDE itself.
98 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
99 typedef typename
std::pair
<Address
, Address
> Fde_address
;
100 typedef typename
std::vector
<Fde_address
> Fde_address_list
;
101 typedef typename
Fde_address_list::iterator iterator
;
103 Fde_addresses(unsigned int reserve
)
105 { this->fde_addresses_
.reserve(reserve
); }
108 push_back(Address pc_address
, Address fde_address
)
110 this->fde_addresses_
.push_back(std::make_pair(pc_address
, fde_address
));
115 { return this->fde_addresses_
.begin(); }
119 { return this->fde_addresses_
.end(); }
122 Fde_address_list fde_addresses_
;
125 // Compare Fde_address objects.
127 struct Fde_address_compare
130 operator()(const typename Fde_addresses
<size
>::Fde_address
& f1
,
131 const typename Fde_addresses
<size
>::Fde_address
& f2
) const
132 { return f1
.first
< f2
.first
; }
135 // Return the PC to which an FDE refers.
136 template<int size
, bool big_endian
>
137 typename
elfcpp::Elf_types
<size
>::Elf_Addr
138 get_fde_pc(typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
,
139 const unsigned char* eh_frame_contents
,
140 section_offset_type fde_offset
, unsigned char fde_encoding
);
142 // Convert Fde_offsets to Fde_addresses.
143 template<int size
, bool big_endian
>
145 get_fde_addresses(Output_file
* of
,
146 const Fde_offsets
* fde_offsets
,
147 Fde_addresses
<size
>* fde_addresses
);
149 // The .eh_frame section.
150 Output_section
* eh_frame_section_
;
151 // The .eh_frame section data.
152 const Eh_frame
* eh_frame_data_
;
153 // Data from the FDEs in the .eh_frame sections.
154 Fde_offsets fde_offsets_
;
155 // Whether we found any .eh_frame sections which we could not
157 bool any_unrecognized_eh_frame_sections_
;
160 // This class holds an FDE.
165 Fde(Relobj
* object
, unsigned int shndx
, section_offset_type input_offset
,
166 const unsigned char* contents
, size_t length
)
167 : object_(object
), shndx_(shndx
), input_offset_(input_offset
),
168 contents_(reinterpret_cast<const char*>(contents
), length
)
171 // Return the length of this FDE. Add 4 for the length and 4 for
172 // the offset to the CIE.
175 { return this->contents_
.length() + 8; }
177 // Add a mapping for this FDE to MERGE_MAP.
179 add_mapping(section_offset_type output_offset
, Merge_map
* merge_map
) const
181 merge_map
->add_mapping(this->object_
, this->shndx_
,
182 this->input_offset_
, this->length(),
186 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
187 // encoding, from the CIE. Record the FDE in EH_FRAME_HDR. Return
189 template<int size
, bool big_endian
>
191 write(unsigned char* oview
, section_offset_type offset
,
192 section_offset_type cie_offset
, unsigned char fde_encoding
,
193 Eh_frame_hdr
* eh_frame_hdr
);
196 // The object in which this FDE was seen.
198 // Input section index for this FDE.
200 // Offset within the input section for this FDE.
201 section_offset_type input_offset_
;
203 std::string contents_
;
206 // This class holds a CIE.
211 Cie(Relobj
* object
, unsigned int shndx
, section_offset_type input_offset
,
212 unsigned char fde_encoding
, const char* personality_name
,
213 const unsigned char* contents
, size_t length
)
216 input_offset_(input_offset
),
217 fde_encoding_(fde_encoding
),
218 personality_name_(personality_name
),
220 contents_(reinterpret_cast<const char*>(contents
), length
)
225 // We permit copying a CIE when there are no FDEs. This is
226 // convenient in the code which creates them.
228 : object_(cie
.object_
),
230 input_offset_(cie
.input_offset_
),
231 fde_encoding_(cie
.fde_encoding_
),
232 personality_name_(cie
.personality_name_
),
234 contents_(cie
.contents_
)
235 { gold_assert(cie
.fdes_
.empty()); }
237 // Add an FDE associated with this CIE.
240 { this->fdes_
.push_back(fde
); }
242 // Return the number of FDEs.
245 { return this->fdes_
.size(); }
247 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
248 // followed by all its FDEs. ADDRALIGN is the required address
249 // alignment, typically 4 or 8. This updates MERGE_MAP with the
250 // mapping. It returns the new output offset.
252 set_output_offset(section_offset_type output_offset
, unsigned int addralign
,
255 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
256 // exception frame header for FDE recording. Return the new offset.
257 template<int size
, bool big_endian
>
259 write(unsigned char* oview
, section_offset_type offset
,
260 Eh_frame_hdr
* eh_frame_hdr
);
262 friend bool operator<(const Cie
&, const Cie
&);
263 friend bool operator==(const Cie
&, const Cie
&);
266 // The class is not assignable.
267 Cie
& operator=(const Cie
&);
269 // The object in which this CIE was first seen.
271 // Input section index for this CIE.
273 // Offset within the input section for this CIE.
274 section_offset_type input_offset_
;
275 // The encoding of the FDE. This is a DW_EH_PE code.
276 unsigned char fde_encoding_
;
277 // The name of the personality routine. This will be the name of a
278 // global symbol, or will be the empty string.
279 std::string personality_name_
;
281 std::vector
<Fde
*> fdes_
;
283 std::string contents_
;
286 extern bool operator<(const Cie
&, const Cie
&);
287 extern bool operator==(const Cie
&, const Cie
&);
289 // This class manages .eh_frame sections. It discards duplicate
290 // exception information.
292 class Eh_frame
: public Output_section_data
297 // Record the associated Eh_frame_hdr, if any.
299 set_eh_frame_hdr(Eh_frame_hdr
* hdr
)
300 { this->eh_frame_hdr_
= hdr
; }
302 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
303 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
304 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
305 // is the relocation section if any (0 for none, -1U for multiple).
306 // RELOC_TYPE is the type of the relocation section if any. This
307 // returns whether the section was incorporated into the .eh_frame
309 template<int size
, bool big_endian
>
311 add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
312 const unsigned char* symbols
,
313 section_size_type symbols_size
,
314 const unsigned char* symbol_names
,
315 section_size_type symbol_names_size
,
316 unsigned int shndx
, unsigned int reloc_shndx
,
317 unsigned int reloc_type
);
319 // Return the number of FDEs.
323 // Set the final data size.
325 set_final_data_size();
327 // Return the output address for an input address.
329 do_output_offset(const Relobj
*, unsigned int shndx
,
330 section_offset_type offset
,
331 section_offset_type
* poutput
) const;
333 // Return whether this is the merge section for an input section.
335 do_is_merge_section_for(const Relobj
*, unsigned int shndx
) const;
337 // Write the data to the file.
339 do_write(Output_file
*);
342 // The comparison routine for the CIE map.
346 operator()(const Cie
* cie1
, const Cie
* cie2
) const
347 { return *cie1
< *cie2
; }
350 // A set of unique CIEs.
351 typedef std::set
<Cie
*, Cie_less
> Cie_offsets
;
353 // A list of unmergeable CIEs.
354 typedef std::vector
<Cie
*> Unmergeable_cie_offsets
;
356 // A mapping from offsets to CIEs. This is used while reading an
358 typedef std::map
<uint64_t, Cie
*> Offsets_to_cie
;
360 // A list of CIEs, and a bool indicating whether the CIE is
362 typedef std::vector
<std::pair
<Cie
*, bool> > New_cies
;
366 skip_leb128(const unsigned char**, const unsigned char*);
368 // The implementation of add_ehframe_input_section.
369 template<int size
, bool big_endian
>
371 do_add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
372 const unsigned char* symbols
,
373 section_size_type symbols_size
,
374 const unsigned char* symbol_names
,
375 section_size_type symbol_names_size
,
377 unsigned int reloc_shndx
,
378 unsigned int reloc_type
,
379 const unsigned char* pcontents
,
380 section_size_type contents_len
,
384 template<int size
, bool big_endian
>
386 read_cie(Sized_relobj
<size
, big_endian
>* object
,
388 const unsigned char* symbols
,
389 section_size_type symbols_size
,
390 const unsigned char* symbol_names
,
391 section_size_type symbol_names_size
,
392 const unsigned char* pcontents
,
393 const unsigned char* pcie
,
394 const unsigned char *pcieend
,
395 Track_relocs
<size
, big_endian
>* relocs
,
396 Offsets_to_cie
* cies
,
400 template<int size
, bool big_endian
>
402 read_fde(Sized_relobj
<size
, big_endian
>* object
,
404 const unsigned char* symbols
,
405 section_size_type symbols_size
,
406 const unsigned char* pcontents
,
408 const unsigned char* pfde
,
409 const unsigned char *pfdeend
,
410 Track_relocs
<size
, big_endian
>* relocs
,
411 Offsets_to_cie
* cies
);
413 // Template version of write function.
414 template<int size
, bool big_endian
>
416 do_sized_write(unsigned char* oview
);
418 // The exception frame header, if any.
419 Eh_frame_hdr
* eh_frame_hdr_
;
420 // A mapping from all unique CIEs to their offset in the output
422 Cie_offsets cie_offsets_
;
423 // A mapping from unmergeable CIEs to their offset in the output
425 Unmergeable_cie_offsets unmergeable_cie_offsets_
;
426 // A mapping from input sections to the output section.
427 Merge_map merge_map_
;
430 } // End namespace gold.
432 #endif // !defined(GOLD_EHFRAME_H)