Fix ehframe header handling for shared libraries.
[external/binutils.git] / gold / ehframe.cc
1 // ehframe.cc -- handle exception frame sections for gold
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 #include "gold.h"
24
25 #include <cstring>
26 #include <algorithm>
27
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "symtab.h"
31 #include "reloc.h"
32 #include "ehframe.h"
33
34 namespace gold
35 {
36
37 // This file handles generation of the exception frame header that
38 // gcc's runtime support libraries use to find unwind information at
39 // runtime.  This file also handles discarding duplicate exception
40 // frame information.
41
42 // The exception frame header starts with four bytes:
43
44 // 0: The version number, currently 1.
45
46 // 1: The encoding of the pointer to the exception frames.  This can
47 //    be any DWARF unwind encoding (DW_EH_PE_*).  It is normally a 4
48 //    byte PC relative offset (DW_EH_PE_pcrel | DW_EH_PE_sdata4).
49
50 // 2: The encoding of the count of the number of FDE pointers in the
51 //    lookup table.  This can be any DWARF unwind encoding, and in
52 //    particular can be DW_EH_PE_omit if the count is omitted.  It is
53 //    normally a 4 byte unsigned count (DW_EH_PE_udata4).
54
55 // 3: The encoding of the lookup table entries.  Currently gcc's
56 //    libraries will only support DW_EH_PE_datarel | DW_EH_PE_sdata4,
57 //    which means that the values are 4 byte offsets from the start of
58 //    the table.
59
60 // The exception frame header is followed by a pointer to the contents
61 // of the exception frame section (.eh_frame).  This pointer is
62 // encoded as specified in the byte at offset 1 of the header (i.e.,
63 // it is normally a 4 byte PC relative offset).
64
65 // If there is a lookup table, this is followed by the count of the
66 // number of FDE pointers, encoded as specified in the byte at offset
67 // 2 of the header (i.e., normally a 4 byte unsigned integer).
68
69 // This is followed by the table, which should start at an 4-byte
70 // aligned address in memory.  Each entry in the table is 8 bytes.
71 // Each entry represents an FDE.  The first four bytes of each entry
72 // are an offset to the starting PC for the FDE.  The last four bytes
73 // of each entry are an offset to the FDE data.  The offsets are from
74 // the start of the exception frame header information.  The entries
75 // are in sorted order by starting PC.
76
77 const int eh_frame_hdr_size = 4;
78
79 // Construct the exception frame header.
80
81 Eh_frame_hdr::Eh_frame_hdr(Output_section* eh_frame_section,
82                            const Eh_frame* eh_frame_data)
83   : Output_section_data(4),
84     eh_frame_section_(eh_frame_section),
85     eh_frame_data_(eh_frame_data),
86     fde_offsets_(),
87     any_unrecognized_eh_frame_sections_(false)
88 {
89 }
90
91 // Set the size of the exception frame header.
92
93 void
94 Eh_frame_hdr::set_final_data_size()
95 {
96   unsigned int data_size = eh_frame_hdr_size + 4;
97   if (!this->any_unrecognized_eh_frame_sections_)
98     {
99       unsigned int fde_count = this->eh_frame_data_->fde_count();
100       if (fde_count != 0)
101         data_size += 4 + 8 * fde_count;
102       this->fde_offsets_.reserve(fde_count);
103     }
104   this->set_data_size(data_size);
105 }
106
107 // Write the data to the flie.
108
109 void
110 Eh_frame_hdr::do_write(Output_file* of)
111 {
112   if (parameters->get_size() == 32)
113     {
114       if (!parameters->is_big_endian())
115         {
116 #ifdef HAVE_TARGET_32_LITTLE
117           this->do_sized_write<32, false>(of);
118 #else
119           gold_unreachable();
120 #endif
121         }
122       else
123         {
124 #ifdef HAVE_TARGET_32_BIG
125           this->do_sized_write<32, true>(of);
126 #else
127           gold_unreachable();
128 #endif
129         }
130     }
131   else if (parameters->get_size() == 64)
132     {
133       if (!parameters->is_big_endian())
134         {
135 #ifdef HAVE_TARGET_64_LITTLE
136           this->do_sized_write<64, false>(of);
137 #else
138           gold_unreachable();
139 #endif
140         }
141       else
142         {
143 #ifdef HAVE_TARGET_64_BIG
144           this->do_sized_write<64, true>(of);
145 #else
146           gold_unreachable();
147 #endif
148         }
149     }
150   else
151     gold_unreachable();
152 }
153
154 // Write the data to the file with the right endianness.
155
156 template<int size, bool big_endian>
157 void
158 Eh_frame_hdr::do_sized_write(Output_file* of)
159 {
160   const off_t off = this->offset();
161   const off_t oview_size = this->data_size();
162   unsigned char* const oview = of->get_output_view(off, oview_size);
163
164   // Version number.
165   oview[0] = 1;
166
167   // Write out a 4 byte PC relative offset to the address of the
168   // .eh_frame section.
169   oview[1] = elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4;
170   uint64_t eh_frame_address = this->eh_frame_section_->address();
171   uint64_t eh_frame_hdr_address = this->address();
172   uint64_t eh_frame_offset = (eh_frame_address -
173                               (eh_frame_hdr_address + 4));
174   elfcpp::Swap<32, big_endian>::writeval(oview + 4, eh_frame_offset);
175
176   if (this->any_unrecognized_eh_frame_sections_
177       || this->fde_offsets_.empty())
178     {
179       // There are no FDEs, or we didn't recognize the format of the
180       // some of the .eh_frame sections, so we can't write out the
181       // sorted table.
182       oview[2] = elfcpp::DW_EH_PE_omit;
183       oview[3] = elfcpp::DW_EH_PE_omit;
184
185       gold_assert(oview_size == 8);
186     }
187   else
188     {
189       oview[2] = elfcpp::DW_EH_PE_udata4;
190       oview[3] = elfcpp::DW_EH_PE_datarel | elfcpp::DW_EH_PE_sdata4;
191
192       elfcpp::Swap<32, big_endian>::writeval(oview + 8,
193                                              this->fde_offsets_.size());
194
195       // We have the offsets of the FDEs in the .eh_frame section.  We
196       // couldn't easily get the PC values before, as they depend on
197       // relocations which are, of course, target specific.  This code
198       // is run after all those relocations have been applied to the
199       // output file.  Here we read the output file again to find the
200       // PC values.  Then we sort the list and write it out.
201
202       Fde_addresses<size> fde_addresses(this->fde_offsets_.size());
203       this->get_fde_addresses<size, big_endian>(of, &this->fde_offsets_,
204                                                 &fde_addresses);
205
206       std::sort(fde_addresses.begin(), fde_addresses.end(),
207                 Fde_address_compare<size>());
208
209       typename elfcpp::Elf_types<size>::Elf_Addr output_address;
210       output_address = this->address();
211
212       unsigned char* pfde = oview + 12;
213       for (typename Fde_addresses<size>::iterator p = fde_addresses.begin();
214            p != fde_addresses.end();
215            ++p)
216         {
217           elfcpp::Swap<32, big_endian>::writeval(pfde,
218                                                  p->first - output_address);
219           elfcpp::Swap<32, big_endian>::writeval(pfde + 4,
220                                                  p->second - output_address);
221           pfde += 8;
222         }
223
224       gold_assert(pfde - oview == oview_size);
225     }
226
227   of->write_output_view(off, oview_size, oview);
228 }
229
230 // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and
231 // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the
232 // FDE's encoding is FDE_ENCODING, return the output address of the
233 // FDE's PC.
234
235 template<int size, bool big_endian>
236 typename elfcpp::Elf_types<size>::Elf_Addr
237 Eh_frame_hdr::get_fde_pc(
238     typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
239     const unsigned char* eh_frame_contents,
240     off_t fde_offset,
241     unsigned char fde_encoding)
242 {
243   // The FDE starts with a 4 byte length and a 4 byte offset to the
244   // CIE.  The PC follows.
245   const unsigned char* p = eh_frame_contents + fde_offset + 8;
246
247   typename elfcpp::Elf_types<size>::Elf_Addr pc;
248   bool is_signed = (fde_encoding & elfcpp::DW_EH_PE_signed) != 0;
249   int pc_size = fde_encoding & 7;
250   if (pc_size == elfcpp::DW_EH_PE_absptr)
251     {
252       if (size == 32)
253         pc_size = elfcpp::DW_EH_PE_udata4;
254       else if (size == 64)
255         pc_size = elfcpp::DW_EH_PE_udata8;
256       else
257         gold_unreachable();
258     }
259
260   switch (pc_size)
261     {
262     case elfcpp::DW_EH_PE_udata2:
263       pc = elfcpp::Swap<16, big_endian>::readval(p);
264       if (is_signed)
265         pc = (pc ^ 0x8000) - 0x8000;
266       break;
267
268     case elfcpp::DW_EH_PE_udata4:
269       pc = elfcpp::Swap<32, big_endian>::readval(p);
270       if (size > 32 && is_signed)
271         pc = (pc ^ 0x80000000) - 0x80000000;
272       break;
273
274     case elfcpp::DW_EH_PE_udata8:
275       gold_assert(size == 64);
276       pc = elfcpp::Swap_unaligned<64, big_endian>::readval(p);
277       break;
278
279     default:
280       // All other cases were rejected in Eh_frame::read_cie.
281       gold_unreachable();
282     }
283
284   switch (fde_encoding & 0xf0)
285     {
286     case 0:
287       break;
288
289     case elfcpp::DW_EH_PE_pcrel:
290       pc += eh_frame_address + fde_offset + 8;
291       break;
292
293     default:
294       // If other cases arise, then we have to handle them, or we have
295       // to reject them by returning false in Eh_frame::read_cie.
296       gold_unreachable();
297     }
298
299   return pc;
300 }
301
302 // Given an array of FDE offsets in the .eh_frame section, return an
303 // array of offsets from the exception frame header to the FDE's
304 // output PC and to the output address of the FDE itself.  We get the
305 // FDE's PC by actually looking in the .eh_frame section we just wrote
306 // to the output file.
307
308 template<int size, bool big_endian>
309 void
310 Eh_frame_hdr::get_fde_addresses(Output_file* of,
311                                 const Fde_offsets* fde_offsets,
312                                 Fde_addresses<size>* fde_addresses)
313 {
314   typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address;
315   eh_frame_address = this->eh_frame_section_->address();
316   off_t eh_frame_offset = this->eh_frame_section_->offset();
317   off_t eh_frame_size = this->eh_frame_section_->data_size();
318   const unsigned char* eh_frame_contents = of->get_input_view(eh_frame_offset,
319                                                               eh_frame_size);
320
321   for (Fde_offsets::const_iterator p = fde_offsets->begin();
322        p != fde_offsets->end();
323        ++p)
324     {
325       typename elfcpp::Elf_types<size>::Elf_Addr fde_pc;
326       fde_pc = this->get_fde_pc<size, big_endian>(eh_frame_address,
327                                                   eh_frame_contents,
328                                                   p->first, p->second);
329       fde_addresses->push_back(fde_pc, eh_frame_address + p->first);
330     }
331
332   of->free_input_view(eh_frame_offset, eh_frame_size, eh_frame_contents);
333 }
334
335 // Class Fde.
336
337 // Write the FDE to OVIEW starting at OFFSET.  CIE_OFFSET is the
338 // offset of the CIE in OVIEW.  FDE_ENCODING is the encoding, from the
339 // CIE.  Record the FDE pc for EH_FRAME_HDR.  Return the new offset.
340
341 template<int size, bool big_endian>
342 off_t
343 Fde::write(unsigned char* oview, off_t offset, off_t cie_offset,
344            unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr)
345 {
346   size_t length = this->contents_.length();
347
348   // Write the length of the FDE as a 32-bit word.  The length word
349   // does not include the four bytes of the length word itself, but it
350   // does include the offset to the CIE.
351   elfcpp::Swap<32, big_endian>::writeval(oview + offset,
352                                          length + 4);
353
354   // Write the offset to the CIE as a 32-bit word.  This is the
355   // difference between the address of the offset word itself and the
356   // CIE address.
357   elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4,
358                                          offset + 4 - cie_offset);
359
360   // Copy the rest of the FDE.  Note that this is run before
361   // relocation processing is done on this section, so the relocations
362   // will later be applied to the FDE data.
363   memcpy(oview + offset + 8, this->contents_.data(), length);
364
365   // Tell the exception frame header about this FDE.
366   if (eh_frame_hdr != NULL)
367     eh_frame_hdr->record_fde(offset, fde_encoding);
368
369   return offset + length + 8;
370 }
371
372 // Class Cie.
373
374 // Destructor.
375
376 Cie::~Cie()
377 {
378   for (std::vector<Fde*>::iterator p = this->fdes_.begin();
379        p != this->fdes_.end();
380        ++p)
381     delete *p;
382 }
383
384 // Set the output offset of a CIE.  Return the new output offset.
385
386 off_t
387 Cie::set_output_offset(off_t output_offset, unsigned int addralign,
388                        Merge_map* merge_map)
389 {
390   size_t length = this->contents_.length();
391   gold_assert((length & (addralign - 1)) == 0);
392   // Add 4 for length and 4 for zero CIE identifier tag.
393   length += 8;
394
395   merge_map->add_mapping(this->object_, this->shndx_, this->input_offset_,
396                          length, output_offset);
397
398   for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
399        p != this->fdes_.end();
400        ++p)
401     {
402       (*p)->add_mapping(output_offset + length, merge_map);
403
404       size_t fde_length = (*p)->length();
405       gold_assert((fde_length & (addralign - 1)) == 0);
406       length += fde_length;
407     }
408
409   return output_offset + length;
410 }
411
412 // Write the CIE to OVIEW starting at OFFSET.  EH_FRAME_HDR is for FDE
413 // recording.  Return the new offset.
414
415 template<int size, bool big_endian>
416 off_t
417 Cie::write(unsigned char* oview, off_t offset, Eh_frame_hdr* eh_frame_hdr)
418 {
419   off_t cie_offset = offset;
420
421   size_t length = this->contents_.length();
422
423   // Write the length of the CIE as a 32-bit word.  The length word
424   // does not include the four bytes of the length word itself.
425   elfcpp::Swap<32, big_endian>::writeval(oview + offset, length + 4);
426
427   // Write the tag which marks this as a CIE: a 32-bit zero.
428   elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0);
429
430   // Write out the CIE data.
431   memcpy(oview + offset + 8, this->contents_.data(), length);
432   offset += length + 8;
433
434   // Write out the associated FDEs.
435   unsigned char fde_encoding = this->fde_encoding_;
436   for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
437        p != this->fdes_.end();
438        ++p)
439     offset = (*p)->write<size, big_endian>(oview, offset, cie_offset,
440                                            fde_encoding, eh_frame_hdr);
441
442   return offset;
443 }
444
445 // We track all the CIEs we see, and merge them when possible.  This
446 // works because each FDE holds an offset to the relevant CIE: we
447 // rewrite the FDEs to point to the merged CIE.  This is worthwhile
448 // because in a typical C++ program many FDEs in many different object
449 // files will use the same CIE.
450
451 // An equality operator for Cie.
452
453 bool
454 operator==(const Cie& cie1, const Cie& cie2)
455 {
456   return (cie1.personality_name_ == cie2.personality_name_
457           && cie1.contents_ == cie2.contents_);
458 }
459
460 // A less-than operator for Cie.
461
462 bool
463 operator<(const Cie& cie1, const Cie& cie2)
464 {
465   if (cie1.personality_name_ != cie2.personality_name_)
466     return cie1.personality_name_ < cie2.personality_name_;
467   return cie1.contents_ < cie2.contents_;
468 }
469
470 // Class Eh_frame.
471
472 Eh_frame::Eh_frame()
473   : Output_section_data(Output_data::default_alignment()),
474     eh_frame_hdr_(NULL),
475     cie_offsets_(),
476     unmergeable_cie_offsets_(),
477     merge_map_()
478 {
479 }
480
481 // Skip an LEB128, updating *PP to point to the next character.
482 // Return false if we ran off the end of the string.
483
484 bool
485 Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend)
486 {
487   const unsigned char* p;
488   for (p = *pp; p < pend; ++p)
489     {
490       if ((*p & 0x80) == 0)
491         {
492           *pp = p + 1;
493           return true;
494         }
495     }
496   return false;
497 }
498
499 // Add input section SHNDX in OBJECT to an exception frame section.
500 // SYMBOLS is the contents of the symbol table section (size
501 // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
502 // SYMBOL_NAMES_SIZE).  RELOC_SHNDX is the index of a relocation
503 // section applying to SHNDX, or 0 if none, or -1U if more than one.
504 // RELOC_TYPE is the type of the reloc section if there is one, either
505 // SHT_REL or SHT_RELA.  We try to parse the input exception frame
506 // data into our data structures.  If we can't do it, we return false
507 // to mean that the section should be handled as a normal input
508 // section.
509
510 template<int size, bool big_endian>
511 bool
512 Eh_frame::add_ehframe_input_section(
513     Sized_relobj<size, big_endian>* object,
514     const unsigned char* symbols,
515     off_t symbols_size,
516     const unsigned char* symbol_names,
517     off_t symbol_names_size,
518     unsigned int shndx,
519     unsigned int reloc_shndx,
520     unsigned int reloc_type)
521 {
522   // Get the section contents.
523   off_t contents_len;
524   const unsigned char* pcontents = object->section_contents(shndx,
525                                                             &contents_len,
526                                                             false);
527   if (contents_len == 0)
528     return false;
529
530   // If this is the marker section for the end of the data, then
531   // return false to force it to be handled as an ordinary input
532   // section.  If we don't do this, we won't correctly handle the case
533   // of unrecognized .eh_frame sections.
534   if (contents_len == 4
535       && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0)
536     return false;
537
538   New_cies new_cies;
539   if (!this->do_add_ehframe_input_section(object, symbols, symbols_size,
540                                           symbol_names, symbol_names_size,
541                                           shndx, reloc_shndx,
542                                           reloc_type, pcontents,
543                                           contents_len, &new_cies))
544     {
545       this->eh_frame_hdr_->found_unrecognized_eh_frame_section();
546
547       for (New_cies::iterator p = new_cies.begin();
548            p != new_cies.end();
549            ++p)
550         delete p->first;
551
552       return false;
553     }
554
555   // Now that we know we are using this section, record any new CIEs
556   // that we found.
557   for (New_cies::const_iterator p = new_cies.begin();
558        p != new_cies.end();
559        ++p)
560     {
561       uint64_t zero = 0;
562       if (p->second)
563         this->cie_offsets_.insert(std::make_pair(p->first, zero));
564       else
565         this->unmergeable_cie_offsets_.push_back(std::make_pair(p->first,
566                                                                 zero));
567     }
568
569   return true;
570 }
571
572 // The bulk of the implementation of add_ehframe_input_section.
573
574 template<int size, bool big_endian>
575 bool
576 Eh_frame::do_add_ehframe_input_section(
577     Sized_relobj<size, big_endian>* object,
578     const unsigned char* symbols,
579     off_t symbols_size,
580     const unsigned char* symbol_names,
581     off_t symbol_names_size,
582     unsigned int shndx,
583     unsigned int reloc_shndx,
584     unsigned int reloc_type,
585     const unsigned char* pcontents,
586     off_t contents_len,
587     New_cies* new_cies)
588 {
589   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
590   Track_relocs<size, big_endian> relocs;
591
592   const unsigned char* p = pcontents;
593   const unsigned char* pend = p + contents_len;
594
595   // Get the contents of the reloc section if any.
596   if (!relocs.initialize(object, reloc_shndx, reloc_type))
597     return false;
598
599   // Keep track of which CIEs are at which offsets.
600   Offsets_to_cie cies;
601
602   while (p < pend)
603     {
604       if (pend - p < 4)
605         return false;
606
607       // There shouldn't be any relocations here.
608       if (relocs.advance(p + 4 - pcontents) > 0)
609         return false;
610
611       unsigned int len = elfcpp::Swap<32, big_endian>::readval(p);
612       p += 4;
613       if (len == 0)
614         {
615           // We should only find a zero-length entry at the end of the
616           // section.
617           if (p < pend)
618             return false;
619           break;
620         }
621       // We don't support a 64-bit .eh_frame.
622       if (len == 0xffffffff)
623         return false;
624       if (static_cast<unsigned int>(pend - p) < len)
625         return false;
626
627       const unsigned char* const pentend = p + len;
628
629       if (pend - p < 4)
630         return false;
631       if (relocs.advance(p + 4 - pcontents) > 0)
632         return false;
633
634       unsigned int id = elfcpp::Swap<32, big_endian>::readval(p);
635       p += 4;
636
637       if (id == 0)
638         {
639           // CIE.
640           if (!this->read_cie(object, shndx, symbols, symbols_size,
641                               symbol_names, symbol_names_size,
642                               pcontents, p, pentend, &relocs, &cies,
643                               new_cies))
644             return false;
645         }
646       else
647         {
648           // FDE.
649           if (!this->read_fde(object, shndx, symbols, symbols_size,
650                               pcontents, id, p, pentend, &relocs, &cies))
651             return false;
652         }
653
654       p = pentend;
655     }
656
657   return true;
658 }
659
660 // Read a CIE.  Return false if we can't parse the information.
661
662 template<int size, bool big_endian>
663 bool
664 Eh_frame::read_cie(Sized_relobj<size, big_endian>* object,
665                    unsigned int shndx,
666                    const unsigned char* symbols,
667                    off_t symbols_size,
668                    const unsigned char* symbol_names,
669                    off_t symbol_names_size,
670                    const unsigned char* pcontents,
671                    const unsigned char* pcie,
672                    const unsigned char *pcieend,
673                    Track_relocs<size, big_endian>* relocs,
674                    Offsets_to_cie* cies,
675                    New_cies* new_cies)
676 {
677   bool mergeable = true;
678
679   // We need to find the personality routine if there is one, since we
680   // can only merge CIEs which use the same routine.  We also need to
681   // find the FDE encoding if there is one, so that we can read the PC
682   // from the FDE.
683
684   const unsigned char* p = pcie;
685
686   if (pcieend - p < 1)
687     return false;
688   unsigned char version = *p++;
689   if (version != 1 && version != 3)
690     return false;
691
692   const unsigned char* paug = p;
693   const void* paugendv = memchr(p, '\0', pcieend - p);
694   const unsigned char* paugend = static_cast<const unsigned char*>(paugendv);
695   if (paugend == NULL)
696     return false;
697   p = paugend + 1;
698
699   if (paug[0] == 'e' && paug[1] == 'h')
700     {
701       // This is a CIE from gcc before version 3.0.  We can't merge
702       // these.  We can still read the FDEs.
703       mergeable = false;
704       paug += 2;
705       if (*paug != '\0')
706         return false;
707       if (pcieend - p < size / 8)
708         return false;
709       p += size / 8;
710     }
711
712   // Skip the code alignment.
713   if (!skip_leb128(&p, pcieend))
714     return false;
715
716   // Skip the data alignment.
717   if (!skip_leb128(&p, pcieend))
718     return false;
719
720   // Skip the return column.
721   if (version == 1)
722     {
723       if (pcieend - p < 1)
724         return false;
725       ++p;
726     }
727   else
728     {
729       if (!skip_leb128(&p, pcieend))
730         return false;
731     }
732
733   if (*paug == 'z')
734     {
735       ++paug;
736       // Skip the augmentation size.
737       if (!skip_leb128(&p, pcieend))
738         return false;
739     }
740
741   unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr;
742   int per_offset = -1;
743   while (*paug != '\0')
744     {
745       switch (*paug)
746         {
747         case 'L': // LSDA encoding.
748           if (pcieend - p < 1)
749             return false;
750           ++p;
751           break;
752
753         case 'R': // FDE encoding.
754           if (pcieend - p < 1)
755             return false;
756           fde_encoding = *p;
757           switch (fde_encoding & 7)
758             {
759             case elfcpp::DW_EH_PE_absptr:
760             case elfcpp::DW_EH_PE_udata2:
761             case elfcpp::DW_EH_PE_udata4:
762             case elfcpp::DW_EH_PE_udata8:
763               break;
764             default:
765               // We don't expect to see any other cases here, and
766               // we're not prepared to handle them.
767               return false;
768             }
769           ++p;
770           break;
771
772         case 'S':
773           break;
774
775         case 'P':
776           // Personality encoding.
777           {
778             if (pcieend - p < 1)
779               return false;
780             unsigned char per_encoding = *p;
781             ++p;
782
783             if ((per_encoding & 0x60) == 0x60)
784               return false;
785             unsigned int per_width;
786             switch (per_encoding & 7)
787               {
788               case elfcpp::DW_EH_PE_udata2:
789                 per_width = 2;
790                 break;
791               case elfcpp::DW_EH_PE_udata4:
792                 per_width = 4;
793                 break;
794               case elfcpp::DW_EH_PE_udata8:
795                 per_width = 8;
796                 break;
797               case elfcpp::DW_EH_PE_absptr:
798                 per_width = size / 8;
799                 break;
800               default:
801                 return false;
802               }
803
804             if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned)
805               {
806                 unsigned int len = p - pcie;
807                 len += per_width - 1;
808                 len &= ~ (per_width - 1);
809                 if (static_cast<unsigned int>(pcieend - p) < len)
810                   return false;
811                 p += len;
812               }
813
814             per_offset = p - pcontents;
815
816             if (static_cast<unsigned int>(pcieend - p) < per_width)
817               return false;
818             p += per_width;
819           }
820           break;
821
822         default:
823           return false;
824         }
825
826       ++paug;
827     }
828
829   const char* personality_name = "";
830   if (per_offset != -1)
831     {
832       if (relocs->advance(per_offset) > 0)
833         return false;
834       if (relocs->next_offset() != per_offset)
835         return false;
836
837       unsigned int personality_symndx = relocs->next_symndx();
838       if (personality_symndx == -1U)
839         return false;
840
841       if (personality_symndx < object->local_symbol_count())
842         {
843           // We can only merge this CIE if the personality routine is
844           // a global symbol.  We can still read the FDEs.
845           mergeable = false;
846         }
847       else
848         {
849           const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
850           if (personality_symndx >= symbols_size / sym_size)
851             return false;
852           elfcpp::Sym<size, big_endian> sym(symbols
853                                             + (personality_symndx * sym_size));
854           unsigned int name_offset = sym.get_st_name();
855           if (name_offset >= symbol_names_size)
856             return false;
857           personality_name = (reinterpret_cast<const char*>(symbol_names)
858                               + name_offset);
859         }
860
861       int r = relocs->advance(per_offset + 1);
862       gold_assert(r == 1);
863     }
864
865   if (relocs->advance(pcieend - pcontents) > 0)
866     return false;
867
868   Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding, 
869           personality_name, pcie, pcieend - pcie);
870   Cie* cie_pointer = NULL;
871   if (mergeable)
872     {
873       Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
874       if (find_cie != this->cie_offsets_.end())
875         cie_pointer = find_cie->first;
876       else
877         {
878           // See if we already saw this CIE in this object file.
879           for (New_cies::const_iterator pc = new_cies->begin();
880                pc != new_cies->end();
881                ++pc)
882             {
883               if (*(pc->first) == cie)
884                 {
885                   cie_pointer = pc->first;
886                   break;
887                 }
888             }
889         }
890     }
891
892   if (cie_pointer == NULL)
893     {
894       cie_pointer = new Cie(cie);
895       new_cies->push_back(std::make_pair(cie_pointer, mergeable));
896     }
897   else
898     {
899       // We are deleting this CIE.  Record that in our mapping from
900       // input sections to the output section.  At this point we don't
901       // know for sure that we are doing a special mapping for this
902       // input section, but that's OK--if we don't do a special
903       // mapping, nobody will ever ask for the mapping we add here.
904       this->merge_map_.add_mapping(object, shndx, (pcie - 8) - pcontents,
905                                    pcieend - (pcie - 8), -1);
906     }
907
908   // Record this CIE plus the offset in the input section.
909   cies->insert(std::make_pair(pcie - pcontents, cie_pointer));
910
911   return true;
912 }
913
914 // Read an FDE.  Return false if we can't parse the information.
915
916 template<int size, bool big_endian>
917 bool
918 Eh_frame::read_fde(Sized_relobj<size, big_endian>* object,
919                    unsigned int shndx,
920                    const unsigned char* symbols,
921                    off_t symbols_size,
922                    const unsigned char* pcontents,
923                    unsigned int offset,
924                    const unsigned char* pfde,
925                    const unsigned char *pfdeend,
926                    Track_relocs<size, big_endian>* relocs,
927                    Offsets_to_cie* cies)
928 {
929   // OFFSET is the distance between the 4 bytes before PFDE to the
930   // start of the CIE.  The offset we recorded for the CIE is 8 bytes
931   // after the start of the CIE--after the length and the zero tag.
932   unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8;
933   Offsets_to_cie::const_iterator pcie = cies->find(cie_offset);
934   if (pcie == cies->end())
935     return false;
936   Cie* cie = pcie->second;
937
938   // The FDE should start with a reloc to the start of the code which
939   // it describes.
940   if (relocs->advance(pfde - pcontents) > 0)
941     return false;
942
943   if (relocs->next_offset() != pfde - pcontents)
944     return false;
945
946   unsigned int symndx = relocs->next_symndx();
947   if (symndx == -1U)
948     return false;
949
950   // There can be another reloc in the FDE, if the CIE specifies an
951   // LSDA (language specific data area).  We currently don't care.  We
952   // will care later if we want to optimize the LSDA from an absolute
953   // pointer to a PC relative offset when generating a shared library.
954   relocs->advance(pfdeend - pcontents);
955
956   unsigned int fde_shndx;
957   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
958   if (symndx >= symbols_size / sym_size)
959     return false;
960   elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size);
961   fde_shndx = sym.get_st_shndx();
962
963   if (fde_shndx != elfcpp::SHN_UNDEF
964       && fde_shndx < object->shnum()
965       && !object->is_section_included(fde_shndx))
966     {
967       // This FDE applies to a section which we are discarding.  We
968       // can discard this FDE.
969       this->merge_map_.add_mapping(object, shndx, (pfde - 8) - pcontents,
970                                    pfdeend - (pfde - 8), -1);
971       return true;
972     }
973
974   cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents,
975                        pfde, pfdeend - pfde));
976
977   return true;
978 }
979
980 // Return the number of FDEs.
981
982 unsigned int
983 Eh_frame::fde_count() const
984 {
985   unsigned int ret = 0;
986   for (Unmergeable_cie_offsets::const_iterator p =
987          this->unmergeable_cie_offsets_.begin();
988        p != this->unmergeable_cie_offsets_.end();
989        ++p)
990     ret += p->first->fde_count();
991   for (Cie_offsets::const_iterator p = this->cie_offsets_.begin();
992        p != this->cie_offsets_.end();
993        ++p)
994     ret += p->first->fde_count();
995   return ret;
996 }
997
998 // Set the final data size.
999
1000 void
1001 Eh_frame::set_final_data_size()
1002 {
1003   off_t start_file_offset = this->offset();
1004   off_t output_offset = 0;
1005
1006   for (Unmergeable_cie_offsets::iterator p =
1007          this->unmergeable_cie_offsets_.begin();
1008        p != this->unmergeable_cie_offsets_.end();
1009        ++p)
1010     {
1011       p->second = start_file_offset + output_offset;
1012       output_offset = p->first->set_output_offset(output_offset,
1013                                                   this->addralign(),
1014                                                   &this->merge_map_);
1015     }
1016
1017   for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1018        p != this->cie_offsets_.end();
1019        ++p)
1020     {
1021       p->second = start_file_offset + output_offset;
1022       output_offset = p->first->set_output_offset(output_offset,
1023                                                   this->addralign(),
1024                                                   &this->merge_map_);
1025     }
1026
1027   gold_assert((output_offset & (this->addralign() - 1)) == 0);
1028   this->set_data_size(output_offset);
1029 }
1030
1031 // Return an output offset for an input offset.
1032
1033 bool
1034 Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx,
1035                            off_t offset, off_t* poutput) const
1036 {
1037   return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
1038 }
1039
1040 // Write the data to the output file.
1041
1042 void
1043 Eh_frame::do_write(Output_file* of)
1044 {
1045   const off_t offset = this->offset();
1046   const off_t oview_size = this->data_size();
1047   unsigned char* const oview = of->get_output_view(offset, oview_size);
1048
1049   if (parameters->get_size() == 32)
1050     {
1051       if (!parameters->is_big_endian())
1052         {
1053 #ifdef HAVE_TARGET_32_LITTLE
1054           this->do_sized_write<32, false>(oview);
1055 #else
1056           gold_unreachable();
1057 #endif
1058         }
1059       else
1060         {
1061 #ifdef HAVE_TARGET_32_BIG
1062           this->do_sized_write<32, true>(oview);
1063 #else
1064           gold_unreachable();
1065 #endif
1066         }
1067     }
1068   else if (parameters->get_size() == 64)
1069     {
1070       if (!parameters->is_big_endian())
1071         {
1072 #ifdef HAVE_TARGET_64_LITTLE
1073           this->do_sized_write<64, false>(oview);
1074 #else
1075           gold_unreachable();
1076 #endif
1077         }
1078       else
1079         {
1080 #ifdef HAVE_TARGET_64_BIG
1081           this->do_sized_write<64, true>(oview);
1082 #else
1083           gold_unreachable();
1084 #endif
1085         }
1086     }
1087   else
1088     gold_unreachable();
1089
1090   of->write_output_view(offset, oview_size, oview);
1091 }
1092
1093 // Write the data to the output file--template version.
1094
1095 template<int size, bool big_endian>
1096 void
1097 Eh_frame::do_sized_write(unsigned char* oview)
1098 {
1099   off_t o = 0;
1100   for (Unmergeable_cie_offsets::iterator p =
1101          this->unmergeable_cie_offsets_.begin();
1102        p != this->unmergeable_cie_offsets_.end();
1103        ++p)
1104     o = p->first->write<size, big_endian>(oview, o, this->eh_frame_hdr_);
1105   for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1106        p != this->cie_offsets_.end();
1107        ++p)
1108     o = p->first->write<size, big_endian>(oview, o, this->eh_frame_hdr_);
1109 }
1110
1111 #ifdef HAVE_TARGET_32_LITTLE
1112 template
1113 bool
1114 Eh_frame::add_ehframe_input_section<32, false>(
1115     Sized_relobj<32, false>* object,
1116     const unsigned char* symbols,
1117     off_t symbols_size,
1118     const unsigned char* symbol_names,
1119     off_t symbol_names_size,
1120     unsigned int shndx,
1121     unsigned int reloc_shndx,
1122     unsigned int reloc_type);
1123 #endif
1124
1125 #ifdef HAVE_TARGET_32_BIG
1126 template
1127 bool
1128 Eh_frame::add_ehframe_input_section<32, true>(
1129     Sized_relobj<32, true>* object,
1130     const unsigned char* symbols,
1131     off_t symbols_size,
1132     const unsigned char* symbol_names,
1133     off_t symbol_names_size,
1134     unsigned int shndx,
1135     unsigned int reloc_shndx,
1136     unsigned int reloc_type);
1137 #endif
1138
1139 #ifdef HAVE_TARGET_64_LITTLE
1140 template
1141 bool
1142 Eh_frame::add_ehframe_input_section<64, false>(
1143     Sized_relobj<64, false>* object,
1144     const unsigned char* symbols,
1145     off_t symbols_size,
1146     const unsigned char* symbol_names,
1147     off_t symbol_names_size,
1148     unsigned int shndx,
1149     unsigned int reloc_shndx,
1150     unsigned int reloc_type);
1151 #endif
1152
1153 #ifdef HAVE_TARGET_64_BIG
1154 template
1155 bool
1156 Eh_frame::add_ehframe_input_section<64, true>(
1157     Sized_relobj<64, true>* object,
1158     const unsigned char* symbols,
1159     off_t symbols_size,
1160     const unsigned char* symbol_names,
1161     off_t symbol_names_size,
1162     unsigned int shndx,
1163     unsigned int reloc_shndx,
1164     unsigned int reloc_type);
1165 #endif
1166
1167 } // End namespace gold.