1 // ehframe.cc -- handle exception frame sections for gold
3 // Copyright (C) 2006-2020 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)
91 // Set the size of the exception frame header.
94 Eh_frame_hdr::set_final_data_size()
96 unsigned int data_size
= eh_frame_hdr_size
+ 4;
97 if (!this->any_unrecognized_eh_frame_sections_
)
99 unsigned int fde_count
= this->eh_frame_data_
->fde_count();
101 data_size
+= 4 + 8 * fde_count
;
102 this->fde_offsets_
.reserve(fde_count
);
104 this->set_data_size(data_size
);
107 // Write the data to the file.
110 Eh_frame_hdr::do_write(Output_file
* of
)
112 switch (parameters
->size_and_endianness())
114 #ifdef HAVE_TARGET_32_LITTLE
115 case Parameters::TARGET_32_LITTLE
:
116 this->do_sized_write
<32, false>(of
);
119 #ifdef HAVE_TARGET_32_BIG
120 case Parameters::TARGET_32_BIG
:
121 this->do_sized_write
<32, true>(of
);
124 #ifdef HAVE_TARGET_64_LITTLE
125 case Parameters::TARGET_64_LITTLE
:
126 this->do_sized_write
<64, false>(of
);
129 #ifdef HAVE_TARGET_64_BIG
130 case Parameters::TARGET_64_BIG
:
131 this->do_sized_write
<64, true>(of
);
139 // Write the data to the file with the right endianness.
141 template<int size
, bool big_endian
>
143 Eh_frame_hdr::do_sized_write(Output_file
* of
)
145 const off_t off
= this->offset();
146 const off_t oview_size
= this->data_size();
147 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
152 // Write out a 4 byte PC relative offset to the address of the
153 // .eh_frame section.
154 oview
[1] = elfcpp::DW_EH_PE_pcrel
| elfcpp::DW_EH_PE_sdata4
;
155 uint64_t eh_frame_address
= this->eh_frame_section_
->address();
156 uint64_t eh_frame_hdr_address
= this->address();
157 uint64_t eh_frame_offset
= (eh_frame_address
-
158 (eh_frame_hdr_address
+ 4));
159 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ 4, eh_frame_offset
);
161 if (this->any_unrecognized_eh_frame_sections_
162 || this->fde_offsets_
.empty())
164 // There are no FDEs, or we didn't recognize the format of the
165 // some of the .eh_frame sections, so we can't write out the
167 oview
[2] = elfcpp::DW_EH_PE_omit
;
168 oview
[3] = elfcpp::DW_EH_PE_omit
;
170 gold_assert(oview_size
== 8);
174 oview
[2] = elfcpp::DW_EH_PE_udata4
;
175 oview
[3] = elfcpp::DW_EH_PE_datarel
| elfcpp::DW_EH_PE_sdata4
;
177 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ 8,
178 this->fde_offsets_
.size());
180 // We have the offsets of the FDEs in the .eh_frame section. We
181 // couldn't easily get the PC values before, as they depend on
182 // relocations which are, of course, target specific. This code
183 // is run after all those relocations have been applied to the
184 // output file. Here we read the output file again to find the
185 // PC values. Then we sort the list and write it out.
187 Fde_addresses
<size
> fde_addresses(this->fde_offsets_
.size());
188 this->get_fde_addresses
<size
, big_endian
>(of
, &this->fde_offsets_
,
191 std::sort(fde_addresses
.begin(), fde_addresses
.end(),
192 Fde_address_compare
<size
>());
194 typename
elfcpp::Elf_types
<size
>::Elf_Addr output_address
;
195 output_address
= this->address();
197 unsigned char* pfde
= oview
+ 12;
198 for (typename Fde_addresses
<size
>::iterator p
= fde_addresses
.begin();
199 p
!= fde_addresses
.end();
202 elfcpp::Swap
<32, big_endian
>::writeval(pfde
,
203 p
->first
- output_address
);
204 elfcpp::Swap
<32, big_endian
>::writeval(pfde
+ 4,
205 p
->second
- output_address
);
209 gold_assert(pfde
- oview
== oview_size
);
212 of
->write_output_view(off
, oview_size
, oview
);
215 // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and
216 // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the
217 // FDE's encoding is FDE_ENCODING, return the output address of the
220 template<int size
, bool big_endian
>
221 typename
elfcpp::Elf_types
<size
>::Elf_Addr
222 Eh_frame_hdr::get_fde_pc(
223 typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
,
224 const unsigned char* eh_frame_contents
,
225 section_offset_type fde_offset
,
226 unsigned char fde_encoding
)
228 // The FDE starts with a 4 byte length and a 4 byte offset to the
229 // CIE. The PC follows.
230 const unsigned char* p
= eh_frame_contents
+ fde_offset
+ 8;
232 typename
elfcpp::Elf_types
<size
>::Elf_Addr pc
;
233 bool is_signed
= (fde_encoding
& elfcpp::DW_EH_PE_signed
) != 0;
234 int pc_size
= fde_encoding
& 7;
235 if (pc_size
== elfcpp::DW_EH_PE_absptr
)
238 pc_size
= elfcpp::DW_EH_PE_udata4
;
240 pc_size
= elfcpp::DW_EH_PE_udata8
;
247 case elfcpp::DW_EH_PE_udata2
:
248 pc
= elfcpp::Swap
<16, big_endian
>::readval(p
);
250 pc
= (pc
^ 0x8000) - 0x8000;
253 case elfcpp::DW_EH_PE_udata4
:
254 pc
= elfcpp::Swap
<32, big_endian
>::readval(p
);
255 if (size
> 32 && is_signed
)
256 pc
= (pc
^ 0x80000000) - 0x80000000;
259 case elfcpp::DW_EH_PE_udata8
:
260 gold_assert(size
== 64);
261 pc
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(p
);
265 // All other cases were rejected in Eh_frame::read_cie.
269 switch (fde_encoding
& 0x70)
274 case elfcpp::DW_EH_PE_pcrel
:
275 pc
+= eh_frame_address
+ fde_offset
+ 8;
278 case elfcpp::DW_EH_PE_datarel
:
279 pc
+= parameters
->target().ehframe_datarel_base();
283 // If other cases arise, then we have to handle them, or we have
284 // to reject them by returning false in Eh_frame::read_cie.
288 gold_assert((fde_encoding
& elfcpp::DW_EH_PE_indirect
) == 0);
293 // Given an array of FDE offsets in the .eh_frame section, return an
294 // array of offsets from the exception frame header to the FDE's
295 // output PC and to the output address of the FDE itself. We get the
296 // FDE's PC by actually looking in the .eh_frame section we just wrote
297 // to the output file.
299 template<int size
, bool big_endian
>
301 Eh_frame_hdr::get_fde_addresses(Output_file
* of
,
302 const Fde_offsets
* fde_offsets
,
303 Fde_addresses
<size
>* fde_addresses
)
305 typename
elfcpp::Elf_types
<size
>::Elf_Addr eh_frame_address
;
306 eh_frame_address
= this->eh_frame_section_
->address();
307 off_t eh_frame_offset
= this->eh_frame_section_
->offset();
308 off_t eh_frame_size
= this->eh_frame_section_
->data_size();
309 const unsigned char* eh_frame_contents
= of
->get_input_view(eh_frame_offset
,
312 for (Fde_offsets::const_iterator p
= fde_offsets
->begin();
313 p
!= fde_offsets
->end();
316 typename
elfcpp::Elf_types
<size
>::Elf_Addr fde_pc
;
317 fde_pc
= this->get_fde_pc
<size
, big_endian
>(eh_frame_address
,
319 p
->first
, p
->second
);
320 fde_addresses
->push_back(fde_pc
, eh_frame_address
+ p
->first
);
323 of
->free_input_view(eh_frame_offset
, eh_frame_size
, eh_frame_contents
);
328 // Write the FDE to OVIEW starting at OFFSET. CIE_OFFSET is the
329 // offset of the CIE in OVIEW. OUTPUT_OFFSET is the offset of the
330 // Eh_frame section within the output section. FDE_ENCODING is the
331 // encoding, from the CIE. ADDRALIGN is the required alignment.
332 // ADDRESS is the virtual address of OVIEW. Record the FDE pc for
333 // EH_FRAME_HDR. Return the new offset.
335 template<int size
, bool big_endian
>
337 Fde::write(unsigned char* oview
, section_offset_type output_offset
,
338 section_offset_type offset
, uint64_t address
, unsigned int addralign
,
339 section_offset_type cie_offset
, unsigned char fde_encoding
,
340 Eh_frame_hdr
* eh_frame_hdr
)
342 gold_assert((offset
& (addralign
- 1)) == 0);
344 size_t length
= this->contents_
.length();
346 // We add 8 when getting the aligned length to account for the
347 // length word and the CIE offset.
348 size_t aligned_full_length
= align_address(length
+ 8, addralign
);
350 // Write the length of the FDE as a 32-bit word. The length word
351 // does not include the four bytes of the length word itself, but it
352 // does include the offset to the CIE.
353 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
,
354 aligned_full_length
- 4);
356 // Write the offset to the CIE as a 32-bit word. This is the
357 // difference between the address of the offset word itself and the
359 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 4,
360 offset
+ 4 - cie_offset
);
362 // Copy the rest of the FDE. Note that this is run before
363 // relocation processing is done on this section, so the relocations
364 // will later be applied to the FDE data.
365 memcpy(oview
+ offset
+ 8, this->contents_
.data(), length
);
367 // If this FDE is associated with a PLT, fill in the PLT's address
369 if (this->object_
== NULL
)
371 gold_assert(memcmp(oview
+ offset
+ 8, "\0\0\0\0\0\0\0\0", 8) == 0);
374 parameters
->target().plt_fde_location(this->u_
.from_linker
.plt
,
377 uint64_t poffset
= paddress
- (address
+ offset
+ 8);
378 int32_t spoffset
= static_cast<int32_t>(poffset
);
379 uint32_t upsize
= static_cast<uint32_t>(psize
);
380 if (static_cast<uint64_t>(static_cast<int64_t>(spoffset
)) != poffset
381 || static_cast<off_t
>(upsize
) != psize
)
382 gold_warning(_("overflow in PLT unwind data; "
383 "unwinding through PLT may fail"));
384 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 8, spoffset
);
385 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ offset
+ 12, upsize
);
388 if (aligned_full_length
> length
+ 8)
389 memset(oview
+ offset
+ length
+ 8, 0, aligned_full_length
- (length
+ 8));
391 // Tell the exception frame header about this FDE.
392 if (eh_frame_hdr
!= NULL
)
393 eh_frame_hdr
->record_fde(output_offset
+ offset
, fde_encoding
);
395 return offset
+ aligned_full_length
;
404 for (std::vector
<Fde
*>::iterator p
= this->fdes_
.begin();
405 p
!= this->fdes_
.end();
410 // Set the output offset of a CIE. Return the new output offset.
413 Cie::set_output_offset(section_offset_type output_offset
,
414 unsigned int addralign
,
415 Output_section_data
*output_data
)
417 size_t length
= this->contents_
.length();
419 // Add 4 for length and 4 for zero CIE identifier tag.
422 if (this->object_
!= NULL
)
424 // Add a mapping so that relocations are applied correctly.
425 this->object_
->add_merge_mapping(output_data
, this->shndx_
,
426 this->input_offset_
, length
,
430 length
= align_address(length
, addralign
);
432 for (std::vector
<Fde
*>::const_iterator p
= this->fdes_
.begin();
433 p
!= this->fdes_
.end();
436 (*p
)->add_mapping(output_offset
+ length
, output_data
);
438 size_t fde_length
= (*p
)->length();
439 fde_length
= align_address(fde_length
, addralign
);
440 length
+= fde_length
;
443 return output_offset
+ length
;
446 // Write the CIE to OVIEW starting at OFFSET. OUTPUT_OFFSET is the
447 // offset of the Eh_frame section within the output section. Round up
448 // the bytes to ADDRALIGN. ADDRESS is the virtual address of OVIEW.
449 // EH_FRAME_HDR is the exception frame header for FDE recording.
450 // POST_FDES stashes FDEs created after mappings were done, for later
451 // writing. Return the new offset.
453 template<int size
, bool big_endian
>
455 Cie::write(unsigned char* oview
, section_offset_type output_offset
,
456 section_offset_type offset
, uint64_t address
,
457 unsigned int addralign
, Eh_frame_hdr
* eh_frame_hdr
,
458 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
, output_offset
, offset
,
496 address
, 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_(),
535 mappings_are_done_(false),
540 // Skip an LEB128, updating *PP to point to the next character.
541 // Return false if we ran off the end of the string.
544 Eh_frame::skip_leb128(const unsigned char** pp
, const unsigned char* pend
)
546 const unsigned char* p
;
547 for (p
= *pp
; p
< pend
; ++p
)
549 if ((*p
& 0x80) == 0)
558 // Add input section SHNDX in OBJECT to an exception frame section.
559 // SYMBOLS is the contents of the symbol table section (size
560 // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
561 // SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation
562 // section applying to SHNDX, or 0 if none, or -1U if more than one.
563 // RELOC_TYPE is the type of the reloc section if there is one, either
564 // SHT_REL or SHT_RELA. We try to parse the input exception frame
565 // data into our data structures. If we can't do it, we return false
566 // to mean that the section should be handled as a normal input
569 template<int size
, bool big_endian
>
570 Eh_frame::Eh_frame_section_disposition
571 Eh_frame::add_ehframe_input_section(
572 Sized_relobj_file
<size
, big_endian
>* object
,
573 const unsigned char* symbols
,
574 section_size_type symbols_size
,
575 const unsigned char* symbol_names
,
576 section_size_type symbol_names_size
,
578 unsigned int reloc_shndx
,
579 unsigned int reloc_type
)
581 // Get the section contents.
582 section_size_type contents_len
;
583 const unsigned char* pcontents
= object
->section_contents(shndx
,
586 if (contents_len
== 0)
587 return EH_EMPTY_SECTION
;
589 // If this is the marker section for the end of the data, then
590 // return false to force it to be handled as an ordinary input
591 // section. If we don't do this, we won't correctly handle the case
592 // of unrecognized .eh_frame sections.
593 if (contents_len
== 4
594 && elfcpp::Swap
<32, big_endian
>::readval(pcontents
) == 0)
595 return EH_END_MARKER_SECTION
;
598 if (!this->do_add_ehframe_input_section(object
, symbols
, symbols_size
,
599 symbol_names
, symbol_names_size
,
601 reloc_type
, pcontents
,
602 contents_len
, &new_cies
))
604 if (this->eh_frame_hdr_
!= NULL
)
605 this->eh_frame_hdr_
->found_unrecognized_eh_frame_section();
607 for (New_cies::iterator p
= new_cies
.begin();
612 return EH_UNRECOGNIZED_SECTION
;
615 // Now that we know we are using this section, record any new CIEs
617 for (New_cies::const_iterator p
= new_cies
.begin();
622 this->cie_offsets_
.insert(p
->first
);
624 this->unmergeable_cie_offsets_
.push_back(p
->first
);
627 return EH_OPTIMIZABLE_SECTION
;
630 // The bulk of the implementation of add_ehframe_input_section.
632 template<int size
, bool big_endian
>
634 Eh_frame::do_add_ehframe_input_section(
635 Sized_relobj_file
<size
, big_endian
>* object
,
636 const unsigned char* symbols
,
637 section_size_type symbols_size
,
638 const unsigned char* symbol_names
,
639 section_size_type symbol_names_size
,
641 unsigned int reloc_shndx
,
642 unsigned int reloc_type
,
643 const unsigned char* pcontents
,
644 section_size_type contents_len
,
647 Track_relocs
<size
, big_endian
> relocs
;
649 const unsigned char* p
= pcontents
;
650 const unsigned char* pend
= p
+ contents_len
;
652 // Get the contents of the reloc section if any.
653 if (!relocs
.initialize(object
, reloc_shndx
, reloc_type
))
656 // Keep track of which CIEs are at which offsets.
664 // There shouldn't be any relocations here.
665 if (relocs
.advance(p
+ 4 - pcontents
) > 0)
668 unsigned int len
= elfcpp::Swap
<32, big_endian
>::readval(p
);
672 // We should only find a zero-length entry at the end of the
678 // We don't support a 64-bit .eh_frame.
679 if (len
== 0xffffffff)
681 if (static_cast<unsigned int>(pend
- p
) < len
)
684 const unsigned char* const pentend
= p
+ len
;
688 if (relocs
.advance(p
+ 4 - pcontents
) > 0)
691 unsigned int id
= elfcpp::Swap
<32, big_endian
>::readval(p
);
697 if (!this->read_cie(object
, shndx
, symbols
, symbols_size
,
698 symbol_names
, symbol_names_size
,
699 pcontents
, p
, pentend
, &relocs
, &cies
,
706 if (!this->read_fde(object
, shndx
, symbols
, symbols_size
,
707 pcontents
, id
, p
, pentend
, &relocs
, &cies
))
717 // Read a CIE. Return false if we can't parse the information.
719 template<int size
, bool big_endian
>
721 Eh_frame::read_cie(Sized_relobj_file
<size
, big_endian
>* object
,
723 const unsigned char* symbols
,
724 section_size_type symbols_size
,
725 const unsigned char* symbol_names
,
726 section_size_type symbol_names_size
,
727 const unsigned char* pcontents
,
728 const unsigned char* pcie
,
729 const unsigned char* pcieend
,
730 Track_relocs
<size
, big_endian
>* relocs
,
731 Offsets_to_cie
* cies
,
734 bool mergeable
= true;
736 // We need to find the personality routine if there is one, since we
737 // can only merge CIEs which use the same routine. We also need to
738 // find the FDE encoding if there is one, so that we can read the PC
741 const unsigned char* p
= pcie
;
745 unsigned char version
= *p
++;
746 if (version
!= 1 && version
!= 3)
749 const unsigned char* paug
= p
;
750 const void* paugendv
= memchr(p
, '\0', pcieend
- p
);
751 const unsigned char* paugend
= static_cast<const unsigned char*>(paugendv
);
756 if (paug
[0] == 'e' && paug
[1] == 'h')
758 // This is a CIE from gcc before version 3.0. We can't merge
759 // these. We can still read the FDEs.
764 if (pcieend
- p
< size
/ 8)
769 // Skip the code alignment.
770 if (!skip_leb128(&p
, pcieend
))
773 // Skip the data alignment.
774 if (!skip_leb128(&p
, pcieend
))
777 // Skip the return column.
786 if (!skip_leb128(&p
, pcieend
))
793 // Skip the augmentation size.
794 if (!skip_leb128(&p
, pcieend
))
798 unsigned char fde_encoding
= elfcpp::DW_EH_PE_absptr
;
800 while (*paug
!= '\0')
804 case 'L': // LSDA encoding.
810 case 'R': // FDE encoding.
814 switch (fde_encoding
& 7)
816 case elfcpp::DW_EH_PE_absptr
:
817 case elfcpp::DW_EH_PE_udata2
:
818 case elfcpp::DW_EH_PE_udata4
:
819 case elfcpp::DW_EH_PE_udata8
:
822 // We don't expect to see any other cases here, and
823 // we're not prepared to handle them.
833 // Personality encoding.
837 unsigned char per_encoding
= *p
;
840 if ((per_encoding
& 0x60) == 0x60)
842 unsigned int per_width
;
843 switch (per_encoding
& 7)
845 case elfcpp::DW_EH_PE_udata2
:
848 case elfcpp::DW_EH_PE_udata4
:
851 case elfcpp::DW_EH_PE_udata8
:
854 case elfcpp::DW_EH_PE_absptr
:
855 per_width
= size
/ 8;
861 if ((per_encoding
& 0xf0) == elfcpp::DW_EH_PE_aligned
)
863 unsigned int len
= p
- pcie
;
864 len
+= per_width
- 1;
865 len
&= ~ (per_width
- 1);
866 if (static_cast<unsigned int>(pcieend
- p
) < len
)
871 per_offset
= p
- pcontents
;
873 if (static_cast<unsigned int>(pcieend
- p
) < per_width
)
886 const char* personality_name
= "";
887 if (per_offset
!= -1)
889 if (relocs
->advance(per_offset
) > 0)
891 if (relocs
->next_offset() != per_offset
)
894 unsigned int personality_symndx
= relocs
->next_symndx();
895 if (personality_symndx
== -1U)
898 if (personality_symndx
< object
->local_symbol_count())
900 // We can only merge this CIE if the personality routine is
901 // a global symbol. We can still read the FDEs.
906 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
907 if (personality_symndx
>= symbols_size
/ sym_size
)
909 elfcpp::Sym
<size
, big_endian
> sym(symbols
910 + (personality_symndx
* sym_size
));
911 unsigned int name_offset
= sym
.get_st_name();
912 if (name_offset
>= symbol_names_size
)
914 personality_name
= (reinterpret_cast<const char*>(symbol_names
)
918 int r
= relocs
->advance(per_offset
+ 1);
922 if (relocs
->advance(pcieend
- pcontents
) > 0)
925 Cie
cie(object
, shndx
, (pcie
- 8) - pcontents
, fde_encoding
,
926 personality_name
, pcie
, pcieend
- pcie
);
927 Cie
* cie_pointer
= NULL
;
930 Cie_offsets::iterator find_cie
= this->cie_offsets_
.find(&cie
);
931 if (find_cie
!= this->cie_offsets_
.end())
932 cie_pointer
= *find_cie
;
935 // See if we already saw this CIE in this object file.
936 for (New_cies::const_iterator pc
= new_cies
->begin();
937 pc
!= new_cies
->end();
940 if (*(pc
->first
) == cie
)
942 cie_pointer
= pc
->first
;
949 if (cie_pointer
== NULL
)
951 cie_pointer
= new Cie(cie
);
952 new_cies
->push_back(std::make_pair(cie_pointer
, mergeable
));
956 // We are deleting this CIE. Record that in our mapping from
957 // input sections to the output section. At this point we don't
958 // know for sure that we are doing a special mapping for this
959 // input section, but that's OK--if we don't do a special
960 // mapping, nobody will ever ask for the mapping we add here.
961 object
->add_merge_mapping(this, shndx
, (pcie
- 8) - pcontents
,
962 pcieend
- (pcie
- 8), -1);
965 // Record this CIE plus the offset in the input section.
966 cies
->insert(std::make_pair(pcie
- pcontents
, cie_pointer
));
971 // Read an FDE. Return false if we can't parse the information.
973 template<int size
, bool big_endian
>
975 Eh_frame::read_fde(Sized_relobj_file
<size
, big_endian
>* object
,
977 const unsigned char* symbols
,
978 section_size_type symbols_size
,
979 const unsigned char* pcontents
,
981 const unsigned char* pfde
,
982 const unsigned char* pfdeend
,
983 Track_relocs
<size
, big_endian
>* relocs
,
984 Offsets_to_cie
* cies
)
986 // OFFSET is the distance between the 4 bytes before PFDE to the
987 // start of the CIE. The offset we recorded for the CIE is 8 bytes
988 // after the start of the CIE--after the length and the zero tag.
989 unsigned int cie_offset
= (pfde
- 4 - pcontents
) - offset
+ 8;
990 Offsets_to_cie::const_iterator pcie
= cies
->find(cie_offset
);
991 if (pcie
== cies
->end())
993 Cie
* cie
= pcie
->second
;
996 switch (cie
->fde_encoding() & 7)
998 case elfcpp::DW_EH_PE_udata2
:
1001 case elfcpp::DW_EH_PE_udata4
:
1004 case elfcpp::DW_EH_PE_udata8
:
1005 gold_assert(size
== 64);
1008 case elfcpp::DW_EH_PE_absptr
:
1009 pc_size
= size
== 32 ? 4 : 8;
1012 // All other cases were rejected in Eh_frame::read_cie.
1016 // The FDE should start with a reloc to the start of the code which
1018 if (relocs
->advance(pfde
- pcontents
) > 0)
1020 if (relocs
->next_offset() != pfde
- pcontents
)
1022 // In an object produced by a relocatable link, gold may have
1023 // discarded a COMDAT group in the previous link, but not the
1024 // corresponding FDEs. In that case, gold will have discarded
1025 // the relocations, so the FDE will have a non-relocatable zero
1026 // (regardless of whether the PC encoding is absolute, pc-relative,
1027 // or data-relative) instead of a pointer to the start of the code.
1029 uint64_t pc_value
= 0;
1033 pc_value
= elfcpp::Swap
<16, big_endian
>::readval(pfde
);
1036 pc_value
= elfcpp::Swap
<32, big_endian
>::readval(pfde
);
1039 pc_value
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pfde
);
1047 // This FDE applies to a discarded function. We
1048 // can discard this FDE.
1049 object
->add_merge_mapping(this, shndx
, (pfde
- 8) - pcontents
,
1050 pfdeend
- (pfde
- 8), -1);
1054 // Otherwise, reject the FDE.
1058 unsigned int symndx
= relocs
->next_symndx();
1062 // There can be another reloc in the FDE, if the CIE specifies an
1063 // LSDA (language specific data area). We currently don't care. We
1064 // will care later if we want to optimize the LSDA from an absolute
1065 // pointer to a PC relative offset when generating a shared library.
1066 relocs
->advance(pfdeend
- pcontents
);
1068 // Find the section index for code that this FDE describes.
1069 // If we have discarded the section, we can also discard the FDE.
1070 unsigned int fde_shndx
;
1071 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1072 if (symndx
>= symbols_size
/ sym_size
)
1074 elfcpp::Sym
<size
, big_endian
> sym(symbols
+ symndx
* sym_size
);
1076 fde_shndx
= object
->adjust_sym_shndx(symndx
, sym
.get_st_shndx(),
1078 bool is_discarded
= (is_ordinary
1079 && fde_shndx
!= elfcpp::SHN_UNDEF
1080 && fde_shndx
< object
->shnum()
1081 && !object
->is_section_included(fde_shndx
));
1083 // Fetch the address range field from the FDE. The offset and size
1084 // of the field depends on the PC encoding given in the CIE, but
1085 // it is always an absolute value. If the address range is 0, this
1086 // FDE corresponds to a function that was discarded during optimization
1087 // (too late to discard the corresponding FDE).
1088 uint64_t address_range
= 0;
1092 address_range
= elfcpp::Swap
<16, big_endian
>::readval(pfde
+ 2);
1095 address_range
= elfcpp::Swap
<32, big_endian
>::readval(pfde
+ 4);
1098 address_range
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pfde
+ 8);
1104 if (is_discarded
|| address_range
== 0)
1106 // This FDE applies to a discarded function. We
1107 // can discard this FDE.
1108 object
->add_merge_mapping(this, shndx
, (pfde
- 8) - pcontents
,
1109 pfdeend
- (pfde
- 8), -1);
1113 cie
->add_fde(new Fde(object
, shndx
, (pfde
- 8) - pcontents
,
1114 pfde
, pfdeend
- pfde
));
1119 // Add unwind information for a PLT.
1122 Eh_frame::add_ehframe_for_plt(Output_data
* plt
, const unsigned char* cie_data
,
1123 size_t cie_length
, const unsigned char* fde_data
,
1126 Cie
cie(NULL
, 0, 0, elfcpp::DW_EH_PE_pcrel
| elfcpp::DW_EH_PE_sdata4
, "",
1127 cie_data
, cie_length
);
1128 Cie_offsets::iterator find_cie
= this->cie_offsets_
.find(&cie
);
1130 if (find_cie
!= this->cie_offsets_
.end())
1134 gold_assert(!this->mappings_are_done_
);
1135 pcie
= new Cie(cie
);
1136 this->cie_offsets_
.insert(pcie
);
1139 Fde
* fde
= new Fde(plt
, fde_data
, fde_length
, this->mappings_are_done_
);
1142 if (this->mappings_are_done_
)
1143 this->final_data_size_
+= align_address(fde_length
+ 8, this->addralign());
1146 // Remove all post-map unwind information for a PLT.
1149 Eh_frame::remove_ehframe_for_plt(Output_data
* plt
,
1150 const unsigned char* cie_data
,
1153 if (!this->mappings_are_done_
)
1156 Cie
cie(NULL
, 0, 0, elfcpp::DW_EH_PE_pcrel
| elfcpp::DW_EH_PE_sdata4
, "",
1157 cie_data
, cie_length
);
1158 Cie_offsets::iterator find_cie
= this->cie_offsets_
.find(&cie
);
1159 gold_assert (find_cie
!= this->cie_offsets_
.end());
1160 Cie
* pcie
= *find_cie
;
1162 while (pcie
->fde_count() != 0)
1164 const Fde
* fde
= pcie
->last_fde();
1165 if (!fde
->post_map(plt
))
1167 size_t length
= fde
->length();
1168 this->final_data_size_
-= align_address(length
+ 8, this->addralign());
1173 // Return the number of FDEs.
1176 Eh_frame::fde_count() const
1178 unsigned int ret
= 0;
1179 for (Unmergeable_cie_offsets::const_iterator p
=
1180 this->unmergeable_cie_offsets_
.begin();
1181 p
!= this->unmergeable_cie_offsets_
.end();
1183 ret
+= (*p
)->fde_count();
1184 for (Cie_offsets::const_iterator p
= this->cie_offsets_
.begin();
1185 p
!= this->cie_offsets_
.end();
1187 ret
+= (*p
)->fde_count();
1191 // Set the final data size.
1194 Eh_frame::set_final_data_size()
1196 // We can be called more than once if Layout::set_segment_offsets
1197 // finds a better mapping. We don't want to add all the mappings
1199 if (this->mappings_are_done_
)
1201 this->set_data_size(this->final_data_size_
);
1205 section_offset_type output_start
= 0;
1206 if (this->is_offset_valid())
1207 output_start
= this->offset() - this->output_section()->offset();
1208 section_offset_type output_offset
= output_start
;
1210 for (Unmergeable_cie_offsets::iterator p
=
1211 this->unmergeable_cie_offsets_
.begin();
1212 p
!= this->unmergeable_cie_offsets_
.end();
1214 output_offset
= (*p
)->set_output_offset(output_offset
,
1218 for (Cie_offsets::iterator p
= this->cie_offsets_
.begin();
1219 p
!= this->cie_offsets_
.end();
1221 output_offset
= (*p
)->set_output_offset(output_offset
,
1225 this->mappings_are_done_
= true;
1226 this->final_data_size_
= output_offset
- output_start
;
1228 gold_assert((output_offset
& (this->addralign() - 1)) == 0);
1229 this->set_data_size(this->final_data_size_
);
1232 // Return an output offset for an input offset.
1235 Eh_frame::do_output_offset(const Relobj
* object
, unsigned int shndx
,
1236 section_offset_type offset
,
1237 section_offset_type
* poutput
) const
1239 return object
->merge_output_offset(shndx
, offset
, poutput
);
1242 // Write the data to the output file.
1245 Eh_frame::do_write(Output_file
* of
)
1247 const off_t offset
= this->offset();
1248 const off_t oview_size
= this->data_size();
1249 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1251 switch (parameters
->size_and_endianness())
1253 #ifdef HAVE_TARGET_32_LITTLE
1254 case Parameters::TARGET_32_LITTLE
:
1255 this->do_sized_write
<32, false>(oview
);
1258 #ifdef HAVE_TARGET_32_BIG
1259 case Parameters::TARGET_32_BIG
:
1260 this->do_sized_write
<32, true>(oview
);
1263 #ifdef HAVE_TARGET_64_LITTLE
1264 case Parameters::TARGET_64_LITTLE
:
1265 this->do_sized_write
<64, false>(oview
);
1268 #ifdef HAVE_TARGET_64_BIG
1269 case Parameters::TARGET_64_BIG
:
1270 this->do_sized_write
<64, true>(oview
);
1277 of
->write_output_view(offset
, oview_size
, oview
);
1280 // Write the data to the output file--template version.
1282 template<int size
, bool big_endian
>
1284 Eh_frame::do_sized_write(unsigned char* oview
)
1286 uint64_t address
= this->address();
1287 unsigned int addralign
= this->addralign();
1288 section_offset_type o
= 0;
1289 const off_t output_offset
= this->offset() - this->output_section()->offset();
1290 Post_fdes post_fdes
;
1291 for (Unmergeable_cie_offsets::iterator p
=
1292 this->unmergeable_cie_offsets_
.begin();
1293 p
!= this->unmergeable_cie_offsets_
.end();
1295 o
= (*p
)->write
<size
, big_endian
>(oview
, output_offset
, o
, address
,
1296 addralign
, this->eh_frame_hdr_
,
1298 for (Cie_offsets::iterator p
= this->cie_offsets_
.begin();
1299 p
!= this->cie_offsets_
.end();
1301 o
= (*p
)->write
<size
, big_endian
>(oview
, output_offset
, o
, address
,
1302 addralign
, this->eh_frame_hdr_
,
1304 for (Post_fdes::iterator p
= post_fdes
.begin();
1305 p
!= post_fdes
.end();
1307 o
= (*p
).fde
->write
<size
, big_endian
>(oview
, output_offset
, o
, address
,
1308 addralign
, (*p
).cie_offset
,
1310 this->eh_frame_hdr_
);
1313 #ifdef HAVE_TARGET_32_LITTLE
1315 Eh_frame::Eh_frame_section_disposition
1316 Eh_frame::add_ehframe_input_section
<32, false>(
1317 Sized_relobj_file
<32, false>* object
,
1318 const unsigned char* symbols
,
1319 section_size_type symbols_size
,
1320 const unsigned char* symbol_names
,
1321 section_size_type symbol_names_size
,
1323 unsigned int reloc_shndx
,
1324 unsigned int reloc_type
);
1327 #ifdef HAVE_TARGET_32_BIG
1329 Eh_frame::Eh_frame_section_disposition
1330 Eh_frame::add_ehframe_input_section
<32, true>(
1331 Sized_relobj_file
<32, true>* object
,
1332 const unsigned char* symbols
,
1333 section_size_type symbols_size
,
1334 const unsigned char* symbol_names
,
1335 section_size_type symbol_names_size
,
1337 unsigned int reloc_shndx
,
1338 unsigned int reloc_type
);
1341 #ifdef HAVE_TARGET_64_LITTLE
1343 Eh_frame::Eh_frame_section_disposition
1344 Eh_frame::add_ehframe_input_section
<64, false>(
1345 Sized_relobj_file
<64, false>* object
,
1346 const unsigned char* symbols
,
1347 section_size_type symbols_size
,
1348 const unsigned char* symbol_names
,
1349 section_size_type symbol_names_size
,
1351 unsigned int reloc_shndx
,
1352 unsigned int reloc_type
);
1355 #ifdef HAVE_TARGET_64_BIG
1357 Eh_frame::Eh_frame_section_disposition
1358 Eh_frame::add_ehframe_input_section
<64, true>(
1359 Sized_relobj_file
<64, true>* object
,
1360 const unsigned char* symbols
,
1361 section_size_type symbols_size
,
1362 const unsigned char* symbol_names
,
1363 section_size_type symbol_names_size
,
1365 unsigned int reloc_shndx
,
1366 unsigned int reloc_type
);
1369 } // End namespace gold.