]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/ehframe.h
Luis Machado <luisgpm@br.ibm.com>
[thirdparty/binutils-gdb.git] / gold / ehframe.h
CommitLineData
3151305a
ILT
1// ehframe.h -- handle exception frame sections for gold -*- C++ -*-
2
3// Copyright 2006, 2007 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 "output.h"
730cdc88 27#include "merge.h"
3151305a
ILT
28
29namespace gold
30{
31
730cdc88
ILT
32template<int size, bool big_endian>
33class Track_relocs;
34
35class Eh_frame;
36
3151305a
ILT
37// This class manages the .eh_frame_hdr section, which holds the data
38// for the PT_GNU_EH_FRAME segment. gcc's unwind support code uses
39// the PT_GNU_EH_FRAME segment to find the list of FDEs. This saves
40// the time required to register the exception handlers at startup
41// time and when a shared object is loaded, and the time required to
42// deregister the exception handlers when a shared object is unloaded.
43
44// FIXME: gcc supports using storing a sorted lookup table for the
45// FDEs in the PT_GNU_EH_FRAME segment, but we do not yet generate
46// that.
47
48class Eh_frame_hdr : public Output_section_data
49{
50 public:
730cdc88
ILT
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
8383303e 60 record_fde(section_offset_type fde_offset, unsigned char fde_encoding)
730cdc88
ILT
61 {
62 if (!this->any_unrecognized_eh_frame_sections_)
63 this->fde_offsets_.push_back(std::make_pair(fde_offset, fde_encoding));
64 }
3151305a
ILT
65
66 // Set the final data size.
67 void
27bc2bce 68 set_final_data_size();
3151305a
ILT
69
70 // Write the data to the file.
71 void
72 do_write(Output_file*);
73
74 private:
730cdc88
ILT
75 // Write the data to the file with the right endianness.
76 template<int size, bool big_endian>
77 void
78 do_sized_write(Output_file*);
79
80 // The data we record for one FDE: the offset of the FDE within the
81 // .eh_frame section, and the FDE encoding.
8383303e 82 typedef std::pair<section_offset_type, unsigned char> Fde_offset;
730cdc88
ILT
83
84 // The list of information we record for an FDE.
85 typedef std::vector<Fde_offset> Fde_offsets;
86
87 // When writing out the header, we convert the FDE offsets into FDE
88 // addresses. This is a list of pairs of the offset from the header
89 // to the FDE PC and to the FDE itself.
90 template<int size>
91 class Fde_addresses
92 {
93 public:
94 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
95 typedef typename std::pair<Address, Address> Fde_address;
96 typedef typename std::vector<Fde_address> Fde_address_list;
97 typedef typename Fde_address_list::iterator iterator;
98
99 Fde_addresses(unsigned int reserve)
100 : fde_addresses_()
101 { this->fde_addresses_.reserve(reserve); }
102
103 void
104 push_back(Address pc_address, Address fde_address)
105 {
106 this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address));
107 }
108
109 iterator
110 begin()
111 { return this->fde_addresses_.begin(); }
112
113 iterator
114 end()
115 { return this->fde_addresses_.end(); }
116
117 private:
118 Fde_address_list fde_addresses_;
119 };
120
121 // Compare Fde_address objects.
122 template<int size>
123 struct Fde_address_compare
124 {
125 bool
126 operator()(const typename Fde_addresses<size>::Fde_address& f1,
127 const typename Fde_addresses<size>::Fde_address& f2) const
128 { return f1.first < f2.first; }
129 };
130
131 // Return the PC to which an FDE refers.
132 template<int size, bool big_endian>
133 typename elfcpp::Elf_types<size>::Elf_Addr
4117d768
ILT
134 get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
135 const unsigned char* eh_frame_contents,
8383303e 136 section_offset_type fde_offset, unsigned char fde_encoding);
730cdc88
ILT
137
138 // Convert Fde_offsets to Fde_addresses.
139 template<int size, bool big_endian>
140 void
141 get_fde_addresses(Output_file* of,
142 const Fde_offsets* fde_offsets,
143 Fde_addresses<size>* fde_addresses);
144
3151305a
ILT
145 // The .eh_frame section.
146 Output_section* eh_frame_section_;
730cdc88
ILT
147 // The .eh_frame section data.
148 const Eh_frame* eh_frame_data_;
149 // Data from the FDEs in the .eh_frame sections.
150 Fde_offsets fde_offsets_;
151 // Whether we found any .eh_frame sections which we could not
152 // process.
153 bool any_unrecognized_eh_frame_sections_;
154};
155
156// This class holds an FDE.
157
158class Fde
159{
160 public:
8383303e 161 Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset,
730cdc88
ILT
162 const unsigned char* contents, size_t length)
163 : object_(object), shndx_(shndx), input_offset_(input_offset),
164 contents_(reinterpret_cast<const char*>(contents), length)
165 { }
166
167 // Return the length of this FDE. Add 4 for the length and 4 for
168 // the offset to the CIE.
169 size_t
170 length() const
171 { return this->contents_.length() + 8; }
172
173 // Add a mapping for this FDE to MERGE_MAP.
174 void
8383303e 175 add_mapping(section_offset_type output_offset, Merge_map* merge_map) const
730cdc88
ILT
176 {
177 merge_map->add_mapping(this->object_, this->shndx_,
178 this->input_offset_, this->length(),
179 output_offset);
180 }
181
182 // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the
183 // encoding, from the CIE. Record the FDE in EH_FRAME_HDR. Return
184 // the new offset.
185 template<int size, bool big_endian>
8383303e
ILT
186 section_offset_type
187 write(unsigned char* oview, section_offset_type offset,
188 section_offset_type cie_offset, unsigned char fde_encoding,
189 Eh_frame_hdr* eh_frame_hdr);
730cdc88
ILT
190
191 private:
192 // The object in which this FDE was seen.
193 Relobj* object_;
194 // Input section index for this FDE.
195 unsigned int shndx_;
196 // Offset within the input section for this FDE.
8383303e 197 section_offset_type input_offset_;
730cdc88
ILT
198 // FDE data.
199 std::string contents_;
200};
201
202// This class holds a CIE.
203
204class Cie
205{
206 public:
8383303e 207 Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset,
730cdc88
ILT
208 unsigned char fde_encoding, const char* personality_name,
209 const unsigned char* contents, size_t length)
210 : object_(object),
211 shndx_(shndx),
212 input_offset_(input_offset),
213 fde_encoding_(fde_encoding),
214 personality_name_(personality_name),
215 fdes_(),
216 contents_(reinterpret_cast<const char*>(contents), length)
217 { }
218
219 ~Cie();
220
221 // We permit copying a CIE when there are no FDEs. This is
222 // convenient in the code which creates them.
223 Cie(const Cie& cie)
224 : object_(cie.object_),
225 shndx_(cie.shndx_),
226 input_offset_(cie.input_offset_),
227 fde_encoding_(cie.fde_encoding_),
228 personality_name_(cie.personality_name_),
229 fdes_(),
230 contents_(cie.contents_)
231 { gold_assert(cie.fdes_.empty()); }
232
233 // Add an FDE associated with this CIE.
234 void
235 add_fde(Fde* fde)
236 { this->fdes_.push_back(fde); }
237
238 // Return the number of FDEs.
239 unsigned int
240 fde_count() const
241 { return this->fdes_.size(); }
242
243 // Set the output offset of this CIE to OUTPUT_OFFSET. It will be
244 // followed by all its FDEs. ADDRALIGN is the required address
245 // alignment, typically 4 or 8. This updates MERGE_MAP with the
246 // mapping. It returns the new output offset.
8383303e
ILT
247 section_offset_type
248 set_output_offset(section_offset_type output_offset, unsigned int addralign,
249 Merge_map*);
730cdc88
ILT
250
251 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the
252 // exception frame header for FDE recording. Return the new offset.
253 template<int size, bool big_endian>
8383303e
ILT
254 section_offset_type
255 write(unsigned char* oview, section_offset_type offset,
256 Eh_frame_hdr* eh_frame_hdr);
730cdc88
ILT
257
258 friend bool operator<(const Cie&, const Cie&);
259 friend bool operator==(const Cie&, const Cie&);
260
261 private:
262 // The class is not assignable.
263 Cie& operator=(const Cie&);
264
265 // The object in which this CIE was first seen.
266 Relobj* object_;
267 // Input section index for this CIE.
268 unsigned int shndx_;
269 // Offset within the input section for this CIE.
8383303e 270 section_offset_type input_offset_;
730cdc88
ILT
271 // The encoding of the FDE. This is a DW_EH_PE code.
272 unsigned char fde_encoding_;
273 // The name of the personality routine. This will be the name of a
274 // global symbol, or will be the empty string.
275 std::string personality_name_;
276 // List of FDEs.
277 std::vector<Fde*> fdes_;
278 // CIE data.
279 std::string contents_;
280};
281
282extern bool operator<(const Cie&, const Cie&);
283extern bool operator==(const Cie&, const Cie&);
284
285// This class manages .eh_frame sections. It discards duplicate
286// exception information.
287
288class Eh_frame : public Output_section_data
289{
290 public:
291 Eh_frame();
292
293 // Record the associated Eh_frame_hdr, if any.
294 void
295 set_eh_frame_hdr(Eh_frame_hdr* hdr)
296 { this->eh_frame_hdr_ = hdr; }
297
298 // Add the input section SHNDX in OBJECT. SYMBOLS is the contents
299 // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is
300 // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX
301 // is the relocation section if any (0 for none, -1U for multiple).
302 // RELOC_TYPE is the type of the relocation section if any. This
303 // returns whether the section was incorporated into the .eh_frame
304 // data.
305 template<int size, bool big_endian>
306 bool
307 add_ehframe_input_section(Sized_relobj<size, big_endian>* object,
308 const unsigned char* symbols,
8383303e 309 section_size_type symbols_size,
730cdc88 310 const unsigned char* symbol_names,
8383303e 311 section_size_type symbol_names_size,
730cdc88
ILT
312 unsigned int shndx, unsigned int reloc_shndx,
313 unsigned int reloc_type);
314
315 // Return the number of FDEs.
316 unsigned int
317 fde_count() const;
318
319 // Set the final data size.
320 void
27bc2bce 321 set_final_data_size();
730cdc88
ILT
322
323 // Return the output address for an input address.
324 bool
8383303e
ILT
325 do_output_offset(const Relobj*, unsigned int shndx,
326 section_offset_type offset,
327 section_offset_type* poutput) const;
730cdc88 328
a9a60db6
ILT
329 // Return whether this is the merge section for an input section.
330 bool
331 do_is_merge_section_for(const Relobj*, unsigned int shndx) const;
332
730cdc88
ILT
333 // Write the data to the file.
334 void
335 do_write(Output_file*);
336
337 private:
338 // The comparison routine for the CIE map.
339 struct Cie_less
340 {
341 bool
342 operator()(const Cie* cie1, const Cie* cie2) const
343 { return *cie1 < *cie2; }
344 };
345
346 // A mapping from unique CIEs to their offset in the output file.
347 typedef std::map<Cie*, uint64_t, Cie_less> Cie_offsets;
348
349 // A list of unmergeable CIEs with their offsets.
350 typedef std::vector<std::pair<Cie*, uint64_t> > Unmergeable_cie_offsets;
351
352 // A mapping from offsets to CIEs. This is used while reading an
353 // input section.
354 typedef std::map<uint64_t, Cie*> Offsets_to_cie;
355
356 // A list of CIEs, and a bool indicating whether the CIE is
357 // mergeable.
358 typedef std::vector<std::pair<Cie*, bool> > New_cies;
359
360 // Skip an LEB128.
361 static bool
362 skip_leb128(const unsigned char**, const unsigned char*);
363
364 // The implementation of add_ehframe_input_section.
365 template<int size, bool big_endian>
366 bool
367 do_add_ehframe_input_section(Sized_relobj<size, big_endian>* object,
368 const unsigned char* symbols,
8383303e 369 section_size_type symbols_size,
730cdc88 370 const unsigned char* symbol_names,
8383303e 371 section_size_type symbol_names_size,
730cdc88
ILT
372 unsigned int shndx,
373 unsigned int reloc_shndx,
374 unsigned int reloc_type,
375 const unsigned char* pcontents,
8383303e 376 section_size_type contents_len,
730cdc88
ILT
377 New_cies*);
378
379 // Read a CIE.
380 template<int size, bool big_endian>
381 bool
382 read_cie(Sized_relobj<size, big_endian>* object,
383 unsigned int shndx,
384 const unsigned char* symbols,
8383303e 385 section_size_type symbols_size,
730cdc88 386 const unsigned char* symbol_names,
8383303e 387 section_size_type symbol_names_size,
730cdc88
ILT
388 const unsigned char* pcontents,
389 const unsigned char* pcie,
390 const unsigned char *pcieend,
391 Track_relocs<size, big_endian>* relocs,
392 Offsets_to_cie* cies,
393 New_cies* new_cies);
394
395 // Read an FDE.
396 template<int size, bool big_endian>
397 bool
398 read_fde(Sized_relobj<size, big_endian>* object,
399 unsigned int shndx,
400 const unsigned char* symbols,
8383303e 401 section_size_type symbols_size,
730cdc88
ILT
402 const unsigned char* pcontents,
403 unsigned int offset,
404 const unsigned char* pfde,
405 const unsigned char *pfdeend,
406 Track_relocs<size, big_endian>* relocs,
407 Offsets_to_cie* cies);
408
409 // Template version of write function.
410 template<int size, bool big_endian>
411 void
412 do_sized_write(unsigned char* oview);
413
414 // The exception frame header, if any.
415 Eh_frame_hdr* eh_frame_hdr_;
416 // A mapping from all unique CIEs to their offset in the output
417 // file.
418 Cie_offsets cie_offsets_;
419 // A mapping from unmergeable CIEs to their offset in the output
420 // file.
421 Unmergeable_cie_offsets unmergeable_cie_offsets_;
422 // A mapping from input sections to the output section.
423 Merge_map merge_map_;
3151305a
ILT
424};
425
426} // End namespace gold.
427
428#endif // !defined(GOLD_EHFRAME_H)