1 // ehframe.h -- handle exception frame sections for gold -*- C++ -*-
3 // Copyright (C) 2006-2019 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 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(section_offset_type 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));
67 // Set the final data size.
69 set_final_data_size();
71 // Write the data to the file.
73 do_write(Output_file*);
75 // Write to a map file.
77 do_print_to_mapfile(Mapfile* mapfile) const
78 { mapfile->print_output_data(this, _("** eh_frame_hdr")); }
81 // Write the data to the file with the right endianness.
82 template<int size, bool big_endian>
84 do_sized_write(Output_file*);
86 // The data we record for one FDE: the offset of the FDE within the
87 // .eh_frame section, and the FDE encoding.
88 typedef std::pair<section_offset_type, unsigned char> Fde_offset;
90 // The list of information we record for an FDE.
91 typedef std::vector<Fde_offset> Fde_offsets;
93 // When writing out the header, we convert the FDE offsets into FDE
94 // addresses. This is a list of pairs of the offset from the header
95 // to the FDE PC and to the FDE itself.
100 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
101 typedef typename std::pair<Address, Address> Fde_address;
102 typedef typename std::vector<Fde_address> Fde_address_list;
103 typedef typename Fde_address_list::iterator iterator;
105 Fde_addresses(unsigned int reserve)
107 { this->fde_addresses_.reserve(reserve); }
110 push_back(Address pc_address, Address fde_address)
112 this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address));
117 { return this->fde_addresses_.begin(); }
121 { return this->fde_addresses_.end(); }
124 Fde_address_list fde_addresses_;
127 // Compare Fde_address objects.
129 struct Fde_address_compare
132 operator()(const typename Fde_addresses<size>::Fde_address& f1,
133 const typename Fde_addresses<size>::Fde_address& f2) const
134 { return f1.first < f2.first; }
137 // Return the PC to which an FDE refers.
138 template<int size, bool big_endian>
139 typename elfcpp::Elf_types<size>::Elf_Addr
140 get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
141 const unsigned char* eh_frame_contents,
142 section_offset_type fde_offset, unsigned char fde_encoding);
144 // Convert Fde_offsets to Fde_addresses.
145 template<int size, bool big_endian>
147 get_fde_addresses(Output_file* of,
148 const Fde_offsets* fde_offsets,
149 Fde_addresses<size>* fde_addresses);
151 // The .eh_frame section.
152 Output_section* eh_frame_section_;
153 // The .eh_frame section data.
154 const Eh_frame* eh_frame_data_;
155 // Data from the FDEs in the .eh_frame sections.
156 Fde_offsets fde_offsets_;
157 // Whether we found any .eh_frame sections which we could not
159 bool any_unrecognized_eh_frame_sections_;
162 // This class holds an FDE.
167 Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset,
168 const unsigned char* contents, size_t length)
170 contents_(reinterpret_cast<const char*>(contents), length)
172 this->u_.from_object.shndx = shndx;
173 this->u_.from_object.input_offset = input_offset;
176 // Create an FDE associated with a PLT.
177 Fde(Output_data* plt, const unsigned char* contents, size_t length,
180 contents_(reinterpret_cast<const char*>(contents), length)
182 this->u_.from_linker.plt = plt;
183 this->u_.from_linker.post_map = post_map;
186 // Return the length of this FDE. Add 4 for the length and 4 for
187 // the offset to the CIE.
190 { return this->contents_.length() + 8; }
192 // Add a mapping for this FDE to MERGE_MAP, so that relocations
193 // against the FDE are applied to right part of the output file.
195 add_mapping(section_offset_type output_offset,
196 Output_section_data* output_data) const
198 if (this->object_ != NULL)
199 this->object_->add_merge_mapping(output_data, this->u_.from_object.shndx,
200 this->u_.from_object.input_offset, this->length(),
204 // Return whether this FDE was added after merge mapping.
207 { return this->object_ == NULL && this->u_.from_linker.post_map; }
209 // Return whether this FDE was added for the PLT after merge mapping.
211 post_map(const Output_data* plt) const
212 { return this->post_map() && this->u_.from_linker.plt == plt; }
214 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
215 // encoding, from the CIE. Round up the bytes to ADDRALIGN if
216 // necessary. ADDRESS is the virtual address of OVIEW. Record the
217 // FDE in EH_FRAME_HDR. Return the new offset.
218 template<int size, bool big_endian>
220 write(unsigned char* oview, section_offset_type output_section_offset,
221 section_offset_type offset, uint64_t address, unsigned int addralign,
222 section_offset_type cie_offset, unsigned char fde_encoding,
223 Eh_frame_hdr* eh_frame_hdr);
226 // The object in which this FDE was seen. This will be NULL for a
227 // linker generated FDE.
231 // These fields are used if the FDE is from an input object (the
232 // object_ field is not NULL).
235 // Input section index for this FDE.
237 // Offset within the input section for this FDE.
238 section_offset_type input_offset;
240 // This field is used if the FDE is generated by the linker (the
241 // object_ field is NULL).
244 // The only linker generated FDEs are for PLT sections, and this
245 // points to the PLT section.
247 // Set if the FDE was added after merge mapping.
252 std::string contents_;
255 // A FDE plus some info from a CIE to allow later writing of the FDE.
259 Post_fde(Fde* f, section_offset_type cie_off, unsigned char encoding)
260 : fde(f), cie_offset(cie_off), fde_encoding(encoding)
264 section_offset_type cie_offset;
265 unsigned char fde_encoding;
268 typedef std::vector<Post_fde> Post_fdes;
270 // This class holds a CIE.
275 Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset,
276 unsigned char fde_encoding, const char* personality_name,
277 const unsigned char* contents, size_t length)
280 input_offset_(input_offset),
281 fde_encoding_(fde_encoding),
282 personality_name_(personality_name),
284 contents_(reinterpret_cast<const char*>(contents), length)
289 // We permit copying a CIE when there are no FDEs. This is
290 // convenient in the code which creates them.
292 : object_(cie.object_),
294 input_offset_(cie.input_offset_),
295 fde_encoding_(cie.fde_encoding_),
296 personality_name_(cie.personality_name_),
298 contents_(cie.contents_)
299 { gold_assert(cie.fdes_.empty()); }
301 // Add an FDE associated with this CIE.
304 { this->fdes_.push_back(fde); }
306 // Remove the last FDE associated with this CIE.
309 { this->fdes_.pop_back(); }
311 // Access the last FDE associated with this CIE.
314 { return this->fdes_.back(); }
316 // Return the number of FDEs.
319 { return this->fdes_.size(); }
321 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
322 // followed by all its FDEs. ADDRALIGN is the required address
323 // alignment, typically 4 or 8. This updates MERGE_MAP with the
324 // mapping. It returns the new output offset.
326 set_output_offset(section_offset_type output_offset, unsigned int addralign,
327 Output_section_data*);
329 // Write the CIE to OVIEW starting at OFFSET. Round up the bytes to
330 // ADDRALIGN. ADDRESS is the virtual address of OVIEW.
331 // EH_FRAME_HDR is the exception frame header for FDE recording.
332 // POST_FDES stashes FDEs created after mappings were done, for later
333 // writing. Return the new offset.
334 template<int size, bool big_endian>
336 write(unsigned char* oview, section_offset_type output_section_offset,
337 section_offset_type offset, uint64_t address,
338 unsigned int addralign, Eh_frame_hdr* eh_frame_hdr,
339 Post_fdes* post_fdes);
341 // Return the FDE encoding.
344 { return this->fde_encoding_; }
346 friend bool operator<(const Cie&, const Cie&);
347 friend bool operator==(const Cie&, const Cie&);
350 // The class is not assignable.
351 Cie& operator=(const Cie&);
353 // The object in which this CIE was first seen. This will be NULL
354 // for a linker generated CIE.
356 // Input section index for this CIE. This will be 0 for a linker
359 // Offset within the input section for this CIE. This will be 0 for
360 // a linker generated CIE.
361 section_offset_type input_offset_;
362 // The encoding of the FDE. This is a DW_EH_PE code.
363 unsigned char fde_encoding_;
364 // The name of the personality routine. This will be the name of a
365 // global symbol, or will be the empty string.
366 std::string personality_name_;
368 std::vector<Fde*> fdes_;
370 std::string contents_;
373 extern bool operator<(const Cie&, const Cie&);
374 extern bool operator==(const Cie&, const Cie&);
376 // This class manages .eh_frame sections. It discards duplicate
377 // exception information.
379 class Eh_frame : public Output_section_data
382 enum Eh_frame_section_disposition
385 EH_UNRECOGNIZED_SECTION,
386 EH_OPTIMIZABLE_SECTION,
387 EH_END_MARKER_SECTION
392 // Record the associated Eh_frame_hdr, if any.
394 set_eh_frame_hdr(Eh_frame_hdr* hdr)
395 { this->eh_frame_hdr_ = hdr; }
397 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
398 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
399 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
400 // is the relocation section if any (0 for none, -1U for multiple).
401 // RELOC_TYPE is the type of the relocation section if any. This
402 // returns whether the section was incorporated into the .eh_frame
404 template<int size, bool big_endian>
405 Eh_frame_section_disposition
406 add_ehframe_input_section(Sized_relobj_file<size, big_endian>* object,
407 const unsigned char* symbols,
408 section_size_type symbols_size,
409 const unsigned char* symbol_names,
410 section_size_type symbol_names_size,
411 unsigned int shndx, unsigned int reloc_shndx,
412 unsigned int reloc_type);
414 // Add a CIE and an FDE for a PLT section, to permit unwinding
415 // through a PLT. The FDE data should start with 8 bytes of zero,
416 // which will be replaced by a 4 byte PC relative reference to the
417 // address of PLT and a 4 byte size of PLT.
419 add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data,
420 size_t cie_length, const unsigned char* fde_data,
423 // Remove all post-map unwind information for a PLT.
425 remove_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data,
428 // Return the number of FDEs.
433 // Set the final data size.
435 set_final_data_size();
437 // Return the output address for an input address.
439 do_output_offset(const Relobj*, unsigned int shndx,
440 section_offset_type offset,
441 section_offset_type* poutput) const;
443 // Write the data to the file.
445 do_write(Output_file*);
447 // Write to a map file.
449 do_print_to_mapfile(Mapfile* mapfile) const
450 { mapfile->print_output_data(this, _("** eh_frame")); }
453 // The comparison routine for the CIE map.
457 operator()(const Cie* cie1, const Cie* cie2) const
458 { return *cie1 < *cie2; }
461 // A set of unique CIEs.
462 typedef std::set<Cie*, Cie_less> Cie_offsets;
464 // A list of unmergeable CIEs.
465 typedef std::vector<Cie*> Unmergeable_cie_offsets;
467 // A mapping from offsets to CIEs. This is used while reading an
469 typedef std::map<uint64_t, Cie*> Offsets_to_cie;
471 // A list of CIEs, and a bool indicating whether the CIE is
473 typedef std::vector<std::pair<Cie*, bool> > New_cies;
477 skip_leb128(const unsigned char**, const unsigned char*);
479 // The implementation of add_ehframe_input_section.
480 template<int size, bool big_endian>
482 do_add_ehframe_input_section(Sized_relobj_file<size, big_endian>* object,
483 const unsigned char* symbols,
484 section_size_type symbols_size,
485 const unsigned char* symbol_names,
486 section_size_type symbol_names_size,
488 unsigned int reloc_shndx,
489 unsigned int reloc_type,
490 const unsigned char* pcontents,
491 section_size_type contents_len,
495 template<int size, bool big_endian>
497 read_cie(Sized_relobj_file<size, big_endian>* object,
499 const unsigned char* symbols,
500 section_size_type symbols_size,
501 const unsigned char* symbol_names,
502 section_size_type symbol_names_size,
503 const unsigned char* pcontents,
504 const unsigned char* pcie,
505 const unsigned char* pcieend,
506 Track_relocs<size, big_endian>* relocs,
507 Offsets_to_cie* cies,
511 template<int size, bool big_endian>
513 read_fde(Sized_relobj_file<size, big_endian>* object,
515 const unsigned char* symbols,
516 section_size_type symbols_size,
517 const unsigned char* pcontents,
519 const unsigned char* pfde,
520 const unsigned char* pfdeend,
521 Track_relocs<size, big_endian>* relocs,
522 Offsets_to_cie* cies);
524 // Template version of write function.
525 template<int size, bool big_endian>
527 do_sized_write(unsigned char* oview);
529 // The exception frame header, if any.
530 Eh_frame_hdr* eh_frame_hdr_;
531 // A mapping from all unique CIEs to their offset in the output
533 Cie_offsets cie_offsets_;
534 // A mapping from unmergeable CIEs to their offset in the output
536 Unmergeable_cie_offsets unmergeable_cie_offsets_;
537 // Whether we have created the mappings to the output section.
538 bool mappings_are_done_;
539 // The final data size. This is only set if mappings_are_done_ is
541 section_size_type final_data_size_;
544 } // End namespace gold.
546 #endif // !defined(GOLD_EHFRAME_H)