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
32 template<int size
, bool big_endian
>
37 // This class manages the .eh_frame_hdr section, which holds the data
38 // for the PT_GNU_EH_FRAME segment. gcc's unwind support code uses
39 // the PT_GNU_EH_FRAME segment to find the list of FDEs. This saves
40 // the time required to register the exception handlers at startup
41 // time and when a shared object is loaded, and the time required to
42 // deregister the exception handlers when a shared object is unloaded.
44 // FIXME: gcc supports using storing a sorted lookup table for the
45 // FDEs in the PT_GNU_EH_FRAME segment, but we do not yet generate
48 class Eh_frame_hdr
: public Output_section_data
51 Eh_frame_hdr(Output_section
* eh_frame_section
, const Eh_frame
*);
53 // Record that we found an unrecognized .eh_frame section.
55 found_unrecognized_eh_frame_section()
56 { this->any_unrecognized_eh_frame_sections_
= true; }
60 record_fde(off_t fde_offset
, unsigned char fde_encoding
)
62 if (!this->any_unrecognized_eh_frame_sections_
)
63 this->fde_offsets_
.push_back(std::make_pair(fde_offset
, fde_encoding
));
66 // Set the final data size.
68 do_set_address(uint64_t address
, off_t offset
);
70 // Write the data to the file.
72 do_write(Output_file
*);
75 // Write the data to the file with the right endianness.
76 template<int size
, bool big_endian
>
78 do_sized_write(Output_file
*);
80 // The data we record for one FDE: the offset of the FDE within the
81 // .eh_frame section, and the FDE encoding.
82 typedef std::pair
<off_t
, unsigned char> Fde_offset
;
84 // The list of information we record for an FDE.
85 typedef std::vector
<Fde_offset
> Fde_offsets
;
87 // When writing out the header, we convert the FDE offsets into FDE
88 // addresses. This is a list of pairs of the offset from the header
89 // to the FDE PC and to the FDE itself.
94 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
95 typedef typename
std::pair
<Address
, Address
> Fde_address
;
96 typedef typename
std::vector
<Fde_address
> Fde_address_list
;
97 typedef typename
Fde_address_list::iterator iterator
;
99 Fde_addresses(unsigned int reserve
)
101 { this->fde_addresses_
.reserve(reserve
); }
104 push_back(Address pc_address
, Address fde_address
)
106 this->fde_addresses_
.push_back(std::make_pair(pc_address
, fde_address
));
111 { return this->fde_addresses_
.begin(); }
115 { return this->fde_addresses_
.end(); }
118 Fde_address_list fde_addresses_
;
121 // Compare Fde_address objects.
123 struct Fde_address_compare
126 operator()(const typename Fde_addresses
<size
>::Fde_address
& f1
,
127 const typename Fde_addresses
<size
>::Fde_address
& f2
) const
128 { return f1
.first
< f2
.first
; }
131 // Return the PC to which an FDE refers.
132 template<int size
, bool big_endian
>
133 typename
elfcpp::Elf_types
<size
>::Elf_Addr
134 get_fde_pc(const unsigned char* eh_frame_contents
,
135 off_t fde_offset
, unsigned char fde_encoding
);
137 // Convert Fde_offsets to Fde_addresses.
138 template<int size
, bool big_endian
>
140 get_fde_addresses(Output_file
* of
,
141 const Fde_offsets
* fde_offsets
,
142 Fde_addresses
<size
>* fde_addresses
);
144 // The .eh_frame section.
145 Output_section
* eh_frame_section_
;
146 // The .eh_frame section data.
147 const Eh_frame
* eh_frame_data_
;
148 // Data from the FDEs in the .eh_frame sections.
149 Fde_offsets fde_offsets_
;
150 // Whether we found any .eh_frame sections which we could not
152 bool any_unrecognized_eh_frame_sections_
;
155 // This class holds an FDE.
160 Fde(Relobj
* object
, unsigned int shndx
, off_t input_offset
,
161 const unsigned char* contents
, size_t length
)
162 : object_(object
), shndx_(shndx
), input_offset_(input_offset
),
163 contents_(reinterpret_cast<const char*>(contents
), length
)
166 // Return the length of this FDE. Add 4 for the length and 4 for
167 // the offset to the CIE.
170 { return this->contents_
.length() + 8; }
172 // Add a mapping for this FDE to MERGE_MAP.
174 add_mapping(off_t output_offset
, Merge_map
* merge_map
) const
176 merge_map
->add_mapping(this->object_
, this->shndx_
,
177 this->input_offset_
, this->length(),
181 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
182 // encoding, from the CIE. Record the FDE in EH_FRAME_HDR. Return
184 template<int size
, bool big_endian
>
186 write(unsigned char* oview
, off_t offset
, off_t cie_offset
,
187 unsigned char fde_encoding
, Eh_frame_hdr
* eh_frame_hdr
);
190 // The object in which this FDE was seen.
192 // Input section index for this FDE.
194 // Offset within the input section for this FDE.
197 std::string contents_
;
200 // This class holds a CIE.
205 Cie(Relobj
* object
, unsigned int shndx
, off_t input_offset
,
206 unsigned char fde_encoding
, const char* personality_name
,
207 const unsigned char* contents
, size_t length
)
210 input_offset_(input_offset
),
211 fde_encoding_(fde_encoding
),
212 personality_name_(personality_name
),
214 contents_(reinterpret_cast<const char*>(contents
), length
)
219 // We permit copying a CIE when there are no FDEs. This is
220 // convenient in the code which creates them.
222 : object_(cie
.object_
),
224 input_offset_(cie
.input_offset_
),
225 fde_encoding_(cie
.fde_encoding_
),
226 personality_name_(cie
.personality_name_
),
228 contents_(cie
.contents_
)
229 { gold_assert(cie
.fdes_
.empty()); }
231 // Add an FDE associated with this CIE.
234 { this->fdes_
.push_back(fde
); }
236 // Return the number of FDEs.
239 { return this->fdes_
.size(); }
241 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
242 // followed by all its FDEs. ADDRALIGN is the required address
243 // alignment, typically 4 or 8. This updates MERGE_MAP with the
244 // mapping. It returns the new output offset.
246 set_output_offset(off_t output_offset
, unsigned int addralign
, Merge_map
*);
248 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
249 // exception frame header for FDE recording. Return the new offset.
250 template<int size
, bool big_endian
>
252 write(unsigned char* oview
, off_t offset
, Eh_frame_hdr
* eh_frame_hdr
);
254 friend bool operator<(const Cie
&, const Cie
&);
255 friend bool operator==(const Cie
&, const Cie
&);
258 // The class is not assignable.
259 Cie
& operator=(const Cie
&);
261 // The object in which this CIE was first seen.
263 // Input section index for this CIE.
265 // Offset within the input section for this CIE.
267 // The encoding of the FDE. This is a DW_EH_PE code.
268 unsigned char fde_encoding_
;
269 // The name of the personality routine. This will be the name of a
270 // global symbol, or will be the empty string.
271 std::string personality_name_
;
273 std::vector
<Fde
*> fdes_
;
275 std::string contents_
;
278 extern bool operator<(const Cie
&, const Cie
&);
279 extern bool operator==(const Cie
&, const Cie
&);
281 // This class manages .eh_frame sections. It discards duplicate
282 // exception information.
284 class Eh_frame
: public Output_section_data
289 // Record the associated Eh_frame_hdr, if any.
291 set_eh_frame_hdr(Eh_frame_hdr
* hdr
)
292 { this->eh_frame_hdr_
= hdr
; }
294 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
295 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
296 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
297 // is the relocation section if any (0 for none, -1U for multiple).
298 // RELOC_TYPE is the type of the relocation section if any. This
299 // returns whether the section was incorporated into the .eh_frame
301 template<int size
, bool big_endian
>
303 add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
304 const unsigned char* symbols
,
306 const unsigned char* symbol_names
,
307 off_t symbol_names_size
,
308 unsigned int shndx
, unsigned int reloc_shndx
,
309 unsigned int reloc_type
);
311 // Return the number of FDEs.
315 // Set the final data size.
317 do_set_address(uint64_t, off_t
);
319 // Return the output address for an input address.
321 do_output_offset(const Relobj
*, unsigned int shndx
, off_t offset
,
322 off_t
* poutput
) const;
324 // Write the data to the file.
326 do_write(Output_file
*);
329 // The comparison routine for the CIE map.
333 operator()(const Cie
* cie1
, const Cie
* cie2
) const
334 { return *cie1
< *cie2
; }
337 // A mapping from unique CIEs to their offset in the output file.
338 typedef std::map
<Cie
*, uint64_t, Cie_less
> Cie_offsets
;
340 // A list of unmergeable CIEs with their offsets.
341 typedef std::vector
<std::pair
<Cie
*, uint64_t> > Unmergeable_cie_offsets
;
343 // A mapping from offsets to CIEs. This is used while reading an
345 typedef std::map
<uint64_t, Cie
*> Offsets_to_cie
;
347 // A list of CIEs, and a bool indicating whether the CIE is
349 typedef std::vector
<std::pair
<Cie
*, bool> > New_cies
;
353 skip_leb128(const unsigned char**, const unsigned char*);
355 // The implementation of add_ehframe_input_section.
356 template<int size
, bool big_endian
>
358 do_add_ehframe_input_section(Sized_relobj
<size
, big_endian
>* object
,
359 const unsigned char* symbols
,
361 const unsigned char* symbol_names
,
362 off_t symbol_names_size
,
364 unsigned int reloc_shndx
,
365 unsigned int reloc_type
,
366 const unsigned char* pcontents
,
371 template<int size
, bool big_endian
>
373 read_cie(Sized_relobj
<size
, big_endian
>* object
,
375 const unsigned char* symbols
,
377 const unsigned char* symbol_names
,
378 off_t symbol_names_size
,
379 const unsigned char* pcontents
,
380 const unsigned char* pcie
,
381 const unsigned char *pcieend
,
382 Track_relocs
<size
, big_endian
>* relocs
,
383 Offsets_to_cie
* cies
,
387 template<int size
, bool big_endian
>
389 read_fde(Sized_relobj
<size
, big_endian
>* object
,
391 const unsigned char* symbols
,
393 const unsigned char* pcontents
,
395 const unsigned char* pfde
,
396 const unsigned char *pfdeend
,
397 Track_relocs
<size
, big_endian
>* relocs
,
398 Offsets_to_cie
* cies
);
400 // Template version of write function.
401 template<int size
, bool big_endian
>
403 do_sized_write(unsigned char* oview
);
405 // The exception frame header, if any.
406 Eh_frame_hdr
* eh_frame_hdr_
;
407 // A mapping from all unique CIEs to their offset in the output
409 Cie_offsets cie_offsets_
;
410 // A mapping from unmergeable CIEs to their offset in the output
412 Unmergeable_cie_offsets unmergeable_cie_offsets_
;
413 // A mapping from input sections to the output section.
414 Merge_map merge_map_
;
417 } // End namespace gold.
419 #endif // !defined(GOLD_EHFRAME_H)