1 // ehframe.h -- handle exception frame sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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
));
71 // Set the final data size.
73 set_final_data_size();
75 // Write the data to the file.
77 do_write(Output_file
*);
79 // Write to a map file.
81 do_print_to_mapfile(Mapfile
* mapfile
) const
82 { mapfile
->print_output_data(this, _("** eh_frame_hdr")); }
85 // Write the data to the file with the right endianness.
86 template<int size
, bool big_endian
>
88 do_sized_write(Output_file
*);
90 // The data we record for one FDE: the offset of the FDE within the
91 // .eh_frame section, and the FDE encoding.
92 typedef std::pair
<section_offset_type
, unsigned char> Fde_offset
;
94 // The list of information we record for an FDE.
95 typedef std::vector
<Fde_offset
> Fde_offsets
;
97 // When writing out the header, we convert the FDE offsets into FDE
98 // addresses. This is a list of pairs of the offset from the header
99 // to the FDE PC and to the FDE itself.
104 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
105 typedef typename
std::pair
<Address
, Address
> Fde_address
;
106 typedef typename
std::vector
<Fde_address
> Fde_address_list
;
107 typedef typename
Fde_address_list::iterator iterator
;
109 Fde_addresses(unsigned int reserve
)
111 { this->fde_addresses_
.reserve(reserve
); }
114 push_back(Address pc_address
, Address fde_address
)
116 this->fde_addresses_
.push_back(std::make_pair(pc_address
, fde_address
));
121 { return this->fde_addresses_
.begin(); }
125 { return this->fde_addresses_
.end(); }
128 Fde_address_list fde_addresses_
;
131 // Compare Fde_address objects.
133 struct Fde_address_compare
136 operator()(const typename Fde_addresses
<size
>::Fde_address
& f1
,
137 const typename Fde_addresses
<size
>::Fde_address
& f2
) const
138 { return f1
.first
< f2
.first
; }
141 // Return the PC to which an FDE refers.
142 template<int size
, bool big_endian
>
143 typename
elfcpp::Elf_types
<size
>::Elf_Addr
144 get_fde_pc(typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
,
145 const unsigned char* eh_frame_contents
,
146 section_offset_type fde_offset
, unsigned char fde_encoding
);
148 // Convert Fde_offsets to Fde_addresses.
149 template<int size
, bool big_endian
>
151 get_fde_addresses(Output_file
* of
,
152 const Fde_offsets
* fde_offsets
,
153 Fde_addresses
<size
>* fde_addresses
);
155 // The .eh_frame section.
156 Output_section
* eh_frame_section_
;
157 // The .eh_frame section data.
158 const Eh_frame
* eh_frame_data_
;
159 // Data from the FDEs in the .eh_frame sections.
160 Fde_offsets fde_offsets_
;
161 // Whether we found any .eh_frame sections which we could not
163 bool any_unrecognized_eh_frame_sections_
;
166 // This class holds an FDE.
171 Fde(Relobj
* object
, unsigned int shndx
, section_offset_type input_offset
,
172 const unsigned char* contents
, size_t length
)
173 : object_(object
), shndx_(shndx
), input_offset_(input_offset
),
174 contents_(reinterpret_cast<const char*>(contents
), length
)
177 // Return the length of this FDE. Add 4 for the length and 4 for
178 // the offset to the CIE.
181 { return this->contents_
.length() + 8; }
183 // Add a mapping for this FDE to MERGE_MAP.
185 add_mapping(section_offset_type output_offset
, Merge_map
* merge_map
) const
187 merge_map
->add_mapping(this->object_
, this->shndx_
,
188 this->input_offset_
, this->length(),
192 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
193 // encoding, from the CIE. Round up the bytes to ADDRALIGN if
194 // necessary. Record the FDE in EH_FRAME_HDR. Return the new
196 template<int size
, bool big_endian
>
198 write(unsigned char* oview
, section_offset_type offset
,
199 unsigned int addralign
, section_offset_type cie_offset
,
200 unsigned char fde_encoding
, Eh_frame_hdr
* eh_frame_hdr
);
203 // The object in which this FDE was seen.
205 // Input section index for this FDE.
207 // Offset within the input section for this FDE.
208 section_offset_type input_offset_
;
210 std::string contents_
;
213 // This class holds a CIE.
218 Cie(Relobj
* object
, unsigned int shndx
, section_offset_type input_offset
,
219 unsigned char fde_encoding
, const char* personality_name
,
220 const unsigned char* contents
, size_t length
)
223 input_offset_(input_offset
),
224 fde_encoding_(fde_encoding
),
225 personality_name_(personality_name
),
227 contents_(reinterpret_cast<const char*>(contents
), length
)
232 // We permit copying a CIE when there are no FDEs. This is
233 // convenient in the code which creates them.
235 : object_(cie
.object_
),
237 input_offset_(cie
.input_offset_
),
238 fde_encoding_(cie
.fde_encoding_
),
239 personality_name_(cie
.personality_name_
),
241 contents_(cie
.contents_
)
242 { gold_assert(cie
.fdes_
.empty()); }
244 // Add an FDE associated with this CIE.
247 { this->fdes_
.push_back(fde
); }
249 // Return the number of FDEs.
252 { return this->fdes_
.size(); }
254 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
255 // followed by all its FDEs. ADDRALIGN is the required address
256 // alignment, typically 4 or 8. This updates MERGE_MAP with the
257 // mapping. It returns the new output offset.
259 set_output_offset(section_offset_type output_offset
, unsigned int addralign
,
262 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
263 // exception frame header for FDE recording. Round up the bytes to
264 // ADDRALIGN. Return the new offset.
265 template<int size
, bool big_endian
>
267 write(unsigned char* oview
, section_offset_type offset
,
268 unsigned int addralign
, Eh_frame_hdr
* eh_frame_hdr
);
270 friend bool operator<(const Cie
&, const Cie
&);
271 friend bool operator==(const Cie
&, const Cie
&);
274 // The class is not assignable.
275 Cie
& operator=(const Cie
&);
277 // The object in which this CIE was first seen.
279 // Input section index for this CIE.
281 // Offset within the input section for this CIE.
282 section_offset_type input_offset_
;
283 // The encoding of the FDE. This is a DW_EH_PE code.
284 unsigned char fde_encoding_
;
285 // The name of the personality routine. This will be the name of a
286 // global symbol, or will be the empty string.
287 std::string personality_name_
;
289 std::vector
<Fde
*> fdes_
;
291 std::string contents_
;
294 extern bool operator<(const Cie
&, const Cie
&);
295 extern bool operator==(const Cie
&, const Cie
&);
297 // This class manages .eh_frame sections. It discards duplicate
298 // exception information.
300 class Eh_frame
: public Output_section_data
305 // Record the associated Eh_frame_hdr, if any.
307 set_eh_frame_hdr(Eh_frame_hdr
* hdr
)
308 { this->eh_frame_hdr_
= hdr
; }
310 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
311 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
312 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
313 // is the relocation section if any (0 for none, -1U for multiple).
314 // RELOC_TYPE is the type of the relocation section if any. This
315 // returns whether the section was incorporated into the .eh_frame
317 template<int size
, bool big_endian
>
319 add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
320 const unsigned char* symbols
,
321 section_size_type symbols_size
,
322 const unsigned char* symbol_names
,
323 section_size_type symbol_names_size
,
324 unsigned int shndx
, unsigned int reloc_shndx
,
325 unsigned int reloc_type
);
327 // Return the number of FDEs.
332 // Set the final data size.
334 set_final_data_size();
336 // Return the output address for an input address.
338 do_output_offset(const Relobj
*, unsigned int shndx
,
339 section_offset_type offset
,
340 section_offset_type
* poutput
) const;
342 // Return whether this is the merge section for an input section.
344 do_is_merge_section_for(const Relobj
*, unsigned int shndx
) const;
346 // Write the data to the file.
348 do_write(Output_file
*);
350 // Write to a map file.
352 do_print_to_mapfile(Mapfile
* mapfile
) const
353 { mapfile
->print_output_data(this, _("** eh_frame")); }
356 // The comparison routine for the CIE map.
360 operator()(const Cie
* cie1
, const Cie
* cie2
) const
361 { return *cie1
< *cie2
; }
364 // A set of unique CIEs.
365 typedef std::set
<Cie
*, Cie_less
> Cie_offsets
;
367 // A list of unmergeable CIEs.
368 typedef std::vector
<Cie
*> Unmergeable_cie_offsets
;
370 // A mapping from offsets to CIEs. This is used while reading an
372 typedef std::map
<uint64_t, Cie
*> Offsets_to_cie
;
374 // A list of CIEs, and a bool indicating whether the CIE is
376 typedef std::vector
<std::pair
<Cie
*, bool> > New_cies
;
380 skip_leb128(const unsigned char**, const unsigned char*);
382 // The implementation of add_ehframe_input_section.
383 template<int size
, bool big_endian
>
385 do_add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
386 const unsigned char* symbols
,
387 section_size_type symbols_size
,
388 const unsigned char* symbol_names
,
389 section_size_type symbol_names_size
,
391 unsigned int reloc_shndx
,
392 unsigned int reloc_type
,
393 const unsigned char* pcontents
,
394 section_size_type contents_len
,
398 template<int size
, bool big_endian
>
400 read_cie(Sized_relobj
<size
, big_endian
>* object
,
402 const unsigned char* symbols
,
403 section_size_type symbols_size
,
404 const unsigned char* symbol_names
,
405 section_size_type symbol_names_size
,
406 const unsigned char* pcontents
,
407 const unsigned char* pcie
,
408 const unsigned char* pcieend
,
409 Track_relocs
<size
, big_endian
>* relocs
,
410 Offsets_to_cie
* cies
,
414 template<int size
, bool big_endian
>
416 read_fde(Sized_relobj
<size
, big_endian
>* object
,
418 const unsigned char* symbols
,
419 section_size_type symbols_size
,
420 const unsigned char* pcontents
,
422 const unsigned char* pfde
,
423 const unsigned char* pfdeend
,
424 Track_relocs
<size
, big_endian
>* relocs
,
425 Offsets_to_cie
* cies
);
427 // Template version of write function.
428 template<int size
, bool big_endian
>
430 do_sized_write(unsigned char* oview
);
432 // The exception frame header, if any.
433 Eh_frame_hdr
* eh_frame_hdr_
;
434 // A mapping from all unique CIEs to their offset in the output
436 Cie_offsets cie_offsets_
;
437 // A mapping from unmergeable CIEs to their offset in the output
439 Unmergeable_cie_offsets unmergeable_cie_offsets_
;
440 // A mapping from input sections to the output section.
441 Merge_map merge_map_
;
442 // Whether we have created the mappings to the output section.
443 bool mappings_are_done_
;
444 // The final data size. This is only set if mappings_are_done_ is
446 section_size_type final_data_size_
;
449 } // End namespace gold.
451 #endif // !defined(GOLD_EHFRAME_H)