* ld-elf/sec-to-seg.exp: New test: Checks the assignment of
[deliverable/binutils-gdb.git] / gold / ehframe.h
CommitLineData
3151305a
ILT
1// ehframe.h -- handle exception frame sections for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
3151305a
ILT
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
1cac254c
ILT
26#include <map>
27#include <set>
28#include <vector>
29
3151305a 30#include "output.h"
730cdc88 31#include "merge.h"
3151305a
ILT
32
33namespace gold
34{
35
730cdc88
ILT
36template<int size, bool big_endian>
37class Track_relocs;
38
39class Eh_frame;
40
3151305a
ILT
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// 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.
51
52class Eh_frame_hdr : public Output_section_data
53{
54 public:
730cdc88
ILT
55 Eh_frame_hdr(Output_section* eh_frame_section, const Eh_frame*);
56
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; }
61
62 // Record an FDE.
63 void
8383303e 64 record_fde(section_offset_type fde_offset, unsigned char fde_encoding)
730cdc88
ILT
65 {
66 if (!this->any_unrecognized_eh_frame_sections_)
67 this->fde_offsets_.push_back(std::make_pair(fde_offset, fde_encoding));
68 }
3151305a 69
7d9e3d98 70 protected:
3151305a
ILT
71 // Set the final data size.
72 void
27bc2bce 73 set_final_data_size();
3151305a
ILT
74
75 // Write the data to the file.
76 void
77 do_write(Output_file*);
78
7d9e3d98
ILT
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")); }
83
3151305a 84 private:
730cdc88
ILT
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*);
89
90 // The data we record for one FDE: the offset of the FDE within the
91 // .eh_frame section, and the FDE encoding.
8383303e 92 typedef std::pair<section_offset_type, unsigned char> Fde_offset;
730cdc88
ILT
93
94 // The list of information we record for an FDE.
95 typedef std::vector<Fde_offset> Fde_offsets;
96
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
102 {
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;
108
109 Fde_addresses(unsigned int reserve)
110 : fde_addresses_()
111 { this->fde_addresses_.reserve(reserve); }
112
113 void
114 push_back(Address pc_address, Address fde_address)
115 {
116 this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address));
117 }
118
119 iterator
120 begin()
121 { return this->fde_addresses_.begin(); }
122
123 iterator
124 end()
125 { return this->fde_addresses_.end(); }
126
127 private:
128 Fde_address_list fde_addresses_;
129 };
130
131 // Compare Fde_address objects.
132 template<int size>
133 struct Fde_address_compare
134 {
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; }
139 };
140
141 // Return the PC to which an FDE refers.
142 template<int size, bool big_endian>
143 typename elfcpp::Elf_types<size>::Elf_Addr
4117d768
ILT
144 get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
145 const unsigned char* eh_frame_contents,
8383303e 146 section_offset_type fde_offset, unsigned char fde_encoding);
730cdc88
ILT
147
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);
154
3151305a
ILT
155 // The .eh_frame section.
156 Output_section* eh_frame_section_;
730cdc88
ILT
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_;
164};
165
166// This class holds an FDE.
167
168class Fde
169{
170 public:
8383303e 171 Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset,
730cdc88
ILT
172 const unsigned char* contents, size_t length)
173 : object_(object), shndx_(shndx), input_offset_(input_offset),
174 contents_(reinterpret_cast<const char*>(contents), length)
175 { }
176
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; }
182
183 // Add a mapping for this FDE to MERGE_MAP.
184 void
8383303e 185 add_mapping(section_offset_type output_offset, Merge_map* merge_map) const
730cdc88
ILT
186 {
187 merge_map->add_mapping(this->object_, this->shndx_,
188 this->input_offset_, this->length(),
189 output_offset);
190 }
191
192 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
935e8877
ILT
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.
730cdc88 196 template<int size, bool big_endian>
8383303e
ILT
197 section_offset_type
198 write(unsigned char* oview, section_offset_type offset,
935e8877
ILT
199 unsigned int addralign, section_offset_type cie_offset,
200 unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr);
730cdc88
ILT
201
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.
8383303e 208 section_offset_type input_offset_;
730cdc88
ILT
209 // FDE data.
210 std::string contents_;
211};
212
213// This class holds a CIE.
214
215class Cie
216{
217 public:
8383303e 218 Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset,
730cdc88
ILT
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)
228 { }
229
230 ~Cie();
231
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()); }
243
244 // Add an FDE associated with this CIE.
245 void
246 add_fde(Fde* fde)
247 { this->fdes_.push_back(fde); }
248
249 // Return the number of FDEs.
250 unsigned int
251 fde_count() const
252 { return this->fdes_.size(); }
253
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.
8383303e
ILT
258 section_offset_type
259 set_output_offset(section_offset_type output_offset, unsigned int addralign,
260 Merge_map*);
730cdc88
ILT
261
262 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
935e8877
ILT
263 // exception frame header for FDE recording. Round up the bytes to
264 // ADDRALIGN. Return the new offset.
730cdc88 265 template<int size, bool big_endian>
8383303e
ILT
266 section_offset_type
267 write(unsigned char* oview, section_offset_type offset,
935e8877 268 unsigned int addralign, Eh_frame_hdr* eh_frame_hdr);
730cdc88
ILT
269
270 friend bool operator<(const Cie&, const Cie&);
271 friend bool operator==(const Cie&, const Cie&);
272
273 private:
274 // The class is not assignable.
275 Cie& operator=(const Cie&);
276
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.
8383303e 282 section_offset_type input_offset_;
730cdc88
ILT
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_;
292};
293
294extern bool operator<(const Cie&, const Cie&);
295extern bool operator==(const Cie&, const Cie&);
296
297// This class manages .eh_frame sections. It discards duplicate
298// exception information.
299
300class Eh_frame : public Output_section_data
301{
302 public:
303 Eh_frame();
304
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; }
309
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,
8383303e 321 section_size_type symbols_size,
730cdc88 322 const unsigned char* symbol_names,
8383303e 323 section_size_type symbol_names_size,
730cdc88
ILT
324 unsigned int shndx, unsigned int reloc_shndx,
325 unsigned int reloc_type);
326
327 // Return the number of FDEs.
328 unsigned int
329 fde_count() const;
330
7d9e3d98 331 protected:
730cdc88
ILT
332 // Set the final data size.
333 void
27bc2bce 334 set_final_data_size();
730cdc88
ILT
335
336 // Return the output address for an input address.
337 bool
8383303e
ILT
338 do_output_offset(const Relobj*, unsigned int shndx,
339 section_offset_type offset,
340 section_offset_type* poutput) const;
730cdc88 341
a9a60db6
ILT
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;
345
730cdc88
ILT
346 // Write the data to the file.
347 void
348 do_write(Output_file*);
349
7d9e3d98
ILT
350 // Write to a map file.
351 void
352 do_print_to_mapfile(Mapfile* mapfile) const
353 { mapfile->print_output_data(this, _("** eh_frame")); }
354
730cdc88
ILT
355 private:
356 // The comparison routine for the CIE map.
357 struct Cie_less
358 {
359 bool
360 operator()(const Cie* cie1, const Cie* cie2) const
361 { return *cie1 < *cie2; }
362 };
363
1cac254c
ILT
364 // A set of unique CIEs.
365 typedef std::set<Cie*, Cie_less> Cie_offsets;
730cdc88 366
1cac254c
ILT
367 // A list of unmergeable CIEs.
368 typedef std::vector<Cie*> Unmergeable_cie_offsets;
730cdc88
ILT
369
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;
373
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;
377
378 // Skip an LEB128.
379 static bool
380 skip_leb128(const unsigned char**, const unsigned char*);
381
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,
8383303e 387 section_size_type symbols_size,
730cdc88 388 const unsigned char* symbol_names,
8383303e 389 section_size_type symbol_names_size,
730cdc88
ILT
390 unsigned int shndx,
391 unsigned int reloc_shndx,
392 unsigned int reloc_type,
393 const unsigned char* pcontents,
8383303e 394 section_size_type contents_len,
730cdc88
ILT
395 New_cies*);
396
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,
8383303e 403 section_size_type symbols_size,
730cdc88 404 const unsigned char* symbol_names,
8383303e 405 section_size_type symbol_names_size,
730cdc88
ILT
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);
412
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,
8383303e 419 section_size_type symbols_size,
730cdc88
ILT
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);
426
427 // Template version of write function.
428 template<int size, bool big_endian>
429 void
430 do_sized_write(unsigned char* oview);
431
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_;
1d6531cf
ILT
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_;
3151305a
ILT
447};
448
449} // End namespace gold.
450
451#endif // !defined(GOLD_EHFRAME_H)
This page took 0.081336 seconds and 4 git commands to generate.