8306f51b6e75b3e68a014e33b1d4f09bcd682904
[deliverable/binutils-gdb.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 section_offset_type 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. ADDRALIGN is the required alignment. Record the FDE pc for
340 // EH_FRAME_HDR. Return the new offset.
341
342 template<int size, bool big_endian>
343 section_offset_type
344 Fde::write(unsigned char* oview, section_offset_type offset,
345 unsigned int addralign, section_offset_type cie_offset,
346 unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr)
347 {
348 gold_assert((offset & (addralign - 1)) == 0);
349
350 size_t length = this->contents_.length();
351
352 // We add 8 when getting the aligned length to account for the
353 // length word and the CIE offset.
354 size_t aligned_full_length = align_address(length + 8, addralign);
355
356 // Write the length of the FDE as a 32-bit word. The length word
357 // does not include the four bytes of the length word itself, but it
358 // does include the offset to the CIE.
359 elfcpp::Swap<32, big_endian>::writeval(oview + offset,
360 aligned_full_length - 4);
361
362 // Write the offset to the CIE as a 32-bit word. This is the
363 // difference between the address of the offset word itself and the
364 // CIE address.
365 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4,
366 offset + 4 - cie_offset);
367
368 // Copy the rest of the FDE. Note that this is run before
369 // relocation processing is done on this section, so the relocations
370 // will later be applied to the FDE data.
371 memcpy(oview + offset + 8, this->contents_.data(), length);
372
373 if (aligned_full_length > length + 8)
374 memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
375
376 // Tell the exception frame header about this FDE.
377 if (eh_frame_hdr != NULL)
378 eh_frame_hdr->record_fde(offset, fde_encoding);
379
380 return offset + aligned_full_length;
381 }
382
383 // Class Cie.
384
385 // Destructor.
386
387 Cie::~Cie()
388 {
389 for (std::vector<Fde*>::iterator p = this->fdes_.begin();
390 p != this->fdes_.end();
391 ++p)
392 delete *p;
393 }
394
395 // Set the output offset of a CIE. Return the new output offset.
396
397 section_offset_type
398 Cie::set_output_offset(section_offset_type output_offset,
399 unsigned int addralign,
400 Merge_map* merge_map)
401 {
402 size_t length = this->contents_.length();
403
404 // Add 4 for length and 4 for zero CIE identifier tag.
405 length += 8;
406
407 merge_map->add_mapping(this->object_, this->shndx_, this->input_offset_,
408 length, output_offset);
409
410 length = align_address(length, addralign);
411
412 for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
413 p != this->fdes_.end();
414 ++p)
415 {
416 (*p)->add_mapping(output_offset + length, merge_map);
417
418 size_t fde_length = (*p)->length();
419 fde_length = align_address(fde_length, addralign);
420 length += fde_length;
421 }
422
423 return output_offset + length;
424 }
425
426 // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is for FDE
427 // recording. Round up the bytes to ADDRALIGN. Return the new
428 // offset.
429
430 template<int size, bool big_endian>
431 section_offset_type
432 Cie::write(unsigned char* oview, section_offset_type offset,
433 unsigned int addralign, Eh_frame_hdr* eh_frame_hdr)
434 {
435 gold_assert((offset & (addralign - 1)) == 0);
436
437 section_offset_type cie_offset = offset;
438
439 size_t length = this->contents_.length();
440
441 // We add 8 when getting the aligned length to account for the
442 // length word and the CIE tag.
443 size_t aligned_full_length = align_address(length + 8, addralign);
444
445 // Write the length of the CIE as a 32-bit word. The length word
446 // does not include the four bytes of the length word itself.
447 elfcpp::Swap<32, big_endian>::writeval(oview + offset,
448 aligned_full_length - 4);
449
450 // Write the tag which marks this as a CIE: a 32-bit zero.
451 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0);
452
453 // Write out the CIE data.
454 memcpy(oview + offset + 8, this->contents_.data(), length);
455
456 if (aligned_full_length > length + 8)
457 memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
458
459 offset += aligned_full_length;
460
461 // Write out the associated FDEs.
462 unsigned char fde_encoding = this->fde_encoding_;
463 for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
464 p != this->fdes_.end();
465 ++p)
466 offset = (*p)->write<size, big_endian>(oview, offset, addralign,
467 cie_offset, fde_encoding,
468 eh_frame_hdr);
469
470 return offset;
471 }
472
473 // We track all the CIEs we see, and merge them when possible. This
474 // works because each FDE holds an offset to the relevant CIE: we
475 // rewrite the FDEs to point to the merged CIE. This is worthwhile
476 // because in a typical C++ program many FDEs in many different object
477 // files will use the same CIE.
478
479 // An equality operator for Cie.
480
481 bool
482 operator==(const Cie& cie1, const Cie& cie2)
483 {
484 return (cie1.personality_name_ == cie2.personality_name_
485 && cie1.contents_ == cie2.contents_);
486 }
487
488 // A less-than operator for Cie.
489
490 bool
491 operator<(const Cie& cie1, const Cie& cie2)
492 {
493 if (cie1.personality_name_ != cie2.personality_name_)
494 return cie1.personality_name_ < cie2.personality_name_;
495 return cie1.contents_ < cie2.contents_;
496 }
497
498 // Class Eh_frame.
499
500 Eh_frame::Eh_frame()
501 : Output_section_data(Output_data::default_alignment()),
502 eh_frame_hdr_(NULL),
503 cie_offsets_(),
504 unmergeable_cie_offsets_(),
505 merge_map_()
506 {
507 }
508
509 // Skip an LEB128, updating *PP to point to the next character.
510 // Return false if we ran off the end of the string.
511
512 bool
513 Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend)
514 {
515 const unsigned char* p;
516 for (p = *pp; p < pend; ++p)
517 {
518 if ((*p & 0x80) == 0)
519 {
520 *pp = p + 1;
521 return true;
522 }
523 }
524 return false;
525 }
526
527 // Add input section SHNDX in OBJECT to an exception frame section.
528 // SYMBOLS is the contents of the symbol table section (size
529 // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
530 // SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation
531 // section applying to SHNDX, or 0 if none, or -1U if more than one.
532 // RELOC_TYPE is the type of the reloc section if there is one, either
533 // SHT_REL or SHT_RELA. We try to parse the input exception frame
534 // data into our data structures. If we can't do it, we return false
535 // to mean that the section should be handled as a normal input
536 // section.
537
538 template<int size, bool big_endian>
539 bool
540 Eh_frame::add_ehframe_input_section(
541 Sized_relobj<size, big_endian>* object,
542 const unsigned char* symbols,
543 section_size_type symbols_size,
544 const unsigned char* symbol_names,
545 section_size_type symbol_names_size,
546 unsigned int shndx,
547 unsigned int reloc_shndx,
548 unsigned int reloc_type)
549 {
550 // Get the section contents.
551 section_size_type contents_len;
552 const unsigned char* pcontents = object->section_contents(shndx,
553 &contents_len,
554 false);
555 if (contents_len == 0)
556 return false;
557
558 // If this is the marker section for the end of the data, then
559 // return false to force it to be handled as an ordinary input
560 // section. If we don't do this, we won't correctly handle the case
561 // of unrecognized .eh_frame sections.
562 if (contents_len == 4
563 && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0)
564 return false;
565
566 New_cies new_cies;
567 if (!this->do_add_ehframe_input_section(object, symbols, symbols_size,
568 symbol_names, symbol_names_size,
569 shndx, reloc_shndx,
570 reloc_type, pcontents,
571 contents_len, &new_cies))
572 {
573 this->eh_frame_hdr_->found_unrecognized_eh_frame_section();
574
575 for (New_cies::iterator p = new_cies.begin();
576 p != new_cies.end();
577 ++p)
578 delete p->first;
579
580 return false;
581 }
582
583 // Now that we know we are using this section, record any new CIEs
584 // that we found.
585 for (New_cies::const_iterator p = new_cies.begin();
586 p != new_cies.end();
587 ++p)
588 {
589 if (p->second)
590 this->cie_offsets_.insert(p->first);
591 else
592 this->unmergeable_cie_offsets_.push_back(p->first);
593 }
594
595 return true;
596 }
597
598 // The bulk of the implementation of add_ehframe_input_section.
599
600 template<int size, bool big_endian>
601 bool
602 Eh_frame::do_add_ehframe_input_section(
603 Sized_relobj<size, big_endian>* object,
604 const unsigned char* symbols,
605 section_size_type symbols_size,
606 const unsigned char* symbol_names,
607 section_size_type symbol_names_size,
608 unsigned int shndx,
609 unsigned int reloc_shndx,
610 unsigned int reloc_type,
611 const unsigned char* pcontents,
612 section_size_type contents_len,
613 New_cies* new_cies)
614 {
615 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
616 Track_relocs<size, big_endian> relocs;
617
618 const unsigned char* p = pcontents;
619 const unsigned char* pend = p + contents_len;
620
621 // Get the contents of the reloc section if any.
622 if (!relocs.initialize(object, reloc_shndx, reloc_type))
623 return false;
624
625 // Keep track of which CIEs are at which offsets.
626 Offsets_to_cie cies;
627
628 while (p < pend)
629 {
630 if (pend - p < 4)
631 return false;
632
633 // There shouldn't be any relocations here.
634 if (relocs.advance(p + 4 - pcontents) > 0)
635 return false;
636
637 unsigned int len = elfcpp::Swap<32, big_endian>::readval(p);
638 p += 4;
639 if (len == 0)
640 {
641 // We should only find a zero-length entry at the end of the
642 // section.
643 if (p < pend)
644 return false;
645 break;
646 }
647 // We don't support a 64-bit .eh_frame.
648 if (len == 0xffffffff)
649 return false;
650 if (static_cast<unsigned int>(pend - p) < len)
651 return false;
652
653 const unsigned char* const pentend = p + len;
654
655 if (pend - p < 4)
656 return false;
657 if (relocs.advance(p + 4 - pcontents) > 0)
658 return false;
659
660 unsigned int id = elfcpp::Swap<32, big_endian>::readval(p);
661 p += 4;
662
663 if (id == 0)
664 {
665 // CIE.
666 if (!this->read_cie(object, shndx, symbols, symbols_size,
667 symbol_names, symbol_names_size,
668 pcontents, p, pentend, &relocs, &cies,
669 new_cies))
670 return false;
671 }
672 else
673 {
674 // FDE.
675 if (!this->read_fde(object, shndx, symbols, symbols_size,
676 pcontents, id, p, pentend, &relocs, &cies))
677 return false;
678 }
679
680 p = pentend;
681 }
682
683 return true;
684 }
685
686 // Read a CIE. Return false if we can't parse the information.
687
688 template<int size, bool big_endian>
689 bool
690 Eh_frame::read_cie(Sized_relobj<size, big_endian>* object,
691 unsigned int shndx,
692 const unsigned char* symbols,
693 section_size_type symbols_size,
694 const unsigned char* symbol_names,
695 section_size_type symbol_names_size,
696 const unsigned char* pcontents,
697 const unsigned char* pcie,
698 const unsigned char *pcieend,
699 Track_relocs<size, big_endian>* relocs,
700 Offsets_to_cie* cies,
701 New_cies* new_cies)
702 {
703 bool mergeable = true;
704
705 // We need to find the personality routine if there is one, since we
706 // can only merge CIEs which use the same routine. We also need to
707 // find the FDE encoding if there is one, so that we can read the PC
708 // from the FDE.
709
710 const unsigned char* p = pcie;
711
712 if (pcieend - p < 1)
713 return false;
714 unsigned char version = *p++;
715 if (version != 1 && version != 3)
716 return false;
717
718 const unsigned char* paug = p;
719 const void* paugendv = memchr(p, '\0', pcieend - p);
720 const unsigned char* paugend = static_cast<const unsigned char*>(paugendv);
721 if (paugend == NULL)
722 return false;
723 p = paugend + 1;
724
725 if (paug[0] == 'e' && paug[1] == 'h')
726 {
727 // This is a CIE from gcc before version 3.0. We can't merge
728 // these. We can still read the FDEs.
729 mergeable = false;
730 paug += 2;
731 if (*paug != '\0')
732 return false;
733 if (pcieend - p < size / 8)
734 return false;
735 p += size / 8;
736 }
737
738 // Skip the code alignment.
739 if (!skip_leb128(&p, pcieend))
740 return false;
741
742 // Skip the data alignment.
743 if (!skip_leb128(&p, pcieend))
744 return false;
745
746 // Skip the return column.
747 if (version == 1)
748 {
749 if (pcieend - p < 1)
750 return false;
751 ++p;
752 }
753 else
754 {
755 if (!skip_leb128(&p, pcieend))
756 return false;
757 }
758
759 if (*paug == 'z')
760 {
761 ++paug;
762 // Skip the augmentation size.
763 if (!skip_leb128(&p, pcieend))
764 return false;
765 }
766
767 unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr;
768 int per_offset = -1;
769 while (*paug != '\0')
770 {
771 switch (*paug)
772 {
773 case 'L': // LSDA encoding.
774 if (pcieend - p < 1)
775 return false;
776 ++p;
777 break;
778
779 case 'R': // FDE encoding.
780 if (pcieend - p < 1)
781 return false;
782 fde_encoding = *p;
783 switch (fde_encoding & 7)
784 {
785 case elfcpp::DW_EH_PE_absptr:
786 case elfcpp::DW_EH_PE_udata2:
787 case elfcpp::DW_EH_PE_udata4:
788 case elfcpp::DW_EH_PE_udata8:
789 break;
790 default:
791 // We don't expect to see any other cases here, and
792 // we're not prepared to handle them.
793 return false;
794 }
795 ++p;
796 break;
797
798 case 'S':
799 break;
800
801 case 'P':
802 // Personality encoding.
803 {
804 if (pcieend - p < 1)
805 return false;
806 unsigned char per_encoding = *p;
807 ++p;
808
809 if ((per_encoding & 0x60) == 0x60)
810 return false;
811 unsigned int per_width;
812 switch (per_encoding & 7)
813 {
814 case elfcpp::DW_EH_PE_udata2:
815 per_width = 2;
816 break;
817 case elfcpp::DW_EH_PE_udata4:
818 per_width = 4;
819 break;
820 case elfcpp::DW_EH_PE_udata8:
821 per_width = 8;
822 break;
823 case elfcpp::DW_EH_PE_absptr:
824 per_width = size / 8;
825 break;
826 default:
827 return false;
828 }
829
830 if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned)
831 {
832 unsigned int len = p - pcie;
833 len += per_width - 1;
834 len &= ~ (per_width - 1);
835 if (static_cast<unsigned int>(pcieend - p) < len)
836 return false;
837 p += len;
838 }
839
840 per_offset = p - pcontents;
841
842 if (static_cast<unsigned int>(pcieend - p) < per_width)
843 return false;
844 p += per_width;
845 }
846 break;
847
848 default:
849 return false;
850 }
851
852 ++paug;
853 }
854
855 const char* personality_name = "";
856 if (per_offset != -1)
857 {
858 if (relocs->advance(per_offset) > 0)
859 return false;
860 if (relocs->next_offset() != per_offset)
861 return false;
862
863 unsigned int personality_symndx = relocs->next_symndx();
864 if (personality_symndx == -1U)
865 return false;
866
867 if (personality_symndx < object->local_symbol_count())
868 {
869 // We can only merge this CIE if the personality routine is
870 // a global symbol. We can still read the FDEs.
871 mergeable = false;
872 }
873 else
874 {
875 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
876 if (personality_symndx >= symbols_size / sym_size)
877 return false;
878 elfcpp::Sym<size, big_endian> sym(symbols
879 + (personality_symndx * sym_size));
880 unsigned int name_offset = sym.get_st_name();
881 if (name_offset >= symbol_names_size)
882 return false;
883 personality_name = (reinterpret_cast<const char*>(symbol_names)
884 + name_offset);
885 }
886
887 int r = relocs->advance(per_offset + 1);
888 gold_assert(r == 1);
889 }
890
891 if (relocs->advance(pcieend - pcontents) > 0)
892 return false;
893
894 Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding,
895 personality_name, pcie, pcieend - pcie);
896 Cie* cie_pointer = NULL;
897 if (mergeable)
898 {
899 Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
900 if (find_cie != this->cie_offsets_.end())
901 cie_pointer = *find_cie;
902 else
903 {
904 // See if we already saw this CIE in this object file.
905 for (New_cies::const_iterator pc = new_cies->begin();
906 pc != new_cies->end();
907 ++pc)
908 {
909 if (*(pc->first) == cie)
910 {
911 cie_pointer = pc->first;
912 break;
913 }
914 }
915 }
916 }
917
918 if (cie_pointer == NULL)
919 {
920 cie_pointer = new Cie(cie);
921 new_cies->push_back(std::make_pair(cie_pointer, mergeable));
922 }
923 else
924 {
925 // We are deleting this CIE. Record that in our mapping from
926 // input sections to the output section. At this point we don't
927 // know for sure that we are doing a special mapping for this
928 // input section, but that's OK--if we don't do a special
929 // mapping, nobody will ever ask for the mapping we add here.
930 this->merge_map_.add_mapping(object, shndx, (pcie - 8) - pcontents,
931 pcieend - (pcie - 8), -1);
932 }
933
934 // Record this CIE plus the offset in the input section.
935 cies->insert(std::make_pair(pcie - pcontents, cie_pointer));
936
937 return true;
938 }
939
940 // Read an FDE. Return false if we can't parse the information.
941
942 template<int size, bool big_endian>
943 bool
944 Eh_frame::read_fde(Sized_relobj<size, big_endian>* object,
945 unsigned int shndx,
946 const unsigned char* symbols,
947 section_size_type symbols_size,
948 const unsigned char* pcontents,
949 unsigned int offset,
950 const unsigned char* pfde,
951 const unsigned char *pfdeend,
952 Track_relocs<size, big_endian>* relocs,
953 Offsets_to_cie* cies)
954 {
955 // OFFSET is the distance between the 4 bytes before PFDE to the
956 // start of the CIE. The offset we recorded for the CIE is 8 bytes
957 // after the start of the CIE--after the length and the zero tag.
958 unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8;
959 Offsets_to_cie::const_iterator pcie = cies->find(cie_offset);
960 if (pcie == cies->end())
961 return false;
962 Cie* cie = pcie->second;
963
964 // The FDE should start with a reloc to the start of the code which
965 // it describes.
966 if (relocs->advance(pfde - pcontents) > 0)
967 return false;
968
969 if (relocs->next_offset() != pfde - pcontents)
970 return false;
971
972 unsigned int symndx = relocs->next_symndx();
973 if (symndx == -1U)
974 return false;
975
976 // There can be another reloc in the FDE, if the CIE specifies an
977 // LSDA (language specific data area). We currently don't care. We
978 // will care later if we want to optimize the LSDA from an absolute
979 // pointer to a PC relative offset when generating a shared library.
980 relocs->advance(pfdeend - pcontents);
981
982 unsigned int fde_shndx;
983 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
984 if (symndx >= symbols_size / sym_size)
985 return false;
986 elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size);
987 fde_shndx = sym.get_st_shndx();
988
989 if (fde_shndx != elfcpp::SHN_UNDEF
990 && fde_shndx < object->shnum()
991 && !object->is_section_included(fde_shndx))
992 {
993 // This FDE applies to a section which we are discarding. We
994 // can discard this FDE.
995 this->merge_map_.add_mapping(object, shndx, (pfde - 8) - pcontents,
996 pfdeend - (pfde - 8), -1);
997 return true;
998 }
999
1000 cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents,
1001 pfde, pfdeend - pfde));
1002
1003 return true;
1004 }
1005
1006 // Return the number of FDEs.
1007
1008 unsigned int
1009 Eh_frame::fde_count() const
1010 {
1011 unsigned int ret = 0;
1012 for (Unmergeable_cie_offsets::const_iterator p =
1013 this->unmergeable_cie_offsets_.begin();
1014 p != this->unmergeable_cie_offsets_.end();
1015 ++p)
1016 ret += (*p)->fde_count();
1017 for (Cie_offsets::const_iterator p = this->cie_offsets_.begin();
1018 p != this->cie_offsets_.end();
1019 ++p)
1020 ret += (*p)->fde_count();
1021 return ret;
1022 }
1023
1024 // Set the final data size.
1025
1026 void
1027 Eh_frame::set_final_data_size()
1028 {
1029 section_offset_type output_offset = 0;
1030
1031 for (Unmergeable_cie_offsets::iterator p =
1032 this->unmergeable_cie_offsets_.begin();
1033 p != this->unmergeable_cie_offsets_.end();
1034 ++p)
1035 output_offset = (*p)->set_output_offset(output_offset,
1036 this->addralign(),
1037 &this->merge_map_);
1038
1039 for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1040 p != this->cie_offsets_.end();
1041 ++p)
1042 output_offset = (*p)->set_output_offset(output_offset,
1043 this->addralign(),
1044 &this->merge_map_);
1045
1046 gold_assert((output_offset & (this->addralign() - 1)) == 0);
1047 this->set_data_size(output_offset);
1048 }
1049
1050 // Return an output offset for an input offset.
1051
1052 bool
1053 Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx,
1054 section_offset_type offset,
1055 section_offset_type* poutput) const
1056 {
1057 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
1058 }
1059
1060 // Return whether this is the merge section for an input section.
1061
1062 bool
1063 Eh_frame::do_is_merge_section_for(const Relobj* object,
1064 unsigned int shndx) const
1065 {
1066 return this->merge_map_.is_merge_section_for(object, shndx);
1067 }
1068
1069 // Write the data to the output file.
1070
1071 void
1072 Eh_frame::do_write(Output_file* of)
1073 {
1074 const off_t offset = this->offset();
1075 const off_t oview_size = this->data_size();
1076 unsigned char* const oview = of->get_output_view(offset, oview_size);
1077
1078 if (parameters->get_size() == 32)
1079 {
1080 if (!parameters->is_big_endian())
1081 {
1082 #ifdef HAVE_TARGET_32_LITTLE
1083 this->do_sized_write<32, false>(oview);
1084 #else
1085 gold_unreachable();
1086 #endif
1087 }
1088 else
1089 {
1090 #ifdef HAVE_TARGET_32_BIG
1091 this->do_sized_write<32, true>(oview);
1092 #else
1093 gold_unreachable();
1094 #endif
1095 }
1096 }
1097 else if (parameters->get_size() == 64)
1098 {
1099 if (!parameters->is_big_endian())
1100 {
1101 #ifdef HAVE_TARGET_64_LITTLE
1102 this->do_sized_write<64, false>(oview);
1103 #else
1104 gold_unreachable();
1105 #endif
1106 }
1107 else
1108 {
1109 #ifdef HAVE_TARGET_64_BIG
1110 this->do_sized_write<64, true>(oview);
1111 #else
1112 gold_unreachable();
1113 #endif
1114 }
1115 }
1116 else
1117 gold_unreachable();
1118
1119 of->write_output_view(offset, oview_size, oview);
1120 }
1121
1122 // Write the data to the output file--template version.
1123
1124 template<int size, bool big_endian>
1125 void
1126 Eh_frame::do_sized_write(unsigned char* oview)
1127 {
1128 unsigned int addralign = this->addralign();
1129 section_offset_type o = 0;
1130 for (Unmergeable_cie_offsets::iterator p =
1131 this->unmergeable_cie_offsets_.begin();
1132 p != this->unmergeable_cie_offsets_.end();
1133 ++p)
1134 o = (*p)->write<size, big_endian>(oview, o, addralign,
1135 this->eh_frame_hdr_);
1136 for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1137 p != this->cie_offsets_.end();
1138 ++p)
1139 o = (*p)->write<size, big_endian>(oview, o, addralign,
1140 this->eh_frame_hdr_);
1141 }
1142
1143 #ifdef HAVE_TARGET_32_LITTLE
1144 template
1145 bool
1146 Eh_frame::add_ehframe_input_section<32, false>(
1147 Sized_relobj<32, false>* object,
1148 const unsigned char* symbols,
1149 section_size_type symbols_size,
1150 const unsigned char* symbol_names,
1151 section_size_type symbol_names_size,
1152 unsigned int shndx,
1153 unsigned int reloc_shndx,
1154 unsigned int reloc_type);
1155 #endif
1156
1157 #ifdef HAVE_TARGET_32_BIG
1158 template
1159 bool
1160 Eh_frame::add_ehframe_input_section<32, true>(
1161 Sized_relobj<32, true>* object,
1162 const unsigned char* symbols,
1163 section_size_type symbols_size,
1164 const unsigned char* symbol_names,
1165 section_size_type symbol_names_size,
1166 unsigned int shndx,
1167 unsigned int reloc_shndx,
1168 unsigned int reloc_type);
1169 #endif
1170
1171 #ifdef HAVE_TARGET_64_LITTLE
1172 template
1173 bool
1174 Eh_frame::add_ehframe_input_section<64, false>(
1175 Sized_relobj<64, false>* object,
1176 const unsigned char* symbols,
1177 section_size_type symbols_size,
1178 const unsigned char* symbol_names,
1179 section_size_type symbol_names_size,
1180 unsigned int shndx,
1181 unsigned int reloc_shndx,
1182 unsigned int reloc_type);
1183 #endif
1184
1185 #ifdef HAVE_TARGET_64_BIG
1186 template
1187 bool
1188 Eh_frame::add_ehframe_input_section<64, true>(
1189 Sized_relobj<64, true>* object,
1190 const unsigned char* symbols,
1191 section_size_type symbols_size,
1192 const unsigned char* symbol_names,
1193 section_size_type symbol_names_size,
1194 unsigned int shndx,
1195 unsigned int reloc_shndx,
1196 unsigned int reloc_type);
1197 #endif
1198
1199 } // End namespace gold.
This page took 0.053924 seconds and 4 git commands to generate.