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