Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / gold / ehframe.h
blob06d0fdc4bcfedab2114485f3292849b500de78e0
1 // ehframe.h -- handle exception frame sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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
26 #include <map>
27 #include <set>
28 #include <vector>
30 #include "output.h"
31 #include "merge.h"
33 namespace gold
36 template<int size, bool big_endian>
37 class Track_relocs;
39 class Eh_frame;
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
50 // that.
52 class Eh_frame_hdr : public Output_section_data
54 public:
55 Eh_frame_hdr(Output_section* eh_frame_section, const Eh_frame*);
57 // Record that we found an unrecognized .eh_frame section.
58 void
59 found_unrecognized_eh_frame_section()
60 { this->any_unrecognized_eh_frame_sections_ = true; }
62 // Record an FDE.
63 void
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 protected:
71 // Set the final data size.
72 void
73 set_final_data_size();
75 // Write the data to the file.
76 void
77 do_write(Output_file*);
79 // Write to a map file.
80 void
81 do_print_to_mapfile(Mapfile* mapfile) const
82 { mapfile->print_output_data(this, _("** eh_frame_hdr")); }
84 private:
85 // Write the data to the file with the right endianness.
86 template<int size, bool big_endian>
87 void
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.
100 template<int size>
101 class Fde_addresses
103 public:
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)
110 : fde_addresses_()
111 { this->fde_addresses_.reserve(reserve); }
113 void
114 push_back(Address pc_address, Address fde_address)
116 this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address));
119 iterator
120 begin()
121 { return this->fde_addresses_.begin(); }
123 iterator
124 end()
125 { return this->fde_addresses_.end(); }
127 private:
128 Fde_address_list fde_addresses_;
131 // Compare Fde_address objects.
132 template<int size>
133 struct Fde_address_compare
135 bool
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>
150 void
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
162 // process.
163 bool any_unrecognized_eh_frame_sections_;
166 // This class holds an FDE.
168 class Fde
170 public:
171 Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset,
172 const unsigned char* contents, size_t len)
173 : object_(object), shndx_(shndx), input_offset_(input_offset),
174 contents_(reinterpret_cast<const char*>(contents), len)
177 // Return the length of this FDE. Add 4 for the length and 4 for
178 // the offset to the CIE.
179 size_t
180 length() const
181 { return this->contents_.length() + 8; }
183 // Add a mapping for this FDE to MERGE_MAP.
184 void
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(),
189 output_offset);
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
195 // offset.
196 template<int size, bool big_endian>
197 section_offset_type
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);
202 private:
203 // The object in which this FDE was seen.
204 Relobj* object_;
205 // Input section index for this FDE.
206 unsigned int shndx_;
207 // Offset within the input section for this FDE.
208 section_offset_type input_offset_;
209 // FDE data.
210 std::string contents_;
213 // This class holds a CIE.
215 class Cie
217 public:
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)
221 : object_(object),
222 shndx_(shndx),
223 input_offset_(input_offset),
224 fde_encoding_(fde_encoding),
225 personality_name_(personality_name),
226 fdes_(),
227 contents_(reinterpret_cast<const char*>(contents), length)
230 ~Cie();
232 // We permit copying a CIE when there are no FDEs. This is
233 // convenient in the code which creates them.
234 Cie(const Cie& cie)
235 : object_(cie.object_),
236 shndx_(cie.shndx_),
237 input_offset_(cie.input_offset_),
238 fde_encoding_(cie.fde_encoding_),
239 personality_name_(cie.personality_name_),
240 fdes_(),
241 contents_(cie.contents_)
242 { gold_assert(cie.fdes_.empty()); }
244 // Add an FDE associated with this CIE.
245 void
246 add_fde(Fde* fde)
247 { this->fdes_.push_back(fde); }
249 // Return the number of FDEs.
250 unsigned int
251 fde_count() const
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.
258 section_offset_type
259 set_output_offset(section_offset_type output_offset, unsigned int addralign,
260 Merge_map*);
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>
266 section_offset_type
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&);
273 private:
274 // The class is not assignable.
275 Cie& operator=(const Cie&);
277 // The object in which this CIE was first seen.
278 Relobj* object_;
279 // Input section index for this CIE.
280 unsigned int shndx_;
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_;
288 // List of FDEs.
289 std::vector<Fde*> fdes_;
290 // CIE data.
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
302 public:
303 Eh_frame();
305 // Record the associated Eh_frame_hdr, if any.
306 void
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
316 // data.
317 template<int size, bool big_endian>
318 bool
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.
328 unsigned int
329 fde_count() const;
331 protected:
332 // Set the final data size.
333 void
334 set_final_data_size();
336 // Return the output address for an input address.
337 bool
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.
343 bool
344 do_is_merge_section_for(const Relobj*, unsigned int shndx) const;
346 // Write the data to the file.
347 void
348 do_write(Output_file*);
350 // Write to a map file.
351 void
352 do_print_to_mapfile(Mapfile* mapfile) const
353 { mapfile->print_output_data(this, _("** eh_frame")); }
355 private:
356 // The comparison routine for the CIE map.
357 struct Cie_less
359 bool
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
371 // input section.
372 typedef std::map<uint64_t, Cie*> Offsets_to_cie;
374 // A list of CIEs, and a bool indicating whether the CIE is
375 // mergeable.
376 typedef std::vector<std::pair<Cie*, bool> > New_cies;
378 // Skip an LEB128.
379 static bool
380 skip_leb128(const unsigned char**, const unsigned char*);
382 // The implementation of add_ehframe_input_section.
383 template<int size, bool big_endian>
384 bool
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,
390 unsigned int shndx,
391 unsigned int reloc_shndx,
392 unsigned int reloc_type,
393 const unsigned char* pcontents,
394 section_size_type contents_len,
395 New_cies*);
397 // Read a CIE.
398 template<int size, bool big_endian>
399 bool
400 read_cie(Sized_relobj<size, big_endian>* object,
401 unsigned int shndx,
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,
411 New_cies* new_cies);
413 // Read an FDE.
414 template<int size, bool big_endian>
415 bool
416 read_fde(Sized_relobj<size, big_endian>* object,
417 unsigned int shndx,
418 const unsigned char* symbols,
419 section_size_type symbols_size,
420 const unsigned char* pcontents,
421 unsigned int offset,
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>
429 void
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
435 // file.
436 Cie_offsets cie_offsets_;
437 // A mapping from unmergeable CIEs to their offset in the output
438 // file.
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
445 // true.
446 section_size_type final_data_size_;
449 } // End namespace gold.
451 #endif // !defined(GOLD_EHFRAME_H)