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. Round up the bytes to ADDRALIGN if
188 // necessary. Record the FDE in EH_FRAME_HDR. Return the new
190 template<int size, bool big_endian>
192 write(unsigned char* oview, section_offset_type offset,
193 unsigned int addralign, section_offset_type cie_offset,
194 unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr);
197 // The object in which this FDE was seen.
199 // Input section index for this FDE.
201 // Offset within the input section for this FDE.
202 section_offset_type input_offset_;
204 std::string contents_;
207 // This class holds a CIE.
212 Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset,
213 unsigned char fde_encoding, const char* personality_name,
214 const unsigned char* contents, size_t length)
217 input_offset_(input_offset),
218 fde_encoding_(fde_encoding),
219 personality_name_(personality_name),
221 contents_(reinterpret_cast<const char*>(contents), length)
226 // We permit copying a CIE when there are no FDEs. This is
227 // convenient in the code which creates them.
229 : object_(cie.object_),
231 input_offset_(cie.input_offset_),
232 fde_encoding_(cie.fde_encoding_),
233 personality_name_(cie.personality_name_),
235 contents_(cie.contents_)
236 { gold_assert(cie.fdes_.empty()); }
238 // Add an FDE associated with this CIE.
241 { this->fdes_.push_back(fde); }
243 // Return the number of FDEs.
246 { return this->fdes_.size(); }
248 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
249 // followed by all its FDEs. ADDRALIGN is the required address
250 // alignment, typically 4 or 8. This updates MERGE_MAP with the
251 // mapping. It returns the new output offset.
253 set_output_offset(section_offset_type output_offset, unsigned int addralign,
256 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
257 // exception frame header for FDE recording. Round up the bytes to
258 // ADDRALIGN. Return the new offset.
259 template<int size, bool big_endian>
261 write(unsigned char* oview, section_offset_type offset,
262 unsigned int addralign, Eh_frame_hdr* eh_frame_hdr);
264 friend bool operator<(const Cie&, const Cie&);
265 friend bool operator==(const Cie&, const Cie&);
268 // The class is not assignable.
269 Cie& operator=(const Cie&);
271 // The object in which this CIE was first seen.
273 // Input section index for this CIE.
275 // Offset within the input section for this CIE.
276 section_offset_type input_offset_;
277 // The encoding of the FDE. This is a DW_EH_PE code.
278 unsigned char fde_encoding_;
279 // The name of the personality routine. This will be the name of a
280 // global symbol, or will be the empty string.
281 std::string personality_name_;
283 std::vector<Fde*> fdes_;
285 std::string contents_;
288 extern bool operator<(const Cie&, const Cie&);
289 extern bool operator==(const Cie&, const Cie&);
291 // This class manages .eh_frame sections. It discards duplicate
292 // exception information.
294 class Eh_frame : public Output_section_data
299 // Record the associated Eh_frame_hdr, if any.
301 set_eh_frame_hdr(Eh_frame_hdr* hdr)
302 { this->eh_frame_hdr_ = hdr; }
304 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
305 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
306 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
307 // is the relocation section if any (0 for none, -1U for multiple).
308 // RELOC_TYPE is the type of the relocation section if any. This
309 // returns whether the section was incorporated into the .eh_frame
311 template<int size, bool big_endian>
313 add_ehframe_input_section(Sized_relobj<size, big_endian>* object,
314 const unsigned char* symbols,
315 section_size_type symbols_size,
316 const unsigned char* symbol_names,
317 section_size_type symbol_names_size,
318 unsigned int shndx, unsigned int reloc_shndx,
319 unsigned int reloc_type);
321 // Return the number of FDEs.
325 // Set the final data size.
327 set_final_data_size();
329 // Return the output address for an input address.
331 do_output_offset(const Relobj*, unsigned int shndx,
332 section_offset_type offset,
333 section_offset_type* poutput) const;
335 // Return whether this is the merge section for an input section.
337 do_is_merge_section_for(const Relobj*, unsigned int shndx) const;
339 // Write the data to the file.
341 do_write(Output_file*);
344 // The comparison routine for the CIE map.
348 operator()(const Cie* cie1, const Cie* cie2) const
349 { return *cie1 < *cie2; }
352 // A set of unique CIEs.
353 typedef std::set<Cie*, Cie_less> Cie_offsets;
355 // A list of unmergeable CIEs.
356 typedef std::vector<Cie*> Unmergeable_cie_offsets;
358 // A mapping from offsets to CIEs. This is used while reading an
360 typedef std::map<uint64_t, Cie*> Offsets_to_cie;
362 // A list of CIEs, and a bool indicating whether the CIE is
364 typedef std::vector<std::pair<Cie*, bool> > New_cies;
368 skip_leb128(const unsigned char**, const unsigned char*);
370 // The implementation of add_ehframe_input_section.
371 template<int size, bool big_endian>
373 do_add_ehframe_input_section(Sized_relobj<size, big_endian>* object,
374 const unsigned char* symbols,
375 section_size_type symbols_size,
376 const unsigned char* symbol_names,
377 section_size_type symbol_names_size,
379 unsigned int reloc_shndx,
380 unsigned int reloc_type,
381 const unsigned char* pcontents,
382 section_size_type contents_len,
386 template<int size, bool big_endian>
388 read_cie(Sized_relobj<size, big_endian>* object,
390 const unsigned char* symbols,
391 section_size_type symbols_size,
392 const unsigned char* symbol_names,
393 section_size_type symbol_names_size,
394 const unsigned char* pcontents,
395 const unsigned char* pcie,
396 const unsigned char *pcieend,
397 Track_relocs<size, big_endian>* relocs,
398 Offsets_to_cie* cies,
402 template<int size, bool big_endian>
404 read_fde(Sized_relobj<size, big_endian>* object,
406 const unsigned char* symbols,
407 section_size_type symbols_size,
408 const unsigned char* pcontents,
410 const unsigned char* pfde,
411 const unsigned char *pfdeend,
412 Track_relocs<size, big_endian>* relocs,
413 Offsets_to_cie* cies);
415 // Template version of write function.
416 template<int size, bool big_endian>
418 do_sized_write(unsigned char* oview);
420 // The exception frame header, if any.
421 Eh_frame_hdr* eh_frame_hdr_;
422 // A mapping from all unique CIEs to their offset in the output
424 Cie_offsets cie_offsets_;
425 // A mapping from unmergeable CIEs to their offset in the output
427 Unmergeable_cie_offsets unmergeable_cie_offsets_;
428 // A mapping from input sections to the output section.
429 Merge_map merge_map_;
432 } // End namespace gold.
434 #endif // !defined(GOLD_EHFRAME_H)