d5ac668e417faf423b8feddca5d3cf33705119b5
[deliverable/binutils-gdb.git] / gold / ehframe.h
1 // ehframe.h -- handle exception frame sections for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_EHFRAME_H
24 #define GOLD_EHFRAME_H
25
26 #include <map>
27 #include <set>
28 #include <vector>
29
30 #include "output.h"
31 #include "merge.h"
32
33 namespace gold
34 {
35
36 template<int size, bool big_endian>
37 class Track_relocs;
38
39 class Eh_frame;
40
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.
47
48 class Eh_frame_hdr : public Output_section_data
49 {
50 public:
51 Eh_frame_hdr(Output_section* eh_frame_section, const Eh_frame*);
52
53 // Record that we found an unrecognized .eh_frame section.
54 void
55 found_unrecognized_eh_frame_section()
56 { this->any_unrecognized_eh_frame_sections_ = true; }
57
58 // Record an FDE.
59 void
60 record_fde(section_offset_type fde_offset, unsigned char fde_encoding)
61 {
62 if (!this->any_unrecognized_eh_frame_sections_)
63 {
64 Hold_lock(*this->lock_);
65 this->fde_offsets_.push_back(std::make_pair(fde_offset, fde_encoding));
66 }
67 }
68
69 protected:
70 // Set the final data size.
71 void
72 set_final_data_size();
73
74 // Write the data to the file.
75 void
76 do_write(Output_file*);
77
78 // Write to a map file.
79 void
80 do_print_to_mapfile(Mapfile* mapfile) const
81 { mapfile->print_output_data(this, _("** eh_frame_hdr")); }
82
83 private:
84 // Write the data to the file with the right endianness.
85 template<int size, bool big_endian>
86 void
87 do_sized_write(Output_file*);
88
89 // The data we record for one FDE: the offset of the FDE within the
90 // .eh_frame section, and the FDE encoding.
91 typedef std::pair<section_offset_type, unsigned char> Fde_offset;
92
93 // The list of information we record for an FDE.
94 typedef std::vector<Fde_offset> Fde_offsets;
95
96 // When writing out the header, we convert the FDE offsets into FDE
97 // addresses. This is a list of pairs of the offset from the header
98 // to the FDE PC and to the FDE itself.
99 template<int size>
100 class Fde_addresses
101 {
102 public:
103 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
104 typedef typename std::pair<Address, Address> Fde_address;
105 typedef typename std::vector<Fde_address> Fde_address_list;
106 typedef typename Fde_address_list::iterator iterator;
107
108 Fde_addresses(unsigned int reserve)
109 : fde_addresses_()
110 { this->fde_addresses_.reserve(reserve); }
111
112 void
113 push_back(Address pc_address, Address fde_address)
114 {
115 this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address));
116 }
117
118 iterator
119 begin()
120 { return this->fde_addresses_.begin(); }
121
122 iterator
123 end()
124 { return this->fde_addresses_.end(); }
125
126 private:
127 Fde_address_list fde_addresses_;
128 };
129
130 // Compare Fde_address objects.
131 template<int size>
132 struct Fde_address_compare
133 {
134 bool
135 operator()(const typename Fde_addresses<size>::Fde_address& f1,
136 const typename Fde_addresses<size>::Fde_address& f2) const
137 { return f1.first < f2.first; }
138 };
139
140 // Return the PC to which an FDE refers.
141 template<int size, bool big_endian>
142 typename elfcpp::Elf_types<size>::Elf_Addr
143 get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
144 const unsigned char* eh_frame_contents,
145 section_offset_type fde_offset, unsigned char fde_encoding);
146
147 // Convert Fde_offsets to Fde_addresses.
148 template<int size, bool big_endian>
149 void
150 get_fde_addresses(Output_file* of,
151 const Fde_offsets* fde_offsets,
152 Fde_addresses<size>* fde_addresses);
153
154 // The .eh_frame section.
155 Output_section* eh_frame_section_;
156 // The .eh_frame section data.
157 const Eh_frame* eh_frame_data_;
158 // Data from the FDEs in the .eh_frame sections.
159 Fde_offsets fde_offsets_;
160 // Whether we found any .eh_frame sections which we could not
161 // process.
162 bool any_unrecognized_eh_frame_sections_;
163 // Lock held while updating fde_offsets_.
164 Lock* lock_;
165 };
166
167 // This class holds an FDE.
168
169 class Fde
170 {
171 public:
172 Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset,
173 const unsigned char* contents, size_t length)
174 : object_(object),
175 contents_(reinterpret_cast<const char*>(contents), length)
176 {
177 this->u_.from_object.shndx = shndx;
178 this->u_.from_object.input_offset = input_offset;
179 }
180
181 // Create an FDE associated with a PLT.
182 Fde(Output_data* plt, const unsigned char* contents, size_t length,
183 bool post_map)
184 : object_(NULL),
185 contents_(reinterpret_cast<const char*>(contents), length)
186 {
187 this->u_.from_linker.plt = plt;
188 this->u_.from_linker.post_map = post_map;
189 }
190
191 // Return the length of this FDE. Add 4 for the length and 4 for
192 // the offset to the CIE.
193 size_t
194 length() const
195 { return this->contents_.length() + 8; }
196
197 // Add a mapping for this FDE to MERGE_MAP, so that relocations
198 // against the FDE are applied to right part of the output file.
199 void
200 add_mapping(section_offset_type output_offset, Merge_map* merge_map) const
201 {
202 if (this->object_ != NULL)
203 merge_map->add_mapping(this->object_, this->u_.from_object.shndx,
204 this->u_.from_object.input_offset, this->length(),
205 output_offset);
206 }
207
208 // Return whether this FDE was added after merge mapping.
209 bool
210 post_map()
211 { return this->object_ == NULL && this->u_.from_linker.post_map; }
212
213 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
214 // encoding, from the CIE. Round up the bytes to ADDRALIGN if
215 // necessary. ADDRESS is the virtual address of OVIEW. Record the
216 // FDE in EH_FRAME_HDR. Return the new offset.
217 template<int size, bool big_endian>
218 section_offset_type
219 write(unsigned char* oview, section_offset_type offset,
220 uint64_t address, unsigned int addralign,
221 section_offset_type cie_offset, unsigned char fde_encoding,
222 Eh_frame_hdr* eh_frame_hdr);
223
224 private:
225 // The object in which this FDE was seen. This will be NULL for a
226 // linker generated FDE.
227 Relobj* object_;
228 union
229 {
230 // These fields are used if the FDE is from an input object (the
231 // object_ field is not NULL).
232 struct
233 {
234 // Input section index for this FDE.
235 unsigned int shndx;
236 // Offset within the input section for this FDE.
237 section_offset_type input_offset;
238 } from_object;
239 // This field is used if the FDE is generated by the linker (the
240 // object_ field is NULL).
241 struct
242 {
243 // The only linker generated FDEs are for PLT sections, and this
244 // points to the PLT section.
245 Output_data* plt;
246 // Set if the FDE was added after merge mapping.
247 bool post_map;
248 } from_linker;
249 } u_;
250 // FDE data.
251 std::string contents_;
252 };
253
254 // A FDE plus some info from a CIE to allow later writing of the FDE.
255
256 struct Post_fde
257 {
258 Post_fde(Fde* f, section_offset_type cie_off, unsigned char encoding)
259 : fde(f), cie_offset(cie_off), fde_encoding(encoding)
260 { }
261
262 Fde* fde;
263 section_offset_type cie_offset;
264 unsigned char fde_encoding;
265 };
266
267 typedef std::vector<Post_fde> Post_fdes;
268
269 // This class holds a CIE.
270
271 class Cie
272 {
273 public:
274 Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset,
275 unsigned char fde_encoding, const char* personality_name,
276 const unsigned char* contents, size_t length)
277 : object_(object),
278 shndx_(shndx),
279 input_offset_(input_offset),
280 fde_encoding_(fde_encoding),
281 personality_name_(personality_name),
282 fdes_(),
283 contents_(reinterpret_cast<const char*>(contents), length)
284 { }
285
286 ~Cie();
287
288 // We permit copying a CIE when there are no FDEs. This is
289 // convenient in the code which creates them.
290 Cie(const Cie& cie)
291 : object_(cie.object_),
292 shndx_(cie.shndx_),
293 input_offset_(cie.input_offset_),
294 fde_encoding_(cie.fde_encoding_),
295 personality_name_(cie.personality_name_),
296 fdes_(),
297 contents_(cie.contents_)
298 { gold_assert(cie.fdes_.empty()); }
299
300 // Add an FDE associated with this CIE.
301 void
302 add_fde(Fde* fde)
303 { this->fdes_.push_back(fde); }
304
305 // Return the number of FDEs.
306 unsigned int
307 fde_count() const
308 { return this->fdes_.size(); }
309
310 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
311 // followed by all its FDEs. ADDRALIGN is the required address
312 // alignment, typically 4 or 8. This updates MERGE_MAP with the
313 // mapping. It returns the new output offset.
314 section_offset_type
315 set_output_offset(section_offset_type output_offset, unsigned int addralign,
316 Merge_map*);
317
318 // Write the CIE to OVIEW starting at OFFSET. Round up the bytes to
319 // ADDRALIGN. ADDRESS is the virtual address of OVIEW.
320 // EH_FRAME_HDR is the exception frame header for FDE recording.
321 // POST_FDES stashes FDEs created after mappings were done, for later
322 // writing. Return the new offset.
323 template<int size, bool big_endian>
324 section_offset_type
325 write(unsigned char* oview, section_offset_type offset, uint64_t address,
326 unsigned int addralign, Eh_frame_hdr* eh_frame_hdr,
327 Post_fdes* post_fdes);
328
329 friend bool operator<(const Cie&, const Cie&);
330 friend bool operator==(const Cie&, const Cie&);
331
332 private:
333 // The class is not assignable.
334 Cie& operator=(const Cie&);
335
336 // The object in which this CIE was first seen. This will be NULL
337 // for a linker generated CIE.
338 Relobj* object_;
339 // Input section index for this CIE. This will be 0 for a linker
340 // generated CIE.
341 unsigned int shndx_;
342 // Offset within the input section for this CIE. This will be 0 for
343 // a linker generated CIE.
344 section_offset_type input_offset_;
345 // The encoding of the FDE. This is a DW_EH_PE code.
346 unsigned char fde_encoding_;
347 // The name of the personality routine. This will be the name of a
348 // global symbol, or will be the empty string.
349 std::string personality_name_;
350 // List of FDEs.
351 std::vector<Fde*> fdes_;
352 // CIE data.
353 std::string contents_;
354 };
355
356 extern bool operator<(const Cie&, const Cie&);
357 extern bool operator==(const Cie&, const Cie&);
358
359 // This class manages .eh_frame sections. It discards duplicate
360 // exception information.
361
362 class Eh_frame : public Output_section_data
363 {
364 public:
365 Eh_frame();
366
367 // Record the associated Eh_frame_hdr, if any.
368 void
369 set_eh_frame_hdr(Eh_frame_hdr* hdr)
370 { this->eh_frame_hdr_ = hdr; }
371
372 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
373 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
374 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
375 // is the relocation section if any (0 for none, -1U for multiple).
376 // RELOC_TYPE is the type of the relocation section if any. This
377 // returns whether the section was incorporated into the .eh_frame
378 // data.
379 template<int size, bool big_endian>
380 bool
381 add_ehframe_input_section(Sized_relobj_file<size, big_endian>* object,
382 const unsigned char* symbols,
383 section_size_type symbols_size,
384 const unsigned char* symbol_names,
385 section_size_type symbol_names_size,
386 unsigned int shndx, unsigned int reloc_shndx,
387 unsigned int reloc_type);
388
389 // Add a CIE and an FDE for a PLT section, to permit unwinding
390 // through a PLT. The FDE data should start with 8 bytes of zero,
391 // which will be replaced by a 4 byte PC relative reference to the
392 // address of PLT and a 4 byte size of PLT.
393 void
394 add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data,
395 size_t cie_length, const unsigned char* fde_data,
396 size_t fde_length);
397
398 // Return the number of FDEs.
399 unsigned int
400 fde_count() const;
401
402 protected:
403 // Set the final data size.
404 void
405 set_final_data_size();
406
407 // Return the output address for an input address.
408 bool
409 do_output_offset(const Relobj*, unsigned int shndx,
410 section_offset_type offset,
411 section_offset_type* poutput) const;
412
413 // Return whether this is the merge section for an input section.
414 bool
415 do_is_merge_section_for(const Relobj*, unsigned int shndx) const;
416
417 // Write the data to the file.
418 void
419 do_write(Output_file*);
420
421 // Write to a map file.
422 void
423 do_print_to_mapfile(Mapfile* mapfile) const
424 { mapfile->print_output_data(this, _("** eh_frame")); }
425
426 private:
427 // The comparison routine for the CIE map.
428 struct Cie_less
429 {
430 bool
431 operator()(const Cie* cie1, const Cie* cie2) const
432 { return *cie1 < *cie2; }
433 };
434
435 // A set of unique CIEs.
436 typedef std::set<Cie*, Cie_less> Cie_offsets;
437
438 // A list of unmergeable CIEs.
439 typedef std::vector<Cie*> Unmergeable_cie_offsets;
440
441 // A mapping from offsets to CIEs. This is used while reading an
442 // input section.
443 typedef std::map<uint64_t, Cie*> Offsets_to_cie;
444
445 // A list of CIEs, and a bool indicating whether the CIE is
446 // mergeable.
447 typedef std::vector<std::pair<Cie*, bool> > New_cies;
448
449 // Skip an LEB128.
450 static bool
451 skip_leb128(const unsigned char**, const unsigned char*);
452
453 // The implementation of add_ehframe_input_section.
454 template<int size, bool big_endian>
455 bool
456 do_add_ehframe_input_section(Sized_relobj_file<size, big_endian>* object,
457 const unsigned char* symbols,
458 section_size_type symbols_size,
459 const unsigned char* symbol_names,
460 section_size_type symbol_names_size,
461 unsigned int shndx,
462 unsigned int reloc_shndx,
463 unsigned int reloc_type,
464 const unsigned char* pcontents,
465 section_size_type contents_len,
466 New_cies*);
467
468 // Read a CIE.
469 template<int size, bool big_endian>
470 bool
471 read_cie(Sized_relobj_file<size, big_endian>* object,
472 unsigned int shndx,
473 const unsigned char* symbols,
474 section_size_type symbols_size,
475 const unsigned char* symbol_names,
476 section_size_type symbol_names_size,
477 const unsigned char* pcontents,
478 const unsigned char* pcie,
479 const unsigned char* pcieend,
480 Track_relocs<size, big_endian>* relocs,
481 Offsets_to_cie* cies,
482 New_cies* new_cies);
483
484 // Read an FDE.
485 template<int size, bool big_endian>
486 bool
487 read_fde(Sized_relobj_file<size, big_endian>* object,
488 unsigned int shndx,
489 const unsigned char* symbols,
490 section_size_type symbols_size,
491 const unsigned char* pcontents,
492 unsigned int offset,
493 const unsigned char* pfde,
494 const unsigned char* pfdeend,
495 Track_relocs<size, big_endian>* relocs,
496 Offsets_to_cie* cies);
497
498 // Template version of write function.
499 template<int size, bool big_endian>
500 void
501 do_sized_write(unsigned char* oview);
502
503 // The exception frame header, if any.
504 Eh_frame_hdr* eh_frame_hdr_;
505 // A mapping from all unique CIEs to their offset in the output
506 // file.
507 Cie_offsets cie_offsets_;
508 // A mapping from unmergeable CIEs to their offset in the output
509 // file.
510 Unmergeable_cie_offsets unmergeable_cie_offsets_;
511 // A mapping from input sections to the output section.
512 Merge_map merge_map_;
513 // Whether we have created the mappings to the output section.
514 bool mappings_are_done_;
515 // The final data size. This is only set if mappings_are_done_ is
516 // true.
517 section_size_type final_data_size_;
518 };
519
520 } // End namespace gold.
521
522 #endif // !defined(GOLD_EHFRAME_H)
This page took 0.038901 seconds and 4 git commands to generate.