1 // ehframe.cc -- handle exception frame sections for gold
3 // Copyright 2006, 2007, 2008, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
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
42 // The exception frame header starts with four bytes:
44 // 0: The version number, currently 1.
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).
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).
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
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).
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).
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.
77 const int eh_frame_hdr_size
= 4;
79 // Construct the exception frame header.
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
),
87 any_unrecognized_eh_frame_sections_(false),
92 // Set the size of the exception frame header.
95 Eh_frame_hdr::set_final_data_size()
97 unsigned int data_size
= eh_frame_hdr_size
+ 4;
98 if (!this->any_unrecognized_eh_frame_sections_
)
100 unsigned int fde_count
= this->eh_frame_data_
->fde_count();
102 data_size
+= 4 + 8 * fde_count
;
103 this->fde_offsets_
.reserve(fde_count
);
105 this->set_data_size(data_size
);
106 // We need a lock for updating the fde_offsets_ vector while writing
108 this->lock_
= new Lock();
111 // Write the data to the file.
114 Eh_frame_hdr::do_write(Output_file
* of
)
116 switch (parameters
->size_and_endianness())
118 #ifdef HAVE_TARGET_32_LITTLE
119 case Parameters::TARGET_32_LITTLE
:
120 this->do_sized_write
<32, false>(of
);
123 #ifdef HAVE_TARGET_32_BIG
124 case Parameters::TARGET_32_BIG
:
125 this->do_sized_write
<32, true>(of
);
128 #ifdef HAVE_TARGET_64_LITTLE
129 case Parameters::TARGET_64_LITTLE
:
130 this->do_sized_write
<64, false>(of
);
133 #ifdef HAVE_TARGET_64_BIG
134 case Parameters::TARGET_64_BIG
:
135 this->do_sized_write
<64, true>(of
);
143 // Write the data to the file with the right endianness.
145 template<int size
, bool big_endian
>
147 Eh_frame_hdr::do_sized_write(Output_file
* of
)
149 const off_t off
= this->offset();
150 const off_t oview_size
= this->data_size();
151 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
156 // Write out a 4 byte PC relative offset to the address of the
157 // .eh_frame section.
158 oview
[1] = elfcpp::DW_EH_PE_pcrel
| elfcpp::DW_EH_PE_sdata4
;
159 uint64_t eh_frame_address
= this->eh_frame_section_
->address();
160 uint64_t eh_frame_hdr_address
= this->address();
161 uint64_t eh_frame_offset
= (eh_frame_address
-
162 (eh_frame_hdr_address
+ 4));
163 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ 4, eh_frame_offset
);
165 if (this->any_unrecognized_eh_frame_sections_
166 || this->fde_offsets_
.empty())
168 // There are no FDEs, or we didn't recognize the format of the
169 // some of the .eh_frame sections, so we can't write out the
171 oview
[2] = elfcpp::DW_EH_PE_omit
;
172 oview
[3] = elfcpp::DW_EH_PE_omit
;
174 gold_assert(oview_size
== 8);
178 oview
[2] = elfcpp::DW_EH_PE_udata4
;
179 oview
[3] = elfcpp::DW_EH_PE_datarel
| elfcpp::DW_EH_PE_sdata4
;
181 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ 8,
182 this->fde_offsets_
.size());
184 // We have the offsets of the FDEs in the .eh_frame section. We
185 // couldn't easily get the PC values before, as they depend on
186 // relocations which are, of course, target specific. This code
187 // is run after all those relocations have been applied to the
188 // output file. Here we read the output file again to find the
189 // PC values. Then we sort the list and write it out.
191 Fde_addresses
<size
> fde_addresses(this->fde_offsets_
.size());
192 this->get_fde_addresses
<size
, big_endian
>(of
, &this->fde_offsets_
,
195 std::sort(fde_addresses
.begin(), fde_addresses
.end(),
196 Fde_address_compare
<size
>());
198 typename
elfcpp::Elf_types
<size
>::Elf_Addr output_address
;
199 output_address
= this->address();
201 unsigned char* pfde
= oview
+ 12;
202 for (typename Fde_addresses
<size
>::iterator p
= fde_addresses
.begin();
203 p
!= fde_addresses
.end();
206 elfcpp::Swap
<32, big_endian
>::writeval(pfde
,
207 p
->first
- output_address
);
208 elfcpp::Swap
<32, big_endian
>::writeval(pfde
+ 4,
209 p
->second
- output_address
);
213 gold_assert(pfde
- oview
== oview_size
);
216 of
->write_output_view(off
, oview_size
, oview
);
219 // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and
220 // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the
221 // FDE's encoding is FDE_ENCODING, return the output address of the
224 template<int size
, bool big_endian
>
225 typename
elfcpp::Elf_types
<size
>::Elf_Addr
226 Eh_frame_hdr::get_fde_pc(
227 typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
,
228 const unsigned char* eh_frame_contents
,
229 section_offset_type fde_offset
,
230 unsigned char fde_encoding
)
232 // The FDE starts with a 4 byte length and a 4 byte offset to the
233 // CIE. The PC follows.
234 const unsigned char* p
= eh_frame_contents
+ fde_offset
+ 8;
236 typename
elfcpp::Elf_types
<size
>::Elf_Addr pc
;
237 bool is_signed
= (fde_encoding
& elfcpp::DW_EH_PE_signed
) != 0;
238 int pc_size
= fde_encoding
& 7;
239 if (pc_size
== elfcpp::DW_EH_PE_absptr
)
242 pc_size
= elfcpp::DW_EH_PE_udata4
;
244 pc_size
= elfcpp::DW_EH_PE_udata8
;
251 case elfcpp::DW_EH_PE_udata2
:
252 pc
= elfcpp::Swap
<16, big_endian
>::readval(p
);
254 pc
= (pc
^ 0x8000) - 0x8000;
257 case elfcpp::DW_EH_PE_udata4
:
258 pc
= elfcpp::Swap
<32, big_endian
>::readval(p
);
259 if (size
> 32 && is_signed
)
260 pc
= (pc
^ 0x80000000) - 0x80000000;
263 case elfcpp::DW_EH_PE_udata8
:
264 gold_assert(size
== 64);
265 pc
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(p
);
269 // All other cases were rejected in Eh_frame::read_cie.
273 switch (fde_encoding
& 0x70)
278 case elfcpp::DW_EH_PE_pcrel
:
279 pc
+= eh_frame_address
+ fde_offset
+ 8;
282 case elfcpp::DW_EH_PE_datarel
:
283 pc
+= parameters
->target().ehframe_datarel_base();
287 // If other cases arise, then we have to handle them, or we have
288 // to reject them by returning false in Eh_frame::read_cie.
292 gold_assert((fde_encoding
& elfcpp::DW_EH_PE_indirect
) == 0);
297 // Given an array of FDE offsets in the .eh_frame section, return an
298 // array of offsets from the exception frame header to the FDE's
299 // output PC and to the output address of the FDE itself. We get the
300 // FDE's PC by actually looking in the .eh_frame section we just wrote
301 // to the output file.
303 template<int size
, bool big_endian
>
305 Eh_frame_hdr::get_fde_addresses(Output_file
* of
,
306 const Fde_offsets
* fde_offsets
,
307 Fde_addresses
<size
>* fde_addresses
)
309 typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
;
310 eh_frame_address
= this->eh_frame_section_
->address();
311 off_t eh_frame_offset
= this->eh_frame_section_
->offset();
312 off_t eh_frame_size
= this->eh_frame_section_
->data_size();
313 const unsigned char* eh_frame_contents
= of
->get_input_view(eh_frame_offset
,
316 for (Fde_offsets::const_iterator p
= fde_offsets
->begin();
317 p
!= fde_offsets
->end();
320 typename
elfcpp::Elf_types
<size
>::Elf_Addr fde_pc
;
321 fde_pc
= this->get_fde_pc
<size
, big_endian
>(eh_frame_address
,
323 p
->first
, p
->second
);
324 fde_addresses
->push_back(fde_pc
, eh_frame_address
+ p
->first
);
327 of
->free_input_view(eh_frame_offset
, eh_frame_size
, eh_frame_contents
);
332 // Write the FDE to OVIEW starting at OFFSET. CIE_OFFSET is the
333 // offset of the CIE in OVIEW. FDE_ENCODING is the encoding, from the
334 // CIE. ADDRALIGN is the required alignment. ADDRESS is the virtual
335 // address of OVIEW. Record the FDE pc for EH_FRAME_HDR. Return the
338 template<int size
, bool big_endian
>
340 Fde::write(unsigned char* oview
, section_offset_type offset
,
341 uint64_t address
, unsigned int addralign
,
342 section_offset_type cie_offset
, unsigned char fde_encoding
,
343 Eh_frame_hdr
* eh_frame_hdr
)
345 gold_assert((offset
& (addralign
- 1)) == 0);
347 size_t length
= this->contents_
.length();
349 // We add 8 when getting the aligned length to account for the
350 // length word and the CIE offset.
351 size_t aligned_full_length
= align_address(length
+ 8, addralign
);
353 // Write the length of the FDE as a 32-bit word. The length word
354 // does not include the four bytes of the length word itself, but it
355 // does include the offset to the CIE.
356 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
,
357 aligned_full_length
- 4);
359 // Write the offset to the CIE as a 32-bit word. This is the
360 // difference between the address of the offset word itself and the
362 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 4,
363 offset
+ 4 - cie_offset
);
365 // Copy the rest of the FDE. Note that this is run before
366 // relocation processing is done on this section, so the relocations
367 // will later be applied to the FDE data.
368 memcpy(oview
+ offset
+ 8, this->contents_
.data(), length
);
370 // If this FDE is associated with a PLT, fill in the PLT's address
372 if (this->object_
== NULL
)
374 gold_assert(memcmp(oview
+ offset
+ 8, "\0\0\0\0\0\0\0\0", 8) == 0);
377 parameters
->target().plt_fde_location(this->u_
.from_linker
.plt
,
380 uint64_t poffset
= paddress
- (address
+ offset
+ 8);
381 int32_t spoffset
= static_cast<int32_t>(poffset
);
382 uint32_t upsize
= static_cast<uint32_t>(psize
);
383 if (static_cast<uint64_t>(static_cast<int64_t>(spoffset
)) != poffset
384 || static_cast<off_t
>(upsize
) != psize
)
385 gold_warning(_("overflow in PLT unwind data; "
386 "unwinding through PLT may fail"));
387 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 8, spoffset
);
388 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 12, upsize
);
391 if (aligned_full_length
> length
+ 8)
392 memset(oview
+ offset
+ length
+ 8, 0, aligned_full_length
- (length
+ 8));
394 // Tell the exception frame header about this FDE.
395 if (eh_frame_hdr
!= NULL
)
396 eh_frame_hdr
->record_fde(offset
, fde_encoding
);
398 return offset
+ aligned_full_length
;
407 for (std::vector
<Fde
*>::iterator p
= this->fdes_
.begin();
408 p
!= this->fdes_
.end();
413 // Set the output offset of a CIE. Return the new output offset.
416 Cie::set_output_offset(section_offset_type output_offset
,
417 unsigned int addralign
,
418 Merge_map
* merge_map
)
420 size_t length
= this->contents_
.length();
422 // Add 4 for length and 4 for zero CIE identifier tag.
425 if (this->object_
!= NULL
)
427 // Add a mapping so that relocations are applied correctly.
428 merge_map
->add_mapping(this->object_
, this->shndx_
, this->input_offset_
,
429 length
, output_offset
);
432 length
= align_address(length
, addralign
);
434 for (std::vector
<Fde
*>::const_iterator p
= this->fdes_
.begin();
435 p
!= this->fdes_
.end();
438 (*p
)->add_mapping(output_offset
+ length
, merge_map
);
440 size_t fde_length
= (*p
)->length();
441 fde_length
= align_address(fde_length
, addralign
);
442 length
+= fde_length
;
445 return output_offset
+ length
;
448 // Write the CIE to OVIEW starting at OFFSET. Round up the bytes to
449 // ADDRALIGN. ADDRESS is the virtual address of OVIEW.
450 // EH_FRAME_HDR is the exception frame header for FDE recording.
451 // POST_FDES stashes FDEs created after mappings were done, for later
452 // writing. Return the new offset.
454 template<int size
, bool big_endian
>
456 Cie::write(unsigned char* oview
, section_offset_type offset
,
457 uint64_t address
, unsigned int addralign
,
458 Eh_frame_hdr
* eh_frame_hdr
, Post_fdes
* post_fdes
)
460 gold_assert((offset
& (addralign
- 1)) == 0);
462 section_offset_type cie_offset
= offset
;
464 size_t length
= this->contents_
.length();
466 // We add 8 when getting the aligned length to account for the
467 // length word and the CIE tag.
468 size_t aligned_full_length
= align_address(length
+ 8, addralign
);
470 // Write the length of the CIE as a 32-bit word. The length word
471 // does not include the four bytes of the length word itself.
472 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
,
473 aligned_full_length
- 4);
475 // Write the tag which marks this as a CIE: a 32-bit zero.
476 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 4, 0);
478 // Write out the CIE data.
479 memcpy(oview
+ offset
+ 8, this->contents_
.data(), length
);
481 if (aligned_full_length
> length
+ 8)
482 memset(oview
+ offset
+ length
+ 8, 0, aligned_full_length
- (length
+ 8));
484 offset
+= aligned_full_length
;
486 // Write out the associated FDEs.
487 unsigned char fde_encoding
= this->fde_encoding_
;
488 for (std::vector
<Fde
*>::const_iterator p
= this->fdes_
.begin();
489 p
!= this->fdes_
.end();
492 if ((*p
)->post_map())
493 post_fdes
->push_back(Post_fde(*p
, cie_offset
, fde_encoding
));
495 offset
= (*p
)->write
<size
, big_endian
>(oview
, offset
, address
,
496 addralign
, cie_offset
,
497 fde_encoding
, eh_frame_hdr
);
503 // We track all the CIEs we see, and merge them when possible. This
504 // works because each FDE holds an offset to the relevant CIE: we
505 // rewrite the FDEs to point to the merged CIE. This is worthwhile
506 // because in a typical C++ program many FDEs in many different object
507 // files will use the same CIE.
509 // An equality operator for Cie.
512 operator==(const Cie
& cie1
, const Cie
& cie2
)
514 return (cie1
.personality_name_
== cie2
.personality_name_
515 && cie1
.contents_
== cie2
.contents_
);
518 // A less-than operator for Cie.
521 operator<(const Cie
& cie1
, const Cie
& cie2
)
523 if (cie1
.personality_name_
!= cie2
.personality_name_
)
524 return cie1
.personality_name_
< cie2
.personality_name_
;
525 return cie1
.contents_
< cie2
.contents_
;
531 : Output_section_data(Output_data::default_alignment()),
534 unmergeable_cie_offsets_(),
536 mappings_are_done_(false),
541 // Skip an LEB128, updating *PP to point to the next character.
542 // Return false if we ran off the end of the string.
545 Eh_frame::skip_leb128(const unsigned char** pp
, const unsigned char* pend
)
547 const unsigned char* p
;
548 for (p
= *pp
; p
< pend
; ++p
)
550 if ((*p
& 0x80) == 0)
559 // Add input section SHNDX in OBJECT to an exception frame section.
560 // SYMBOLS is the contents of the symbol table section (size
561 // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
562 // SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation
563 // section applying to SHNDX, or 0 if none, or -1U if more than one.
564 // RELOC_TYPE is the type of the reloc section if there is one, either
565 // SHT_REL or SHT_RELA. We try to parse the input exception frame
566 // data into our data structures. If we can't do it, we return false
567 // to mean that the section should be handled as a normal input
570 template<int size
, bool big_endian
>
572 Eh_frame::add_ehframe_input_section(
573 Sized_relobj_file
<size
, big_endian
>* object
,
574 const unsigned char* symbols
,
575 section_size_type symbols_size
,
576 const unsigned char* symbol_names
,
577 section_size_type symbol_names_size
,
579 unsigned int reloc_shndx
,
580 unsigned int reloc_type
)
582 // Get the section contents.
583 section_size_type contents_len
;
584 const unsigned char* pcontents
= object
->section_contents(shndx
,
587 if (contents_len
== 0)
590 // If this is the marker section for the end of the data, then
591 // return false to force it to be handled as an ordinary input
592 // section. If we don't do this, we won't correctly handle the case
593 // of unrecognized .eh_frame sections.
594 if (contents_len
== 4
595 && elfcpp::Swap
<32, big_endian
>::readval(pcontents
) == 0)
599 if (!this->do_add_ehframe_input_section(object
, symbols
, symbols_size
,
600 symbol_names
, symbol_names_size
,
602 reloc_type
, pcontents
,
603 contents_len
, &new_cies
))
605 if (this->eh_frame_hdr_
!= NULL
)
606 this->eh_frame_hdr_
->found_unrecognized_eh_frame_section();
608 for (New_cies::iterator p
= new_cies
.begin();
616 // Now that we know we are using this section, record any new CIEs
618 for (New_cies::const_iterator p
= new_cies
.begin();
623 this->cie_offsets_
.insert(p
->first
);
625 this->unmergeable_cie_offsets_
.push_back(p
->first
);
631 // The bulk of the implementation of add_ehframe_input_section.
633 template<int size
, bool big_endian
>
635 Eh_frame::do_add_ehframe_input_section(
636 Sized_relobj_file
<size
, big_endian
>* object
,
637 const unsigned char* symbols
,
638 section_size_type symbols_size
,
639 const unsigned char* symbol_names
,
640 section_size_type symbol_names_size
,
642 unsigned int reloc_shndx
,
643 unsigned int reloc_type
,
644 const unsigned char* pcontents
,
645 section_size_type contents_len
,
648 Track_relocs
<size
, big_endian
> relocs
;
650 const unsigned char* p
= pcontents
;
651 const unsigned char* pend
= p
+ contents_len
;
653 // Get the contents of the reloc section if any.
654 if (!relocs
.initialize(object
, reloc_shndx
, reloc_type
))
657 // Keep track of which CIEs are at which offsets.
665 // There shouldn't be any relocations here.
666 if (relocs
.advance(p
+ 4 - pcontents
) > 0)
669 unsigned int len
= elfcpp::Swap
<32, big_endian
>::readval(p
);
673 // We should only find a zero-length entry at the end of the
679 // We don't support a 64-bit .eh_frame.
680 if (len
== 0xffffffff)
682 if (static_cast<unsigned int>(pend
- p
) < len
)
685 const unsigned char* const pentend
= p
+ len
;
689 if (relocs
.advance(p
+ 4 - pcontents
) > 0)
692 unsigned int id
= elfcpp::Swap
<32, big_endian
>::readval(p
);
698 if (!this->read_cie(object
, shndx
, symbols
, symbols_size
,
699 symbol_names
, symbol_names_size
,
700 pcontents
, p
, pentend
, &relocs
, &cies
,
707 if (!this->read_fde(object
, shndx
, symbols
, symbols_size
,
708 pcontents
, id
, p
, pentend
, &relocs
, &cies
))
718 // Read a CIE. Return false if we can't parse the information.
720 template<int size
, bool big_endian
>
722 Eh_frame::read_cie(Sized_relobj_file
<size
, big_endian
>* object
,
724 const unsigned char* symbols
,
725 section_size_type symbols_size
,
726 const unsigned char* symbol_names
,
727 section_size_type symbol_names_size
,
728 const unsigned char* pcontents
,
729 const unsigned char* pcie
,
730 const unsigned char* pcieend
,
731 Track_relocs
<size
, big_endian
>* relocs
,
732 Offsets_to_cie
* cies
,
735 bool mergeable
= true;
737 // We need to find the personality routine if there is one, since we
738 // can only merge CIEs which use the same routine. We also need to
739 // find the FDE encoding if there is one, so that we can read the PC
742 const unsigned char* p
= pcie
;
746 unsigned char version
= *p
++;
747 if (version
!= 1 && version
!= 3)
750 const unsigned char* paug
= p
;
751 const void* paugendv
= memchr(p
, '\0', pcieend
- p
);
752 const unsigned char* paugend
= static_cast<const unsigned char*>(paugendv
);
757 if (paug
[0] == 'e' && paug
[1] == 'h')
759 // This is a CIE from gcc before version 3.0. We can't merge
760 // these. We can still read the FDEs.
765 if (pcieend
- p
< size
/ 8)
770 // Skip the code alignment.
771 if (!skip_leb128(&p
, pcieend
))
774 // Skip the data alignment.
775 if (!skip_leb128(&p
, pcieend
))
778 // Skip the return column.
787 if (!skip_leb128(&p
, pcieend
))
794 // Skip the augmentation size.
795 if (!skip_leb128(&p
, pcieend
))
799 unsigned char fde_encoding
= elfcpp::DW_EH_PE_absptr
;
801 while (*paug
!= '\0')
805 case 'L': // LSDA encoding.
811 case 'R': // FDE encoding.
815 switch (fde_encoding
& 7)
817 case elfcpp::DW_EH_PE_absptr
:
818 case elfcpp::DW_EH_PE_udata2
:
819 case elfcpp::DW_EH_PE_udata4
:
820 case elfcpp::DW_EH_PE_udata8
:
823 // We don't expect to see any other cases here, and
824 // we're not prepared to handle them.
834 // Personality encoding.
838 unsigned char per_encoding
= *p
;
841 if ((per_encoding
& 0x60) == 0x60)
843 unsigned int per_width
;
844 switch (per_encoding
& 7)
846 case elfcpp::DW_EH_PE_udata2
:
849 case elfcpp::DW_EH_PE_udata4
:
852 case elfcpp::DW_EH_PE_udata8
:
855 case elfcpp::DW_EH_PE_absptr
:
856 per_width
= size
/ 8;
862 if ((per_encoding
& 0xf0) == elfcpp::DW_EH_PE_aligned
)
864 unsigned int len
= p
- pcie
;
865 len
+= per_width
- 1;
866 len
&= ~ (per_width
- 1);
867 if (static_cast<unsigned int>(pcieend
- p
) < len
)
872 per_offset
= p
- pcontents
;
874 if (static_cast<unsigned int>(pcieend
- p
) < per_width
)
887 const char* personality_name
= "";
888 if (per_offset
!= -1)
890 if (relocs
->advance(per_offset
) > 0)
892 if (relocs
->next_offset() != per_offset
)
895 unsigned int personality_symndx
= relocs
->next_symndx();
896 if (personality_symndx
== -1U)
899 if (personality_symndx
< object
->local_symbol_count())
901 // We can only merge this CIE if the personality routine is
902 // a global symbol. We can still read the FDEs.
907 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
908 if (personality_symndx
>= symbols_size
/ sym_size
)
910 elfcpp::Sym
<size
, big_endian
> sym(symbols
911 + (personality_symndx
* sym_size
));
912 unsigned int name_offset
= sym
.get_st_name();
913 if (name_offset
>= symbol_names_size
)
915 personality_name
= (reinterpret_cast<const char*>(symbol_names
)
919 int r
= relocs
->advance(per_offset
+ 1);
923 if (relocs
->advance(pcieend
- pcontents
) > 0)
926 Cie
cie(object
, shndx
, (pcie
- 8) - pcontents
, fde_encoding
,
927 personality_name
, pcie
, pcieend
- pcie
);
928 Cie
* cie_pointer
= NULL
;
931 Cie_offsets::iterator find_cie
= this->cie_offsets_
.find(&cie
);
932 if (find_cie
!= this->cie_offsets_
.end())
933 cie_pointer
= *find_cie
;
936 // See if we already saw this CIE in this object file.
937 for (New_cies::const_iterator pc
= new_cies
->begin();
938 pc
!= new_cies
->end();
941 if (*(pc
->first
) == cie
)
943 cie_pointer
= pc
->first
;
950 if (cie_pointer
== NULL
)
952 cie_pointer
= new Cie(cie
);
953 new_cies
->push_back(std::make_pair(cie_pointer
, mergeable
));
957 // We are deleting this CIE. Record that in our mapping from
958 // input sections to the output section. At this point we don't
959 // know for sure that we are doing a special mapping for this
960 // input section, but that's OK--if we don't do a special
961 // mapping, nobody will ever ask for the mapping we add here.
962 this->merge_map_
.add_mapping(object
, shndx
, (pcie
- 8) - pcontents
,
963 pcieend
- (pcie
- 8), -1);
966 // Record this CIE plus the offset in the input section.
967 cies
->insert(std::make_pair(pcie
- pcontents
, cie_pointer
));
972 // Read an FDE. Return false if we can't parse the information.
974 template<int size
, bool big_endian
>
976 Eh_frame::read_fde(Sized_relobj_file
<size
, big_endian
>* object
,
978 const unsigned char* symbols
,
979 section_size_type symbols_size
,
980 const unsigned char* pcontents
,
982 const unsigned char* pfde
,
983 const unsigned char* pfdeend
,
984 Track_relocs
<size
, big_endian
>* relocs
,
985 Offsets_to_cie
* cies
)
987 // OFFSET is the distance between the 4 bytes before PFDE to the
988 // start of the CIE. The offset we recorded for the CIE is 8 bytes
989 // after the start of the CIE--after the length and the zero tag.
990 unsigned int cie_offset
= (pfde
- 4 - pcontents
) - offset
+ 8;
991 Offsets_to_cie::const_iterator pcie
= cies
->find(cie_offset
);
992 if (pcie
== cies
->end())
994 Cie
* cie
= pcie
->second
;
996 // The FDE should start with a reloc to the start of the code which
998 if (relocs
->advance(pfde
- pcontents
) > 0)
1001 if (relocs
->next_offset() != pfde
- pcontents
)
1004 unsigned int symndx
= relocs
->next_symndx();
1008 // There can be another reloc in the FDE, if the CIE specifies an
1009 // LSDA (language specific data area). We currently don't care. We
1010 // will care later if we want to optimize the LSDA from an absolute
1011 // pointer to a PC relative offset when generating a shared library.
1012 relocs
->advance(pfdeend
- pcontents
);
1014 unsigned int fde_shndx
;
1015 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1016 if (symndx
>= symbols_size
/ sym_size
)
1018 elfcpp::Sym
<size
, big_endian
> sym(symbols
+ symndx
* sym_size
);
1020 fde_shndx
= object
->adjust_sym_shndx(symndx
, sym
.get_st_shndx(),
1024 && fde_shndx
!= elfcpp::SHN_UNDEF
1025 && fde_shndx
< object
->shnum()
1026 && !object
->is_section_included(fde_shndx
))
1028 // This FDE applies to a section which we are discarding. We
1029 // can discard this FDE.
1030 this->merge_map_
.add_mapping(object
, shndx
, (pfde
- 8) - pcontents
,
1031 pfdeend
- (pfde
- 8), -1);
1035 cie
->add_fde(new Fde(object
, shndx
, (pfde
- 8) - pcontents
,
1036 pfde
, pfdeend
- pfde
));
1041 // Add unwind information for a PLT.
1044 Eh_frame::add_ehframe_for_plt(Output_data
* plt
, const unsigned char* cie_data
,
1045 size_t cie_length
, const unsigned char* fde_data
,
1048 Cie
cie(NULL
, 0, 0, elfcpp::DW_EH_PE_pcrel
| elfcpp::DW_EH_PE_sdata4
, "",
1049 cie_data
, cie_length
);
1050 Cie_offsets::iterator find_cie
= this->cie_offsets_
.find(&cie
);
1052 if (find_cie
!= this->cie_offsets_
.end())
1056 gold_assert(!this->mappings_are_done_
);
1057 pcie
= new Cie(cie
);
1058 this->cie_offsets_
.insert(pcie
);
1061 Fde
* fde
= new Fde(plt
, fde_data
, fde_length
, this->mappings_are_done_
);
1064 if (this->mappings_are_done_
)
1065 this->final_data_size_
+= align_address(fde_length
+ 8, this->addralign());
1068 // Return the number of FDEs.
1071 Eh_frame::fde_count() const
1073 unsigned int ret
= 0;
1074 for (Unmergeable_cie_offsets::const_iterator p
=
1075 this->unmergeable_cie_offsets_
.begin();
1076 p
!= this->unmergeable_cie_offsets_
.end();
1078 ret
+= (*p
)->fde_count();
1079 for (Cie_offsets::const_iterator p
= this->cie_offsets_
.begin();
1080 p
!= this->cie_offsets_
.end();
1082 ret
+= (*p
)->fde_count();
1086 // Set the final data size.
1089 Eh_frame::set_final_data_size()
1091 // We can be called more than once if Layout::set_segment_offsets
1092 // finds a better mapping. We don't want to add all the mappings
1094 if (this->mappings_are_done_
)
1096 this->set_data_size(this->final_data_size_
);
1100 section_offset_type output_offset
= 0;
1102 for (Unmergeable_cie_offsets::iterator p
=
1103 this->unmergeable_cie_offsets_
.begin();
1104 p
!= this->unmergeable_cie_offsets_
.end();
1106 output_offset
= (*p
)->set_output_offset(output_offset
,
1110 for (Cie_offsets::iterator p
= this->cie_offsets_
.begin();
1111 p
!= this->cie_offsets_
.end();
1113 output_offset
= (*p
)->set_output_offset(output_offset
,
1117 this->mappings_are_done_
= true;
1118 this->final_data_size_
= output_offset
;
1120 gold_assert((output_offset
& (this->addralign() - 1)) == 0);
1121 this->set_data_size(output_offset
);
1124 // Return an output offset for an input offset.
1127 Eh_frame::do_output_offset(const Relobj
* object
, unsigned int shndx
,
1128 section_offset_type offset
,
1129 section_offset_type
* poutput
) const
1131 return this->merge_map_
.get_output_offset(object
, shndx
, offset
, poutput
);
1134 // Return whether this is the merge section for an input section.
1137 Eh_frame::do_is_merge_section_for(const Relobj
* object
,
1138 unsigned int shndx
) const
1140 return this->merge_map_
.is_merge_section_for(object
, shndx
);
1143 // Write the data to the output file.
1146 Eh_frame::do_write(Output_file
* of
)
1148 const off_t offset
= this->offset();
1149 const off_t oview_size
= this->data_size();
1150 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1152 switch (parameters
->size_and_endianness())
1154 #ifdef HAVE_TARGET_32_LITTLE
1155 case Parameters::TARGET_32_LITTLE
:
1156 this->do_sized_write
<32, false>(oview
);
1159 #ifdef HAVE_TARGET_32_BIG
1160 case Parameters::TARGET_32_BIG
:
1161 this->do_sized_write
<32, true>(oview
);
1164 #ifdef HAVE_TARGET_64_LITTLE
1165 case Parameters::TARGET_64_LITTLE
:
1166 this->do_sized_write
<64, false>(oview
);
1169 #ifdef HAVE_TARGET_64_BIG
1170 case Parameters::TARGET_64_BIG
:
1171 this->do_sized_write
<64, true>(oview
);
1178 of
->write_output_view(offset
, oview_size
, oview
);
1181 // Write the data to the output file--template version.
1183 template<int size
, bool big_endian
>
1185 Eh_frame::do_sized_write(unsigned char* oview
)
1187 uint64_t address
= this->address();
1188 unsigned int addralign
= this->addralign();
1189 section_offset_type o
= 0;
1190 Post_fdes post_fdes
;
1191 for (Unmergeable_cie_offsets::iterator p
=
1192 this->unmergeable_cie_offsets_
.begin();
1193 p
!= this->unmergeable_cie_offsets_
.end();
1195 o
= (*p
)->write
<size
, big_endian
>(oview
, o
, address
, addralign
,
1196 this->eh_frame_hdr_
, &post_fdes
);
1197 for (Cie_offsets::iterator p
= this->cie_offsets_
.begin();
1198 p
!= this->cie_offsets_
.end();
1200 o
= (*p
)->write
<size
, big_endian
>(oview
, o
, address
, addralign
,
1201 this->eh_frame_hdr_
, &post_fdes
);
1202 for (Post_fdes::iterator p
= post_fdes
.begin();
1203 p
!= post_fdes
.end();
1205 o
= (*p
).fde
->write
<size
, big_endian
>(oview
, o
, address
, addralign
,
1208 this->eh_frame_hdr_
);
1211 #ifdef HAVE_TARGET_32_LITTLE
1214 Eh_frame::add_ehframe_input_section
<32, false>(
1215 Sized_relobj_file
<32, false>* object
,
1216 const unsigned char* symbols
,
1217 section_size_type symbols_size
,
1218 const unsigned char* symbol_names
,
1219 section_size_type symbol_names_size
,
1221 unsigned int reloc_shndx
,
1222 unsigned int reloc_type
);
1225 #ifdef HAVE_TARGET_32_BIG
1228 Eh_frame::add_ehframe_input_section
<32, true>(
1229 Sized_relobj_file
<32, true>* object
,
1230 const unsigned char* symbols
,
1231 section_size_type symbols_size
,
1232 const unsigned char* symbol_names
,
1233 section_size_type symbol_names_size
,
1235 unsigned int reloc_shndx
,
1236 unsigned int reloc_type
);
1239 #ifdef HAVE_TARGET_64_LITTLE
1242 Eh_frame::add_ehframe_input_section
<64, false>(
1243 Sized_relobj_file
<64, false>* object
,
1244 const unsigned char* symbols
,
1245 section_size_type symbols_size
,
1246 const unsigned char* symbol_names
,
1247 section_size_type symbol_names_size
,
1249 unsigned int reloc_shndx
,
1250 unsigned int reloc_type
);
1253 #ifdef HAVE_TARGET_64_BIG
1256 Eh_frame::add_ehframe_input_section
<64, true>(
1257 Sized_relobj_file
<64, true>* object
,
1258 const unsigned char* symbols
,
1259 section_size_type symbols_size
,
1260 const unsigned char* symbol_names
,
1261 section_size_type symbol_names_size
,
1263 unsigned int reloc_shndx
,
1264 unsigned int reloc_type
);
1267 } // End namespace gold.