OSDN Git Service

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