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